歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> 每天一個Linux命令(29)du命令

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

日期:2017/3/3 12:22:02   编辑:Linux技術
[b] du命令是對文件和目錄磁盤使用的空間的查看。[/b]
[b] (1)用法:[/b]
[b] 用法: du [選項] [文件][/b]
[b] (2)功能:[/b]
[b] 功能: 報告磁盤空間使用情況[/b]
[b] (3)選項參數:[/b]
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)為單位輸出


[b] (4)實例:[/b]
默認是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左右
Copyright © Linux教程網 All Rights Reserved