du命令示例

Linux中的du命令也是查看使用空间的,与df命令不同的是,Linux du命令是对文件和目录磁盘使用的空间的查看,所以它和df命令还是有一些区别的。

1.命令格式

du [选项][文件]

2.命令功能

显示每个文件和目录的磁盘使用空间。

3.命令参数

  • -a-all 显示目录中个别文件的大小。
  • -b-bytes 显示目录或文件大小时,以byte为单位。
  • -c--total 除了显示个别目录或文件的大小外,同时也显示所有目录或文件的总和。
  • -k--kilobytesKB(1024bytes)为单位输出。
  • -m--megabytesMB为单位输出。
  • -s--summarize 仅显示总计,只列出最后加总的值。
  • -h--human-readableKMG为单位,提高信息的可读性。
  • -x--one-file-xystem 以一开始处理时的文件系统为准,若遇上其它不同的文件系统目录则略过。
  • -L<符号链接>或--dereference<符号链接> 显示选项中所指定符号链接的源文件大小。
  • -S--separate-dirs 显示个别目录的大小时,并不含其子目录的大小。
  • -X<文件>或--exclude-from=<文件> 在<文件>指定目录或文件。
  • --exclude=<目录或文件> 略过指定的目录或文件。
  • -D--dereference-args 显示指定符号链接的源文件大小。
  • -H--si-h参数相同,但是KMG是以1000为换算单位。
  • -l--count-links 重复计算硬件链接的文件。

4.使用实例

实例1:显示目录或者文件所占空间

命令:

du

输出:

[zyiz@localhost ~]$ du
     ./test4
      ./project/lib
      ./project/bin
      ./project/doc/info
      ./project/doc/product
      ./project/doc
      ./project/logs/info
      ./project/logs/product
      ./project/logs
      ./project/service/deploy/info
      ./project/service/deploy/product
      ./project/service/deploy
      ./project/service
      ./project
      ./logs/test3
      ./logs
      ./scripts
     .
[zyiz@localhost ~]$

说明:
只显示当前目录下面的子目录的目录大小和当前目录的总的大小,最下面的1288为当前目录的总大小

实例2:显示指定文件所占空间

命令:

du log3.log

输出:

[zyiz@localhost ~]$ du log3.log
    log3.log
[zyiz@localhost ~]$

实例3:查看指定目录的所占空间

命令:

du /usr/local

输出:

[zyiz@localhost ~]$ du /usr/local/
      /usr/local/bin
      /usr/local/etc
      /usr/local/games
      /usr/local/include
      /usr/local/lib
      /usr/local/lib64
      /usr/local/libexec
      /usr/local/sbin
      /usr/local/share/applications
      /usr/local/share/info
      /usr/local/share/man/man1
      /usr/local/share/man/man1x
      /usr/local/share/man/man2
      /usr/local/share/man/man2x
      /usr/local/share/man/man3
      /usr/local/share/man/man3x
      /usr/local/share/man/man4
      /usr/local/share/man/man4x
      /usr/local/share/man/man5
      /usr/local/share/man/man5x
      /usr/local/share/man/man6
      /usr/local/share/man/man6x
      /usr/local/share/man/man7
      /usr/local/share/man/man7x
      /usr/local/share/man/man8
      /usr/local/share/man/man8x
      /usr/local/share/man/man9
      /usr/local/share/man/man9x
      /usr/local/share/man/mann
      /usr/local/share/man
      /usr/local/share
 /usr/local/src
 /usr/local/
[zyiz@localhost ~]$

实例4:显示多个文件所占空间

命令:

du file1 file2

输出:

[zyiz@localhost test4]$ du log.tar log3.log
     log.tar
    log3.log
[zyiz@localhost test4]$

实例5:只显示总和的大小

命令:

du -s

输出:

[zyiz@localhost ~]$ du -s
   .
[zyiz@localhost ~]$ du -s project/
      project/
[zyiz@localhost ~]$ du -s test4/
    test4/
[zyiz@localhost ~]$

实例6:方便阅读的格式显示

命令:

du -h dir

输出:

[zyiz@localhost ~]$ du -h project/
      project/lib
      project/bin
      project/doc/info
      project/doc/product
40K     project/doc
      project/logs/info
      project/logs/product
152K    project/logs
      project/service/deploy/info
      project/service/deploy/product
      project/service/deploy
      project/service
192K    project/
[zyiz@localhost ~]$

实例7:文件和目录都显示

命令执行和输出:

[zyiz@localhost ~]$ du -ah project/
      project/lib
      project/bin/run.py
      project/bin
      project/doc/info
      project/doc/product
40K     project/doc/test.txt
40K     project/doc
      project/logs/info
      project/logs/product
148K    project/logs/readme.txt
4.0K    project/logs/log201710.log
152K    project/logs
      project/service/deploy/info
      project/service/deploy/product
      project/service/deploy
      project/service
192K    project/
[zyiz@localhost ~]$

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

命令:

du -c log3.log log1.log text.txt

输出:

[zyiz@localhost ~]$ ll
total 572
-rwxr-xr-x. 1 zyiz zyiz    464 Feb 15 20:58 log1.log
-rw-rw-r--. 1 zyiz zyiz      0 Feb 15 20:43 log2018.log
-rw-rw-r--. 1 zyiz zyiz      0 Feb 15 20:43 log2019.log
-rw-rw-r--. 1 zyiz zyiz      0 Feb 15 20:43 log2020.log
-rwxrwxr-x. 1 zyiz zyiz      0 Feb 15 20:43 log2.log
-rw-rw-r--. 1 zyiz zyiz 571759 Feb 17 04:02 log3.log
lrwxrwxrwx. 1 zyiz zyiz      7 Feb 15 22:46 log_link.log -> log.log
-rwxr-xr-x. 1 zyiz zyiz      0 Feb 16 21:38 log.log
drwxrwxr-x. 3 zyiz zyiz     35 Feb 16 20:54 logs
drwxrwxr-x. 7 zyiz zyiz     66 Feb 15 20:48 project
-rw-rw-r--. 1 zyiz zyiz      0 Feb 17 02:01 run.py
drwxrwxr-x. 2 root   zyiz     67 Feb 17 02:38 scripts
-rw-rw-r--. 1 zyiz zyiz      0 Feb 15 21:10 test3.log
drwxrwxr-x. 2 zyiz zyiz    177 Feb 16 21:28 test4
-rw-rw-r--. 1 zyiz zyiz     19 Feb 15 21:08 text.txt
-rw-rw-r--. 1 zyiz zyiz      1 Feb 15 20:46 tmpcore.log
[zyiz@localhost ~]$ du -c log3.log log1.log text.txt
    log3.log
      log1.log
      text.txt
    total
[zyiz@localhost ~]$

说明:
加上-c选项后,du不仅显示两个目录各自占用磁盘空间的大小,还在最后一行统计它们的总和。

实例9:按照空间大小排序

命令:

du|sort -nr|more

输出:

[zyiz@localhost ~]$ du|sort -nr|more
   .
    ./test4
    ./project
    ./project/logs
     ./project/doc
      ./scripts
      ./logs/test3
      ./logs
      ./project/service/deploy/product
      ./project/service/deploy/info
      ./project/service/deploy
      ./project/service
      ./project/logs/product
      ./project/logs/info
      ./project/lib
      ./project/doc/product
      ./project/doc/info
      ./project/bin
[zyiz@localhost ~]$

实例10:输出当前目录下各个子目录所使用的空间

命令:

du -h  --max-depth=1

输出:

[zyiz@localhost ~]$ ll
total 572
-rwxr-xr-x. 1 zyiz zyiz    464 Feb 15 20:58 log1.log
-rw-rw-r--. 1 zyiz zyiz      0 Feb 15 20:43 log2018.log
-rw-rw-r--. 1 zyiz zyiz      0 Feb 15 20:43 log2019.log
-rw-rw-r--. 1 zyiz zyiz      0 Feb 15 20:43 log2020.log
-rwxrwxr-x. 1 zyiz zyiz      0 Feb 15 20:43 log2.log
-rw-rw-r--. 1 zyiz zyiz 571759 Feb 17 04:02 log3.log
lrwxrwxrwx. 1 zyiz zyiz      7 Feb 15 22:46 log_link.log -> log.log
-rwxr-xr-x. 1 zyiz zyiz      0 Feb 17 04:16 log.log
drwxrwxr-x. 3 zyiz zyiz     35 Feb 16 20:54 logs
drwxrwxr-x. 7 zyiz zyiz     66 Feb 15 20:48 project
-rw-rw-r--. 1 zyiz zyiz      0 Feb 17 02:01 run.py
drwxrwxr-x. 2 root   zyiz     67 Feb 17 02:38 scripts
-rw-rw-r--. 1 zyiz zyiz      0 Feb 15 21:10 test3.log
drwxrwxr-x. 2 zyiz zyiz    177 Feb 16 21:28 test4
-rw-rw-r--. 1 zyiz zyiz     19 Feb 15 21:08 text.txt
-rw-rw-r--. 1 zyiz zyiz      1 Feb 15 20:46 tmpcore.log
[zyiz@localhost ~]$ du -h  --max-depth=1
468K    ./test4
192K    ./project
4.0K    ./logs
4.0K    ./scripts
1.3M    .
[zyiz@localhost ~]$

上一篇:df命令示例

下一篇:top命令详解示例

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程