How to Use the Du Command in Linux

how to use the du command in Linux

In this tutorial, we are going to explain the “du” command used on every Linux distribution such as Ubuntu, Debian, or CentOS.

“Du” stands for Disk Usage and every Linux user should use it very often to check the amount of disk space used by directory or file. Using the “du” command is very simple by typing it on the console and adding additional phrases called options. In other words, the “du” command is different every time, according to the needs of the user and what the user wants to be displayed as output.

In this blog post, the “du” command will be explained with real examples. Let’s get started!

Prerequisites

  • Fresh install of Ubuntu, CentoOS, Debian or AlmaLinux
  • User privileges: root or non-root user with sudo privileges

Du Command Syntax

This is the general syntax of the du command:

du [Options] [Directory Name or File Name]

The first word of the syntax will always be “du”, the second word will be for arguments like -h (human-readable output), -l (count links), -b (bytes) and etc. The third word is for specifying the name of the directory or file that the user wants to get information for. The second and the third words do not need to be specified, but the output of the “du” command will not be readable or understandable at all.

Executing the Du Command

Execute the “du” command without any arguments for example in the /root directory on your server.

du

You should get the following output.

.

root@vps:~# du
4       ./.config/procps
4       ./.config/mc/mcedit
12      ./.config/mc
20      ./.config
8       ./.nano
4       ./.gnupg/private-keys-v1.d
8       ./.gnupg
4       ./.local/share/mc/mcedit
16      ./.local/share/mc
20      ./.local/share
24      ./.local
8       ./.ssh
4       ./.cache/mc/mcedit
12      ./.cache/mc
16      ./.cache
120     .

As you can see the output is not very understandable, and there is no information about the given numbers if they are in Kilobytes, Megabytes, or Gigabytes

Execute the du command along with the “-h” argument.

du -h

Now, the output will be different:

root@host:~# du -h
4.0K    ./.config/procps
4.0K    ./.config/mc/mcedit
12K     ./.config/mc
20K     ./.config
8.0K    ./.nano
4.0K    ./.gnupg/private-keys-v1.d
8.0K    ./.gnupg
4.0K    ./.local/share/mc/mcedit
16K     ./.local/share/mc
20K     ./.local/share
24K     ./.local
8.0K    ./.ssh
4.0K    ./.cache/mc/mcedit
12K     ./.cache/mc
16K     ./.cache
120K    .

As you can see now, the directories and files are displayed with their sizes in Kilobytes (In this example there were no files or folders in Megabytes).

Display Directory Size

In this example, we are going to use the default WordPress installation directory. Before you check the size we assume that you have downloaded it from their official website.

wget https://wordpress.org/latest.zip
unzip latest.zip

To check the size of the WordPress directory, execute the command below:

du -csxh /wordpress

You should get the following output:

root@vps:~# du -csxh wordpress/
64M     wordpress/
64M     total

This command is very useful, the meaning of the arguments are described below:

-c, --total
              produce a grand total
     -s, --summarize
              display only a total for each argument
     -x, --one-file-system
              skip directories on different file systems
     -h, --human-readable
              print sizes in human readable format (e.g., 1K 234M 2G)

You can also use the additional “-k” or “-m” flags to display the directory in Kilobytes or Megabytes.

root@vps:~# du -csxh -k wordpress/
65296   wordpress/
65296   total
root@host:~# du -csxh -m wordpress/
64      wordpress/
64      total

To check the sizes of all directories and files inside the WordPress directory execute the command below:

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
cd /wordpress

du -bsh *

You should receive the following output:

root@vps:~/wordpress# du -bsh *
405     index.php
20K     license.txt
7.3K    readme.html
7.0K    wp-activate.php
8.4M    wp-admin
351     wp-blog-header.php
2.3K    wp-comments-post.php
3.0K    wp-config-sample.php
9.7M    wp-content
3.9K    wp-cron.php
40M     wp-includes
2.5K    wp-links-opml.php
3.9K    wp-load.php
47K     wp-login.php
8.4K    wp-mail.php
23K     wp-settings.php
32K     wp-signup.php
4.7K    wp-trackback.php
3.2K    xmlrpc.php

This is the explanation of the command above:

-b, --bytes
              equivalent to '--apparent-size --block-size=1'
     -s, --summarize
              display only a total for each argument
     -h, --human-readable
              print sizes in human readable format (e.g., 1K 234M 2G)

More About Du Command

The previous paragraphs were explained with examples of the most used “du” commands in one regular day of one Linux System Administrator. If you want to learn more about the “du” command you can always use the user manual of the “du” command.

man du

The list of the options will be long since the “du” command has plenty of arguments for use.

root@host:~# man du
DU(1)                                                                      User Commands                                                                      DU(1)

NAME
       du - estimate file space usage

SYNOPSIS
       du [OPTION]... [FILE]...
       du [OPTION]... --files0-from=F

DESCRIPTION
       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, in‐
              ternal fragmentation, indirect blocks, and the like
                          .
                          .
                          .
                          .
   Manual page du(1) line 3 (press h for help or q to quit)

That’s it! You learned how to use the Du Command in Linux-based distributions such as Ubuntu, CentOS, Debian and etc.

Of course, you don’t have to know everything about the Du Command and its usage. If you use one of our NVMe VPS Hosting plans, in which case you can simply ask our expert Linux admins to display some information about some directory on your server for you. They are available 24×7 and will take care of your request immediately.

If you liked this post on how to use the Du Command in Linux, please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

1 thought on “How to Use the Du Command in Linux”

  1. To recursively check the disk usage of the specified directory and its subdirectories, use the -a option. This will provide a comprehensive breakdown of storage usage across the directory hierarchy. Your blog is a great resource, and I’m thankful for it.

    Reply

Leave a Comment