What is du?
du is a standard command in Linux that quickly shows information on disk usage. The name of the command itself stands for “disk usage.” While this may seem very basic, du has a whole host of options and uses that make it a valuable tool for both beginners and advanced Linux users.
How to Start Using du
You may be wondering how to start using du. The simplest and most basic way is, of course, using the command as is without any options: However, this isn’t particularly useful information. The output given here is often used by other programs and scripts, but to make it useful for the average user, we will need to include some options. One such option is to convert the size outputs to a format you can read. To do this, you will need to add either the -h flag or the –human-readable flag. This makes the size of the files on the left much easier to parse. But there’s still a lot to sort through here. Luckily, some other options make this easier. The -s flag will summarize the given directory’s disk usage. This is often combined with the -h flag to give a very simple and easy-to-read summary of the directory. Depending on which directories you’re checking with du, you may need to elevate your privileges using sudo or similar. For example: when you want to see how much disk usage the root directory is using. While there are many other useful options you can use with du, the -h and -s flags are the only ones we need in order to clean up disk space.
How to Free Up Linux Disk Space with du
Now that you know how to use du, you can make use of it when freeing up disk space on your computer. The quickest way to clean up your disk is to first find out what is using the most space. You can do this by letting du scan the entire system. To do this, you will add an asterisk to the end of the directory path. The asterisk works as a wildcard and tells the command to look at everything in the directory. This shows you which directory in the filesystem is occupying the most space. From there, you can use the same command in each directory to find out which sub-directories and files are taking up the most space. In many cases, you will be searching your home directory, as this is where you will store your personal files. You can do this by searching ~/, as this is shorthand for your home directory. Continue to repeat these commands as much as needed to narrow down which directories are taking up the most space. For example, if you have a directory called “Storage” in your home folder taking up a lot of space, use du to figure out which files in it are the biggest. From here, you can simply delete the files you don’t want to use the rm command. Do this for every other file you no longer want on your drive to free up the desired amount of storage space.
1. Is there a way to sort by file size?
Yes, you can pipe the output from du into sort in order to sort by file size.
2. How do I see how much free disk space I have?
You would use df, a completely different command with a very simple usage, to see how much free disk space you have:
3. Should I use du to remove programs from my computer?
No, this method is only useful for files you’ve saved yourself. If you want to remove a program, you should use your distribution’s package manager or software center.
Wrapping Up
As you can see, freeing up disk space in Linux using the command line is quite simple. Once you have the hang of using du and all its options, you’ll find that it’s also much quicker and easier than most other methods. But this is just the tip of the iceberg when it comes to commands. Read on to find out which commands to use to search for files in Linux.