Check Linux Disk Usage of Files and Directories


    The du (short for disk usage) utility emerges as a powerful tool for examining and managing the space occupied by files and directories. Whether you’re a sysadmin, developer, or casual Linux user, mastering the du command can save you valuable disk space and help you maintain a well-organized system. In this blog, we’ll take a deep dive into Linux Disk Usage using du utility, exploring its features, options, and practical use cases. The du command is a command-line tool that provides a detailed overview of disk usage for files and directories. It scans the specified directory and its subdirectories, then reports back with the space consumed by each file or directory.

    Linux Disk Usage using du – Analyzing Disk Space Consumption

    With du, you can efficiently analyze disk space consumption and make informed decisions:

    • Identify large files or directories: Sort the output to find the largest space hogs.
    • Monitor disk usage over time: Use du in combination with tools like cronto track usage trends.
    • Detect unnecessary files: Spot files or directories that consume an unusual amount of space.
    • Cleanup and optimization: Find candidates for archiving, compression, or deletion.

    du command options

    The du command offers a range of options to customize its output and behavior:

    • -h : Print sizes in a human-readable format (e.g., KB, MB, GB).
    • -c : Display a grand total of disk usage at the end.
    • -a : Include individual files in the output, not just directories.
    • --max-depth : Limit the depth of subdirectories to display.
    • -x  : Exclude subdirectories on different file systems.
    • -s  :Display only the total sum of disk usage.
    • --exclude : Exclude specific files or directories from the output.

    Usage of du

    To get started, open your terminal and use the du command followed by the options and the path you want to analyze. Here are a few basic examples:

    Display Disk Usage of a Directory

    To display the disk usage of /var/log directory and its subdirectories in a human readable format, use the following command:

    [root@rocky9 ~]# du -h /var/log
    40M	/var/log/audit
    0	/var/log/private
    25M	/var/log/sssd
    18M	/var/log/aide
    6.8M	/var/log/anaconda
    0	/var/log/samba/old
    0	/var/log/samba
    0	/var/log/nginx
    84K	/var/log/mongodb
    0	/var/log/chrony
    158M	/var/log


    Display Disk Usage of the Current Directory

    To display the summary of the total disk usage of the current directory, we use the -s option with the du command as show below:

    du -sh /var/log
    159M	/var/log

    Display Disk Usage for all Files and Subdirectories of the current Directory

    -a, --all is used to write counts for all files, not just directories.

    du -ah /var/log
    8.1M	/var/log/audit/audit.log.4
    8.1M	/var/log/audit/audit.log.3
    8.1M	/var/log/audit/audit.log.2
    8.1M	/var/log/audit/audit.log.1
    7.9M	/var/log/audit/audit.log
    40M	/var/log/audit
    0	/var/log/README
    0	/var/log/tallylog
    0	/var/log/private
    88K	/var/log/wtmp
    24K	/var/log/lastlog
    1.1M	/var/log/dnf.librepo.log.2
    1.1M	/var/log/dnf.librepo.log.1
    760K	/var/log/dnf.librepo.log
    4.0K	/var/log/hawkey.log-20230730
    21M	/var/log/cron-20230730
    0	/var/log/maillog-20230730
    240K	/var/log/messages-20230730
    12K	/var/log/secure-20230730
    0	/var/log/spooler-20230730
    128K	/var/log/messages-20230806
    28K	/var/log/secure-20230813
    158M	/var/log


    du –  set a Threshold for file size.

    When analyzing disk usage, you may only care about files of nontrivial size. You set a threshold for the file sizes you want to see with the –threshold (or -t for short) option. For example, to view only sizes larger than 1 MB:

    du  -ah --threshold=1M /var/log
    8.1M	/var/log/audit/audit.log.4
    8.1M	/var/log/audit/audit.log.3
    8.1M	/var/log/audit/audit.log.2
    8.1M	/var/log/audit/audit.log.1
    11M	/var/log/audit/audit.log
    43M	/var/log/audit
    1.2M	/var/log/sssd/
    16M	/var/log/sssd/
    18M	/var/log/sssd
    8.2M	/var/log/aide/aide.log
    8.2M	/var/log/aide/aide.log-20230820
    18M	/var/log/aide
    1.6M	/var/log/anaconda/syslog
    3.5M	/var/log/anaconda/journal.log
    6.8M	/var/log/anaconda
    11M	/var/log/cron-20230806
    1.1M	/var/log/dnf.log.3
    1.1M	/var/log/dnf.log.2
    1.1M	/var/log/dnf.log.1
    11M	/var/log/cron-20230813
    1.1M	/var/log/dnf.librepo.log.2
    1.1M	/var/log/dnf.librepo.log.1
    21M	/var/log/cron-20230730
    134M	/var/log


    Using du with —exclude=PATTERN option

    Using the --exclude=PATTERN displays the disk usage of all files and directories but excludes the files that match the given pattern.

    For the example below, the du command excludes files with .txt extension when calculating the total size of the directory.

    du -ah --exclude="*.txt" /var/log
    8.1M	/var/log/audit/audit.log.4
    8.1M	/var/log/audit/audit.log.3
    8.1M	/var/log/audit/audit.log.2
    8.1M	/var/log/audit/audit.log.1
    11M	/var/log/audit/audit.log
    43M	/var/log/audit
    0	/var/log/README
    0	/var/log/tallylog
    0	/var/log/private
    88K	/var/log/wtmp


    du – Limit the output to a particular depth of a Directory.

    To limit the output of du to a particular depth of a directory, we can use --max-depth=N option.

    du -ah --max-depth=1 /var
    123M	/var/log
    36K	/var/tmp
    4.3G	/var/lib
    0	/var/run
    0	/var/lock
    0	/var/adm
    24M	/var/cache
    0	/var/db
    0	/var/empty
    0	/var/ftp
    0	/var/games
    0	/var/local
    0	/var/mail
    0	/var/nis
    0	/var/opt
    0	/var/preserve
    12K	/var/spool
    0	/var/yp
    0	/var/kerberos
    0	/var/crash
    4.0K	/var/.updated
    4.4G	/var


    du – Sort Output in Descending Order by Size

    To sort the du output in descending order by size, we can use the sort command with du. The hr option makes the output to the in a human readable manner.

     du -ah /var/log | sort -rh | less
    125M    /var/log
    35M     /var/log/audit
    21M     /var/log/cron-20230730
    18M     /var/log/sssd
    18M     /var/log/aide
    16M     /var/log/sssd/
    11M     /var/log/cron-20230813
    11M     /var/log/cron-20230806
    8.2M    /var/log/aide/aide.log-20230820
    8.2M    /var/log/aide/aide.log
    8.1M    /var/log/audit/audit.log.4
    8.1M    /var/log/audit/audit.log.3
    8.1M    /var/log/audit/audit.log.2
    8.1M    /var/log/audit/audit.log.1
    6.8M    /var/log/anaconda
    3.5M    /var/log/anaconda/journal.log
    2.0M    /var/log/audit/audit.log
    1.6M    /var/log/anaconda/syslog



    The du utility shines as a versatile and essential tool for examining disk usage within Linux file systems. Armed with the knowledge of its usage and options, you can confidently navigate the complexities of disk space allocation, make informed decisions about resource management, and maintain a well-organized and optimized system.

    Recent Articles

    Related Articles

    Leave A Reply

    Please enter your comment!
    Please enter your name here