How to Use the du Command to get Disk Space Used in Ubuntu 16.04 / 18.04

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…

The same way one uses a mouse and keyboard to manage files and folders or perform others simple tasks on Microsoft Windows OS, Ubuntu Linux makes that easy as well, especially for new users…

If you love Microsoft Windows and how easy it is to manage, then Ubuntu might be what you need as a beginner when starting out with Linux systems..

One thing to remember is when managing Linux system, you’ll also want to learn the commands behind the GUI interface and mouse-clicking — and how to use them…  This tutorial is going to show you how…..

This post shows new users and students what the du command is used for and how to use it…

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
                          fragmentation, indirect blocks, and the like
  -B, --block-size=SIZE  scale sizes by SIZE before printing them; e.g.,
                           '-BM' prints sizes in units of 1,048,576 bytes;
                           see SIZE format below
  -b, --bytes           equivalent to '--apparent-size --block-size=1'
  -c, --total           produce a grand total
  -D, --dereference-args  dereference only symlinks that are listed on the
                          command line
  -d, --max-depth=N     print the total for a directory (or file, with --all)
                          only if it is N or fewer levels below the command
                          line argument;  --max-depth=0 is the same as
                          --summarize
      --files0-from=F   summarize disk usage of the
                          NUL-terminated file names specified in file F;
                          if F is -, then read names from standard input
  -H                    equivalent to --dereference-args (-D)
  -h, --human-readable  print sizes in human readable format (e.g., 1K 234M 2G)
      --inodes          list inode usage information instead of block usage
  -k                    like --block-size=1K
  -L, --dereference     dereference all symbolic links
  -l, --count-links     count sizes many times if hard linked
  -m                    like --block-size=1M
  -P, --no-dereference  don't follow any symbolic links (this is the default)
  -S, --separate-dirs   for directories do not include size of subdirectories
      --si              like -h, but use powers of 1000 not 1024
  -s, --summarize       display only a total for each argument
  -t, --threshold=SIZE  exclude entries smaller than SIZE if positive,
                          or entries greater than SIZE if negative
      --time            show time of the last modification of any file in the
                          directory, or any of its subdirectories
      --time=WORD       show time as WORD instead of modification time:
                          atime, access, use, ctime or status
      --time-style=STYLE  show times using STYLE, which can be:
                            full-iso, long-iso, iso, or +FORMAT;
                            FORMAT is interpreted like in 'date'
  -X, --exclude-from=FILE  exclude files that match any pattern in FILE
      --exclude=PATTERN    exclude files that match PATTERN
  -x, --one-file-system    skip directories on different file systems
      --help     display this help and exit
      --version  output version information and exit

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:

Install SPIP CMS on Ubuntu 16.04 / 18.04 with Apache2, MariaDB and PHP 7.2

One Reply to “How to Use the du Command to get Disk Space Used in Ubuntu 16.04 / 18.04”

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.