Linux command (35) du command

du command

  • Function Description: display the size of file or directory

    The du command displays the size of the disk space occupied by the specified directory or file, without any options. The default unit is 1KB.

  • Usage: Du [option] [file]

  • option

    option explain
    -a Recursively display the disk space occupied by each file and subdirectory in the specified directory
    -s Display only the total disk space occupied by the specified directory or file
    -h Display size in a human readable way
    -d, --max-depth=N Displays the size of the specified N-tier subdirectory
    -X < File >, - exclude from = < File > Ignore directory or file specified in file when displaying
    --exclude=PATTERN Ignore the specified directory or file when displaying

Example 1: du command without any options displays the disk space occupied by the current directory, excluding files

[root@study ~]# du 
12      ./.local/share/lftp
12      ./.local/share
12      ./.local
0       ./.config/lftp
0       ./.config
48      ./nginx-1.16.1/auto/cc
4       ./nginx-1.16.1/auto/lib/geoip
4       ./nginx-1.16.1/auto/lib/google-perftools
8       ./nginx-1.16.1/auto/lib/libatomic
4       ./nginx-1.16.1/auto/lib/libgd
...
7032    ./nginx-1.16.1
8100    .

Output result description:

  • First column: displays the disk space size of the current directory in blocks
  • Second column: displays the directory name in the directory
  • Last line: displays the total amount of disk space occupied by the current directory

Example 2: use du-a to recursively display the disk space occupied by each file and subdirectory in the directory

[root@study ~]# du -a
4       ./.bash_logout
4       ./.bash_profile
4       ./.bashrc
4       ./.cshrc
4       ./.tcshrc
4       ./original-ks.cfg
4       ./anaconda-ks.cfg
1012    ./nginx-1.16.1.tar.gz
4       ./nginx-1.16.1/auto/cc/acc
8100    .

Example 3: use du-h to display the amount of disk space occupied by / etc in a human readable way

[root@study ~]# du -h /etc/|less
72K     /etc/grub.d
0       /etc/terminfo
12K     /etc/skel
0       /etc/alternatives
0       /etc/chkconfig.d
40K     /etc/rc.d/init.d
44K     /etc/rc.d
0       /etc/security/console.apps
0       /etc/security/console.perms.d
4.0K    /etc/security/limits.d
0       /etc/security/namespace.d
60K     /etc/security
4.0K    /etc/depmod.d
0       /etc/dracut.conf.d
12K     /etc/default
12K     /etc/prelink.conf.d
12K     /etc/modprobe.d
4.0K    /etc/rsyslog.d
31M     /etc/

Example 4: use du -s to display the total size of the current directory

[root@node1 ~]# du -s /etc/
31564   /etc/

Example 5: using du -sh to display the total size of the directory (in a human readable way)

[root@node1 ~]# du -sh /etc/
31M     /etc/

Example 6: use du -d to display the size of a directory at a specified level

[root@node1 ~]# du -h -d1 /usr/
64M     /usr/bin
40M     /usr/sbin
455M    /usr/lib
159M    /usr/lib64
284M    /usr/share
0       /usr/etc
0       /usr/games
36K     /usr/include
12M     /usr/libexec
0       /usr/local
0       /usr/src
1011M   /usr/
# Show only the size of the directory below / usr directory

Example 7: ignore the specified directory when displaying the directory, create four directories a, b, c and d, and ignore directory b when displaying

[root@node1 tmp]# mkdir {a..d}
[root@node1 tmp]# ls
a  b  c  d
[root@node1 tmp]# du -h --exclude=b
0       ./.ICE-unix
0       ./.font-unix
0       ./.XIM-unix
0       ./.Test-unix
0       ./.X11-unix
0       ./a
0       ./c
0       ./d
0       .

Tags: Linux Nginx Unix Google Anaconda

Posted on Wed, 03 Jun 2020 10:37:51 -0400 by nirali35