The du command displays estimates of disk space used by files or directories on Linux file system.
If you’re a new user or student planning on learning how to use and manage Linux systems, the easiest place to start might be on Ubuntu Linux OS.
Ubuntu is an open-source Linux operating systems that runs on desktops, laptops, server and other devices.
When you’re ready to learn how to use the du commands, follow the guide below:
About du command:
The du command displays estimates of disk space used by files or directories on Linux file system. If you want to know the space used by a particular directory or file in Ubuntu, use the du command.
The same way you use your mouse and keyboard to view a file or directory size or what disk space being used, du is the way to do it on the command line.
Syntax:
The syntax is the rule and format of how the du command can be used. These syntax options can be reordered, but a straight format must be followed.,.
Below is an example syntax of how to use the du command.
du [OPTION.] [FILE].
Options:
The command line options are switches or flags that determined how the commands are executed or controlled. they modify the behavior of the commands. they are separated by spaces and followed after the commands.
Below are some options of the du command:
FILES. | Replace FILES.. with the files or directories when using the du command. |
-a, –all |
Use the -a or –all to display counts for all files and not just directories |
–apparent-size | Use the –apparent-size to print apparent sizes, rather than disk usage |
-c, –total | Use the -c or –total to display the grand total |
-h, –human-readable | Use the -h or –human-readable to print sizes in human readable format, rounding values and using abbreviations |
-S, –separate-dirs | Use the -S or –separate-dirs to only display the total size of the specified directory and not subdirectories |
–help | Display a help message and exit. |
Examples:
Below are some examples of how to run and use the du on Ubuntu Linux.
If you want to view the /var directory size or how much space the /var directory is using on the disk, you run the commands below:
du /var
If you want a human readable format and to make sense of the data, you will want to run the du command with the -sh options. example:
du -sh /var
If you’re not logged in as root, then run sudo with the du command on directories that requires root access.
sudo du -sh /var
You should see an output with just the size of the /var directory and how much space being used by it.
Output: 50.1G /var
If you want to see sizes used by sub-directories inside of the /var directory. you can run the commands below:
sudo du -sh /var/*
Or use the –max-depth=1 command option.
sudo du -h --max-depth=1 /var
That should display similar lines as below:
Output: 4.0K /var/mail 4.0K /var/local 59M /var/log 127M /var/cache 44K /var/tmp 1.5G /var/lib 4.0K /var/opt 4.0K /var/metrics 168K /var/snap 40K /var/spool 4.0K /var/crash 3.3M /var/backups 1.7G /var
You can also combine the du commands with other commands and pipes and display more accurate information. For example, to print out the 3 biggest directories in the /var directory. run the commands below:
sudo du -h /var | sort -rh | head -3
It should output something similar to the lines below:
Output: 1.7G /var 1.5G /var/lib 1.2G /var/lib/snapd
When you run du with the –help option, you’ll see the help text below:
Usage: du [OPTION]. [FILE]. or: du [OPTION]. --files0-from=F Summarize disk usage of the set of FILEs, recursively for directories. Mandatory arguments to long options are mandatory for short options too. -0, --null end each output line with NUL, not newline -a, --all write counts for all files, not just directories --apparent-size print apparent sizes, rather than disk usage; although the apparent size is usually smaller, it may be larger due to holes in ('sparse') files, internal
That’s it!
Congratulations! You’ve learned how to use the du command to create and extract archives files.
You may also like the commands below: