WebMasterCampus
WEB DEVELOPER Resources

Linux du Command

Learn Linux du Command with examples


Linux du Command

In Linux, we can use “du” command is a standard Linux/Unix command that allows a user to gain disk usage information quickly.

The du command can also displays the files and directory sizes in a recursive manner.

du Command Syntax

>> du [OPTION]... [FILE]...

du Command Example

>> du

4       ./fruits
4       ./.landscape
16      ./results
4       ./test/test1/test2
8       ./test/test1
12      ./test
4       ./webmastercampus/linux/commands/ls
8       ./webmastercampus/linux/commands
12      ./webmastercampus/linux
16      ./webmastercampus
8       ./logs
168     .

du -h, –human-readable (Directory Size in Human Readable Format)

The -h flag prints size outputs, such as the ones above, in a human-readable format. This format provides a unit of measure (Bytes).

If we now run the du -h command on the same directory, we see that the 12, 36, and 48 values are in KB.

~$ du -h 

du -s, –summarize (Total Size of a Directory)

The -s flag is added to the -h flag on occasion. They allow us to get a summary of the directory’s usage in a human-readable format.

~$ du -sh

du -a, –all

du - a lists the sizes of all files and directories in the given file path.

The -a option is often combined with the -h flag for ease of use.

Notice the individual file sizes are listed with the directories.

~$ du -ah 

du –time

du –time shows the time of the last modification to any file in the directory or subdirectory that you run it against.

Let’s suppose if any user write files to a subdirectory on accident, and then we needed to find where the write took place. We could use this flag in conjunction with the -ah flags to find the directory last modified.

~$ du -ah --time

du -k

Find out the disk usage of a directory tree with its subtree in Kilobyte (displays size in 1024 bytes units) blocks.

~$ du -k /bin/

98396   /bin/

du -m

To get the summary of disk usage of directory tree along with its subtrees in Megabytes (MB) only.

The “-m” flag counts the blocks in MB units and “-h” stands for human-readable format.

~$ du -mh /bin/

97M     /bin/

du -c

du -c flag provides a grand total usage disk space at the last line.

If your directory is taken 168MB space, then the last last two lines of the output would be.

~$ du -ch

4.0K    ./fruits
4.0K    ./.landscape
16K     ./results
4.0K    ./test/test1/test2
8.0K    ./test/test1
12K     ./test
4.0K    ./webmastercampus/linux/commands/ls
8.0K    ./webmastercampus/linux/commands
12K     ./webmastercampus/linux
16K     ./webmastercampus
8.0K    ./logs
168K    .
168K    total

du -ah –exclude

~$ du -ah --exclude="*.txt" 

0       ./squash
4.0K    ./fruits
4.0K    ./test1
0       ./.landscape/sysinfo.log
4.0K    ./.landscape
0       ./.sudo_as_admin_successful
4.0K    ./results
4.0K    ./email_list_1
12K     ./.bash_history
0       ./.motd_shown
4.0K    ./today.txt.gz
4.0K    ./.profile
4.0K    ./.bash_logout
4.0K    ./test4
8.0K    ./.viminfo
4.0K    ./test3
4.0K    ./.bashrc
4.0K    ./test/test1/test2
8.0K    ./test/test1
12K     ./test
0       ./echo
4.0K    ./webmastercampus/linux/commands/ls
8.0K    ./webmastercampus/linux/commands
12K     ./webmastercampus/linux
16K     ./webmastercampus
4.0K    ./logs/logs_archieve.tar.gz
0       ./logs/log3
0       ./logs/log1
0       ./logs/log2
8.0K    ./logs
4.0K    ./test2
0       ./email_list_soft_link
108K    .

du -ha –time

Display the disk usage based on a modification of time, use the flag “–time” as shown below.

~$ du -ha --time

0       2022-04-15 01:05        ./squash
4.0K    2022-05-10 12:27        ./fruits2.txt
0       2022-04-21 10:43        ./fruits/melon.txt
4.0K    2022-04-21 10:44        ./fruits
4.0K    2022-04-27 02:59        ./test1
4.0K    2022-05-08 12:36        ./music.txt
0       2021-12-23 11:55        ./.landscape/sysinfo.log
4.0K    2021-12-23 11:55        ./.landscape
0       2022-04-05 15:15        ./.sudo_as_admin_successful
4.0K    2022-04-27 01:02        ./file_copy.txt
4.0K    2022-04-25 22:38        ./results/result3.txt
4.0K    2022-04-25 22:38        ./results/result2.txt
4.0K    2022-04-25 22:38        ./results/result1.txt
16K     2022-04-26 00:55        ./results
4.0K    2022-04-25 02:47        ./email_list_1
4.0K    2022-05-09 16:36        ./list2.txt
12K     2022-05-08 14:00        ./.bash_history
4.0K    2022-05-10 12:28        ./fruits.txt
4.0K    2022-05-10 11:11        ./cities1.txt
4.0K    2022-05-09 15:11        ./list1.txt
4.0K    2022-05-08 14:09        ./music2.txt
0       2022-05-08 14:00        ./.motd_shown
4.0K    2022-05-10 18:41        ./myLinuxPractice.txt
4.0K    2022-04-22 15:24        ./today.txt.gz
4.0K    2021-12-23 11:54        ./.profile
4.0K    2021-12-23 11:54        ./.bash_logout
4.0K    2022-04-27 03:00        ./test4
8.0K    2022-05-10 12:29        ./.viminfo
4.0K    2022-04-27 03:00        ./test3
4.0K    2021-12-23 11:54        ./.bashrc
4.0K    2022-04-10 02:11        ./test/test1/test2
8.0K    2022-04-10 02:11        ./test/test1
12K     2022-04-10 02:11        ./test
4.0K    2022-04-15 01:12        ./output.txt
4.0K    2022-05-10 11:12        ./cities2.txt
0       2022-05-10 17:25        ./echo
4.0K    2022-04-13 02:35        ./webmastercampus/linux/commands/ls
8.0K    2022-04-13 02:35        ./webmastercampus/linux/commands
12K     2022-04-13 02:35        ./webmastercampus/linux
16K     2022-04-13 02:35        ./webmastercampus
4.0K    2022-04-26 01:49        ./logs/logs_archieve.tar.gz
0       2022-04-26 01:48        ./logs/log3
0       2022-04-26 01:48        ./logs/log1
0       2022-04-26 01:48        ./logs/log2
8.0K    2022-04-26 01:49        ./logs
4.0K    2022-04-27 03:00        ./test2
4.0K    2022-05-08 14:22        ./music3.txt
0       2022-04-25 02:47        ./email_list_soft_link
168K    2022-05-10 18:41        .

du Command in Linux (Documentation)

~$ man du

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 directo‐
       ries.

       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 ap‐
              parent 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 speci‐
              fied 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 direc‐
              tory, 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

       Display values  are  in  units  of  the  first  available  SIZE  from
        --block-size,  and  the DU_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE envi‐
       ronment variables.  Otherwise, units default to 1024 bytes (or 512 if
       POSIXLY_CORRECT is set).

       The  SIZE  argument  is an integer and optional unit (example: 10K is
       10*1024).  Units are K,M,G,T,P,E,Z,Y (powers of  1024)  or  KB,MB,...
       (powers of 1000).

PATTERNS
       PATTERN is a shell pattern (not a regular expression).  The pattern ?
       matches any one character, whereas * matches any string (composed  of
       zero,  one  or multiple characters).  For example, *.o will match any
       files whose names end in .o.  Therefore, the command

              du --exclude='*.o'

       will skip all files and subdirectories ending in  .o  (including  the
       file .o itself).
Created with love and passion.