一天一个 Linux 命令(25):du 命令

一、简介

Linux下的du (英文全拼:disk usage)命令用于显示目录或文件所占磁盘空间的大小,但是与df命令不同的是du主要查看的目录或文件所占用的磁盘空间,而df是主要查看磁盘的空间。

二、格式说明

du [OPTION]... [FILE]...
  or:  du [OPTION]... --files0-from=F
du [选项]... [文件]...

-0, --null            end each output line with 0 byte rather than 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

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

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

三、选项说明

-a -all 显示目录中个别文件的大小
-B <块大小> 指定显示时的块大小
-b -bytes 显示目录或文件大小时,以byte为单位,相当于'--apparent-size --block-size=1'
-c --total 除了显示个别目录或文件的大小外,同时也显示所有目录或文件的总和
-D --dereference-args 显示指定符号连接的源文件大小
-d --max-depth=<目录层数>,超过指定层数的目录后,予以忽略
-h --human-readable 以K,M,G为单位,提高信息的可读性
-H --si 与-h参数相同,但是K,M,G是以1000为换算单位
-k --kilobytes 以1024 bytes为单位,即是以KB为单位显示文件大小
-l --count-links 重复计算硬件连接的文件
-L<符号连接> --dereference<符号连接> 显示选项中所指定符号连接的源文件大小
-m --megabytes 以1MB为单位,即是以MB为单位显示文件大小
-s --summarize 仅显示总计
-S --separate-dirs 显示个别目录的大小时,并不含其子目录的大小
-t --threshold=SIZE 排除小于SIZE的文件
-x --one-file-xystem 以一开始处理时的文件系统为准,若遇上其它不同的文件系统目录则略过
-X<文件> --exclude-from=<文件> 在<文件>指定目录或文件
--exclude=<目录或文件> 略过指定的目录或文件
--max-depth=<目录层数> 超过指定层数的目录后,予以忽略
--time 显示目录中文件或其子目录的最后修改时间
--time=WORD  指定显示哪种时间,atime, access, use, ctime or status
--time-style=STYLE 使用STYLE显示时,如:full-iso, long-iso, iso,或+FORMAT;格式的解释类似于'date'
--help 显示帮助
--version 显示版本信息

四、命令功能

显示目录或文件所占磁盘空间的大小

五、常见用法

5.1 显示当前目录或者文件所占磁盘空间的大小

# du 
4       ./test3/test2/test5
4       ./test3/test2/tmp
4       ./test3/test2/hello.java
4       ./test3/test2/test4
24      ./test3/test2
28      ./test3
4       ./test2/test5
4       ./test2/hello.java
4       ./test2/test4
16      ./test2
60      .

5.2 以易读方式显示当前目录或者文件所占磁盘空间的大小

# du -h 
4.0K    ./test3/test2/test5
4.0K    ./test3/test2/tmp
4.0K    ./test3/test2/hello.java
4.0K    ./test3/test2/test4
24K     ./test3/test2
28K     ./test3
4.0K    ./test2/test5
4.0K    ./test2/hello.java
4.0K    ./test2/test4
16K     ./test2
60K     .

5.3 显示所有目录或者文件的大小

0       ./test5
4.0K    ./tree.txt
0       ./test1.txt
0       ./link_test.txt
0       ./hard_link_test.txt
0       ./test3/test2.txt
4.0K    ./test3/test2/test2.txt
4.0K    ./test3/test2/test5
4.0K    ./test3/test2/tmp
4.0K    ./test3/test2/hello.java
4.0K    ./test3/test2/test4
24K     ./test3/test2
28K     ./test3
4.0K    ./test.txt
0       ./test1_link.txt
0       ./test2/test2.txt
4.0K    ./test2/test5
4.0K    ./test2/hello.java
4.0K    ./test2/test4
16K     ./test2
4.0K    ./ip.txt
60K     .

5.4 显示当前目录下及子目录所使用的空间

# du -h --max-depth=1
28K     ./test3
16K     ./test2
60K     .

注意:--max-depth 主要显示指定层数的目录

5.5 显示当前目录所占用的磁盘空间

# du -sh
60K     .

5.6 显示指定文件所占的空间

# du -h test.txt
4.0K    test.txt

5.7 显示多个文件所占空间

# du -h ip.txt  test.txt 
4.0K    ip.txt
4.0K    test.txt

5.8 显示几个文件或目录各自占用磁盘空间的大小,还统计它们的总和

# du -hc ip.txt  test.txt 
4.0K    ip.txt
4.0K    test.txt
8.0K    total

 

posted @ 2021-10-12 18:31  joshua317  阅读(281)  评论(0编辑  收藏  举报