How to use the du command in Ubuntu Linux with examples

The ‘du’ command in Linux is utilized to estimate the disk space utilized by files or directories within the Linux file system. It can be especially beneficial to those using the Ubuntu Linux operating system, which is open-source and can run on various devices. It’s important to follow a specific syntax when using each command…

The du command displays disk space estimates used by files or directories on the Linux file system.

Learning how to use the du command in Ubuntu Linux can be very useful for managing disk space on your system. With du, you can quickly find out how much space is being used by a specific directory or file and how much space is being used by all files and directories on your system.

This can be helpful when you need to free up space on your disk or just want to keep track of how much space is being used by different directories and files.

Additionally, knowing how to use the du command can be helpful if you work with other Linux-based systems since the du command is a standard tool available on most Linux distributions.

When you’re ready to learn how to use the du commands, follow the guide below:

About du command:

The du command displays disk space estimates used by files or directories on the Linux file system. If you want to know the space used by a particular directory or file in Ubuntu, use the du command.

In the same way, you use your mouse and keyboard to view a file or directory size or what disk space is 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 determine 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 for 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-sizeUse the –apparent-size to print apparent sizes, rather than disk usage
-c, –totalUse the -c or –total to display the grand total
-h, –human-readableUse the -h or –human-readable to print sizes in human readable format, rounding values and using abbreviations
-S, –separate-dirsUse the -S or –separate-dirs to only display the total size of the specified directory and not subdirectories
–helpDisplay 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 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, run sudo with the du command on directories that require root access.

sudo du -sh /var

You should see an output with just the size of the /var directory and how much space it uses.

Output:
50.1G    /var

If you want to see sizes used by sub-directories inside 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 to create and extract archive files using the du command.

You may also like the commands below:

Richard Avatar

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *


Exit mobile version