How To Use Du Command.
The du command, short for “disk usage” reports the estimated amount of disk space used by given files or directories. It is practically useful for finding files and directories taking up large amounts of disk space.
How to Use the du command
The general syntax for the du command is as follows:
If the given FILE is a directory, du will summarize disk usage of each file and subdirectory in that directory. If no FILE is specified, du will report the disk usage of the current working directory .
When executed without any option du displays the disk usage of the given file or directory and each of its subdirectories in bytes.
You can also pass multiple files and directories to the du command as arguments:
If you run du on a file or directory for which you don’t have permissions, you will get something like “du: cannot read directory”. In this situation, you’ll need to prepend the command with sudo .
du has lots of options, we’ll outline just the most frequently used ones.
The -a option tells du to report the disk space usage of each file within the directory.
Usually, you would want to display only the space occupied by the given directory in a human-readable format. To do that, use the -h option.
For example, to get the total size of the /var/lib and all of its subdirectories, you would run the following command:
We are using sudo because most of the files and directories inside the /var/lib directory are owned by the root user and are not readable by the regular users. The output will look something like this:
To report only the total size of the specified directory, and not for subdirectories use the -s option:
The -c option tells du to report a grand total. This is useful when you want to get the combined size of two or more directories.
If you want to display the disk usage of the n-level subdirectories use the –max-depth option and specify the subdirectories level. For example, to get a report about the first-level directories you would use:
The default behavior of the du utility is to re the disk space used by the directory or file. To find the apparent size of a file, use the –apparent-size switch. The “apparent size” of a file is how much data is actually in the file.
du also allows you to use shell pattern. For example, to get the size of all directories starting with “Do” in your home directory you would run:
Using du with Other Commands
The du command can be combined with other commands with pipes.
For example, to print the 5 largest directories inside the /var directory you would pass the output of du to the sort command to sort the directories by their size and then pipe the output to the head command which will print only the top 5 directories:
By now you should have a good understanding of how to use the du command.
unlike df which prints information about the disk usage of the mounted file systems , the du command gives you estimate of disk space used by given files or directories.