每天一个Linux命令(29)du命令

    du命令是对文件和目录磁盘使用的空间的查看。

    (1)用法:

    用法:  du  [选项]  [文件]

    (2)功能:

    功能:  报告磁盘空间使用情况

    (3)选项参数:

      1) -a  --all               显示对所有文件的统计,而不只是包含子目录。

      2) -b  --bytes              输出以字节为单位的大小,替代缺省时1024字节的计数单位。 

      3) -h --human-readable          以K,M,G为单位,提高信息的可读性。

      4) -s --summarize           对每个参数只显示总和  

      5) --max-depth=n           只输出命令行参数的小于等于第 n 层的目录的总计。 --max-depth=0的作用同于-s选项。

      6) -m --megabytes            输出以兆字节的块为计数单位的大小(就是 1,048,576 字节)

      7) -X file --exclude-from=file       除了从指定的文件中得到模式之外与 --exclude 一样。 模式以行的形式列出。如果指定的文件是'-',那么从标准输 入中读出模式。

      8) -k --kilobytes              以KB(1024bytes)为单位输出

    (4)实例:

      默认是1024个字节为单位

      1)[root@localhost sunjimeng]# du Documents        显示目录或文件的空间使用情况

[root@localhost sunjimeng]# du Documents               //只显示目录
0    Documents/findDir/Dir/CDir
12    Documents/findDir/Dir
12    Documents/findDir
8    Documents/Pdir
8    Documents/NoPdir
28    Documents

      显示文件的空间使用情况

[root@localhost sunjimeng]# du Documents/findDir/Dir/head_text
4    Documents/findDir/Dir/head_text     

      2)[root@localhost sunjimeng]# du -a Documents      详细查看当前目录,子目录下的,所有文件和目录  

[root@localhost sunjimeng]# du -a Documents
4    Documents/findDir/Dir/head_text
4    Documents/findDir/Dir/less2
0    Documents/findDir/Dir/CDir
4    Documents/findDir/Dir/less1.gz
12    Documents/findDir/Dir
12    Documents/findDir
0    Documents/Pdir/find
4    Documents/Pdir/t3.txt
4    Documents/Pdir/vf
0    Documents/Pdir/uText
8    Documents/Pdir
0    Documents/NoPdir/find
4    Documents/NoPdir/t3.txt
4    Documents/NoPdir/vf
0    Documents/NoPdir/uText
8    Documents/NoPdir
28    Documents 

      3)[root@localhost Document]# du          默认显示当前目录的文件夹的空间使用情况

[root@localhost Document]# du
0    ./newDir
12    .
[root@localhost Document]# ll
总用量 12
-rw-r--r--. 1 root      users 85 5月  18 02:58 all.txt
-rw-rw-r--. 1 sunjimeng users  0 5月  19 22:27 B.text3
-rw-rw-r--. 1 sunjimeng users  0 5月  19 22:27 C.text6
-rw-rw-r--. 1 sunjimeng users  0 5月  19 22:28 D.text
drwxr-xr-x. 2 root      users 51 5月  18 02:47 newDir
-rw-r--r--. 1 root      users 42 5月  18 02:53 t1.txt
-rw-r--r--. 1 root      users 43 5月  18 02:54 t2.txt
[root@localhost Document]# cd ../
[root@localhost sunjimeng]# du Document
0    Document/newDir
12    Document

      4)[root@localhost sunjimeng]# du -ah Documents      以易于阅读的方式显示

[root@localhost sunjimeng]# du -ah Documents
4.0K    Documents/findDir/Dir/head_text
4.0K    Documents/findDir/Dir/less2
0    Documents/findDir/Dir/CDir
4.0K    Documents/findDir/Dir/less1.gz
12K    Documents/findDir/Dir
12K    Documents/findDir
0    Documents/Pdir/find
4.0K    Documents/Pdir/t3.txt
4.0K    Documents/Pdir/vf
0    Documents/Pdir/uText
8.0K    Documents/Pdir
0    Documents/NoPdir/find
4.0K    Documents/NoPdir/t3.txt
4.0K    Documents/NoPdir/vf
0    Documents/NoPdir/uText
8.0K    Documents/NoPdir
28K    Documents

      5)[root@localhost sunjimeng]# du -hba Documents      以一个字节为单位显示

[root@localhost sunjimeng]# du -hba Documents
664    Documents/findDir/Dir/head_text
57    Documents/findDir/Dir/less2
6    Documents/findDir/Dir/CDir
67    Documents/findDir/Dir/less1.gz
854    Documents/findDir/Dir
870    Documents/findDir
0    Documents/Pdir/find
85    Documents/Pdir/t3.txt
105    Documents/Pdir/vf
0    Documents/Pdir/uText
241    Documents/Pdir
0    Documents/NoPdir/find
85    Documents/NoPdir/t3.txt
105    Documents/NoPdir/vf
0    Documents/NoPdir/uText
241    Documents/NoPdir
1396    Documents

      6)[root@localhost sunjimeng]# du -s *          只以总数显示子文件夹的空间使用情况

[root@localhost sunjimeng]# du -s *
0    Desktop
12    Document
28    Documents
0    Downloads
0    findTextDir
0    Music
0    Pictures
0    Public
0    Templates
0    Videos
[root@localhost sunjimeng]# du -s        //默认显示当前的文件夹sunjimeng
5328    

     7)[root@localhost sunjimeng]# du -bh * |sort -n     根据目录的大小进行排序,包括目录的子目录

[root@localhost sunjimeng]# du -bh * |sort -n
1.4K    Documents
6    Desktop
6    Documents/findDir/Dir/CDir
6    Downloads
6    findTextDir
6    Music
6    Pictures
6    Public
6    Templates
6    Videos
51    Document/newDir
241    Documents/NoPdir
241    Documents/Pdir
321    Document
854    Documents/findDir/Dir
870    Documents/findDir

      8)[root@localhost /]# du -ahm --max-depth=0      以M为单位显示文件夹的大小,并且可以指定显示的深度

[root@localhost /]# du -ahm --max-depth=0                    //深度为0表示只显示当前文件夹/的大小
du: 无法访问"./proc/4599/task/4599/fd/4": 没有那个文件或目录                   //但必须将整个磁盘全部查询才知道结果
du: 无法访问"./proc/4599/task/4599/fdinfo/4": 没有那个文件或目录
du: 无法访问"./proc/4599/fd/4": 没有那个文件或目录
du: 无法访问"./proc/4599/fdinfo/4": 没有那个文件或目录
du: 无法访问"./run/user/1000/gvfs": 权限不够
3540    .
[root@localhost /]# du -ahm --max-depth=1
96    ./boot
1    ./dev
du: 无法访问"./proc/4670/task/4670/fd/4": 没有那个文件或目录
du: 无法访问"./proc/4670/task/4670/fdinfo/4": 没有那个文件或目录
du: 无法访问"./proc/4670/fd/4": 没有那个文件或目录
du: 无法访问"./proc/4670/fdinfo/4": 没有那个文件或目录
0    ./proc
du: 无法访问"./run/user/1000/gvfs": 权限不够
9    ./run
0    ./sys
28    ./etc
1    ./root
1    ./tmp
100    ./var
3304    ./usr
0    ./bin
0    ./sbin
0    ./lib
0    ./lib64
6    ./home
0    ./media
0    ./mnt
0    ./opt
0    ./srv
0    ./touch_test
0    ./touch_text
3540    .                                            //可知整个ext文件系统的空间使用情况是3540M左右
原文地址:https://www.cnblogs.com/MenAngel/p/5529606.html