Du Command in Linux
4 min read
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
The general syntax for the
du command is as follows:
du [OPTIONS]... FILE...
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:
du ~/Documents ~/Pictures ~/.zshrc
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
du has lots of options, we’ll outline just the most frequently used ones.
-a option tells
du to report the disk space usage of each file within the directory.
du -a ~/Documents
Usually, you would want to display only the space occupied by the given directory in a human-readable format. To do that, use the
For example, to get the total size of the
/var/lib and all of its subdirectories, you would run the following command:
sudo du -h /var
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
sudo du -sh /var
-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.
sudo du -csh /var/log /var/lib
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:
sudo du -h --max-depth=1 /var/lib
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.
sudo du -sh --apparent-size /var/lib
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:
sudo du -csh ~/Do*
du with Other Commands
du command can be combined with other commands with pipes.
For example, to print the 5 largest directories
/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
command which will print only the top 5 directories:
sudo du -h /var/ | sort -rh | head -5
By now you should have a good understanding of how to use the
You can view all available
du command options by typing
man du in your terminal.
If you have any questions or feedback, please leave a comment below.