On any operating system, the files on your hard disk take up a certain amount of space. In Linux specifically, you can view how much space that these files take up in the command line terminal by using the du command. The du command (the name is shortened from “disk usage”), as the name implies, will simply display, in its output, the amount of disk space being used by a specified file or directory.
For example, if you’re wondering why it seems like there are files that are consuming more disk space than they should be, you can use the du command to ascertain which files are the cause of it. In this tutorial, we will discuss the typical applications of the du command and some of its options to help you best utilize the space in your Linux system’s hard disk.
In this tutorial you will learn:
- How to use the du command on Linux
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
|Conventions||# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Frequently Used Options
du command in Linux Basic Examples
- We can use the du command for both files and directories. But for directories specifically, running the du command will simply display the disk usage of every file and subdirectory inside. If you run the du command by itself, without any additional options or a specified file, it will display the disk usage of the current working directory.
$ du /etc 8 /etc/cracklib 12 /etc/vim 12 /etc/cron.monthly [...] total: 11312
Sometimes, though, you might have to view the disk usage of a file or directory that you do not have permissions for. If you try to run the du command on these files or directories, the output will display an error that reads “du: cannot read directory”. You can prevent this by evoking the sudo command.
- We can also use the du command to view the disk usage of multiple files or directories at the same time, instead of just one.
$ du /etc /tmp
In the screenshot above, you can see that we used the sudo command so that the du command has permission to read certain files and directories in the
- In the examples above, we showed how the du command displays the disk usage of every file or subdirectory in a given directory. But we can go one step further from that with the
-a(shortened from “all”) command option and display the disk usage of every file within the entire specified file path instead of only the tail end directory.
$ du -a /home/user
- We can utilize the
-hoption to display the disk usage of a file or directory in a human readable format.
$ du -h /var
You can always use the man command to read more about the touch command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.
The du command is pretty simple, but as you’ve observed throughout the examples section of this article, it comes packed with a lot of options. Many of these options fly under the radar, and even some seasoned system administrators may not know them. However, they can definitely come in handy in various situations. In this section of the tutorial, we’ll show you a few of the lesser known options of the du command that we think are useful.
du command in Linux Advanced Examples
- In the examples above, we’ve covered how, by default, the du command displays the disk usage of a directory and every subdirectory within it. If there is a situation in which you need to view the disk space of one directory exclusively, we can use the
-scommand line option. This option will completely omit, from its output, any subdirectories inside the directory you specify in the command line.
$ du -s Downloads
- We’ve already shown how the du command can be used to display the disk usage of multiple files or directories at the same time. But it can become quite tedious or challenging to summarize the disk usage when there are many files and subdirectories with additional files inside. In this situation, we could simply use the
-coption combines the size of multiple directories and calculates the total, so you don’t have to do any of the heavy lifting.
$ du -c /etc /tmp
As you can see in the screenshot above, while the total disk space usage of the specified directories is displayed, the individual amounts are also shown.
- We can also get the du command to display the absolute amount of data stored within a file or directory, as by default, the du command displays the amount of data that is actually using the space in a hard disk. We can achieve this by using
$ du --apparent-size /home/user 12 /home/linuxconfig/.config/evolution/sources 16 /home/linuxconfig/.config/evolution 152 /home/linuxconfig/.config [...] total: 45955
- You can also specify the depth that the du command will go within a directory with the
--max-depthoption. For example, we would use the syntax below to display the disk usage of one subdirectory within the
Downloadsdirectory, while omitting its subdirectories.
$ du --max-depth=2 Downloads
- We can also use the du command with the
--excludeoption to exclude specified files and directories from its output. We can do this by passing the pattern of the file and directory names as an argument for the du command. For example, we can exclude all hidden files in our home directory passing the
./.name pattern to the du command, as all hidden file names start with a period.
$ du -ha --exclude=./.*
- We can also specify multiple exclusion patterns instead of just one like we did in the previous example. This can be achieved by repeating the
--excludeoption in the same syntax on the command line.
$ du -ha --exclude=./.* --exclude=./P*
As you can see in the screenshot above, we excluded both hidden files and directories and the files and directories whose names start with a P.
- An alternative way of excluding files and directories with the du command is by using the
--exclude-fromcommand line options. This option differs from the one in the previous example in that it takes the name of a text file as an argument, and runs the du command on the patterns written inside. So, for example, let’s create a text file called
exclusions.txtand add in our patterns. We’ll then pass it to the du command.
$ du -ha --exclude-from=exclusions.txt
It might be helpful to know that the size of the text file is included in the output when using the
--exclude-fromcommand line option.
In this tutorial, we learned all about the du command on Linux which is essential to master for users and administrators who like to monitor disk usage from files and directories on Linux frequently. For further reading, check out the df command, which complements du very well.