首页 > Linux操作系统 > Linux操作系统 > Linux学习笔记3--du命令
du - estimate file space usage
SYNOPSIS
du [OPTION]
[FILE]
DESCRIPTION
Summarize disk usage of each FILE, recursively for directories.
Mandatory arguments to long options are mandatory for short options
too.
-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 (鈥榮parse鈥? files, internal fragmentation, indirect blocks,
and the like
-B, --block-size=SIZE use SIZE-byte blocks
-b, --bytes
equivalent to 鈥?-apparent-size --block-size=1鈥?
-c, --total
produce a grand total
-D, --dereference-args
dereference FILEs that are symbolic links
-H like --si, but also evokes a warning; will soon change to be
equivalent to --dereference-args (-D)
-h, --human-readable
print sizes in human readable format (e.g., 1K 234M 2G)
--si like -h, but use powers of 1000 not 1024
-k like --block-size=1K
-l, --count-links
count sizes many times if hard linked
-L, --dereference
dereference all symbolic links
-P, --no-dereference
don鈥檛 follow any symbolic links (this is the default)
-0, --null
end each output line with 0 byte rather than newline
-S, --separate-dirs
do not include size of subdirectories
-s, --summarize
display only a total for each argument
-x, --one-file-system
skip directories on different filesystems
-X FILE, --exclude-from=FILE
Exclude files that match any pattern in FILE.
--exclude=PATTERN Exclude files that match PATTERN.
--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
[root@CentOS4 ~]# cd ok_008 --转到OK_008目录
[root@CentOS4 ok_008]# du -sh --统计OK_008目录的文件大小
32K .
[root@CentOS4 ok_008]# du -sH --统计OK_008目录的文件大小,注意这里的H大写,与上一条命令不同。
du: WARNING: use --si, not -H; the meaning of the -H option will soon
change to be the same as that of --dereference-args (-D)
33k . --由于统计单位的不同,所以显示的大小不一样,其实物理都是都是一样。
[root@CentOS4 ok_008]# du -Sh --统计当前各子目录大小。
8.0K ./ok/008
8.0K ./ok
16K . --细心发现这里是16K,而上面使用du -sh显示的是32k,那么其中的16k哪里去了呢。
[root@CentOS4 ok_008]# cd ..
[root@CentOS4 ~]# du -Sh
16K ./.gstreamer-0.8
8.0K ./.gnome/mime-info
8.0K ./.gnome/application-info
8.0K ./.gnome
52K ./.gconfd

8.0K ./ok_008/ok/008 --这里我们可以发现,原来/ok_008目录就占了16k的大小。
8.0K ./ok_008/ok --由此我们可以区别命令du -Sh 和du -sH的不同之处。
16K ./ok_008
..
--为了更方便的验证命令du -Sh 和du -sH的不同,我们可以这样:
命令du -Sh 和du -sH
[root@CentOS4 ~]# cd ok_008 --除了显示ok_008目录或文件的大小外,同时也显示所有子目录或文件的总和。
[root@CentOS4 ok_008]# du -ch
8.0K ./ok/008
16K ./ok
32K .
32K total --其实就是如数学中的数的累加一样,统计目录或文件的大小。
[root@CentOS4 ok_008]# du -cb --这是以字节为单元大小表示。
4096 ./ok/008
8192 ./ok
12301 .
12301 total
[root@CentOS4 ok_008]# du -ck --这命令与du -ch有点相似点,这里只不过少了单位k。
8 ./ok/008
16 ./ok
32 .
32 total
--为了更好的验证命令du -ck 和命令du -ch的不同,可以使用测试:
[root@CentOS4 ~]# du -ck
16 ./.gstreamer-0.8
8 ./.gnome/mime-info
8 ./.gnome/application-info

24 ./.thumbnails/fail
32 ./.thumbnails
8 ./ok_008/ok/008
16 ./ok_008/ok
32 ./ok_008
1116 .
1116 total
[root@CentOS4 ~]# du -ch
16K ./.gstreamer-0.8
8.0K ./.gnome/mime-info
8.0K ./.gnome/application-info

24K ./.thumbnails/fail
32K ./.thumbnails
8.0K ./ok_008/ok/008
16K ./ok_008/ok
32K ./ok_008
1.1M .
1.1M total
--不难看出命令du -ck 是以k单位来统计,而du -ch在统计过程中自动根据实际以K,M,G为单位,达到提高信息的可读性。

[root@CentOS4 ok_008]# du -h mytest --查看文件mytest的大小
8.0K mytest
[root@CentOS4 ok_008]# du -h /ok --查看ok目录包含ok所有子目录的大小
8.0K /ok/008
16K /ok
怎么样查多的文件大大小呢?
现在我们使用vi建立一新的文件叫oktest
[root@CentOS4 ok_008]# vi oktest
随便输入一些数据然后保存oktest文件。
[root@CentOS4 ok_008]# ls -l
total 24
-rw-r--r-- 1 root root 13 Sep 13 22:36 mytest
drwxr-xr-x 3 root root 4096 Sep 13 22:51 ok
-rw-r--r-- 1 root root 21 Sep 17 20:45 oktest
现在我们要查询文件mytest和oktest的大小。可以这样:
[root@CentOS4 ok_008]# du -h mytest oktest
8.0K mytest
8.0K oktest
我们还可以同时查文件与目录的大小,如:
[root@CentOS4 ok_008]# du -h mytest /ok
8.0K mytest --文件
8.0K /ok/008 --目录
16K /ok --目录

另外:
[root@CentOS4 ok_008]# du -h ./
8.0K ./ok/008
16K ./ok
40K ./
[root@CentOS4 ok_008]# du -h
8.0K ./ok/008
16K ./ok
40K .
这两个命令等效。习惯du -h 这样写法。



目前还不知道具体怎么样使用。当然这一参数不影响学习的进程,到以后用到的时候,再回头了解
也不迟。来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/7764484/viewspace-982562/ ,如需转载,请注明出处,否则将追究法律责任。