Linux学习笔记3--du命令

先man du查看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
中文解释

du(disk   usage)      
  功能说明:显示目录或文件的大小。 
   
  语  法:
du   [-abcDhHklmsSx][-L   <符号连接>][-X   <文件>][--block-size][--exclude=<目录或文件>][--max-depth=<目录层数>][--help][--version][目录或文件]  
   
  补充说明:
du会显示指定的目录或文件所占用的磁盘空间。
     -a或-all          显示目录中个别文件的大小。  
      -b或-bytes         显示目录或文件大小时,以byte为单位。  
      -c或--total        除了显示个别目录或文件的大小外,同时也显示所有目录或文件的总和。  
      -D或--dereference-args   显示指定符号连接的源文件大小。  
      -h或--human-readable    以K,M,G为单位,提高信息的可读性。  
      -H或--si        与-h参数相同,但是K,M,G是以1000为换算单位。  
      -k或--kilobytes   以1024   bytes为单位。  
      -l或--count-links  重复计算硬件连接的文件。  
      -L<符号连接>或--dereference<符号连接>      
    显示选项中所指定符号连接的源文件大小。  
      -m或--megabytes        以1MB为单位。  
      -s或--summarize        仅显示总计。  
      -S或--separate-dirs  显示个别目录的大小时,并不含其子目录的大小。  
      -x或--one-file-xystem     以一开始处理时的文件系统为准,若遇上其它不同的文件系统目录则略过。  
      -X<文件>或--exclude-from=<文件> 在<文件>指定目录或文件。  
      --exclude=<目录或文件>        略过指定的目录或文件。  

操作过程:
[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 这样写法。


    

在学习过程中感觉参数-D或--dereference-args显示指定符号连接的源文件大小,
目前还不知道具体怎么样使用。当然这一参数不影响学习的进程,到以后用到的时候,再回头了解
也不迟。


好了,今天就学到这里,同事要休息,不能边开音响边学习,也该休息了,闪了,做个好梦!~-~
2007-9-17
posted @ 2007-09-18 09:17  ok_008  阅读(2261)  评论(0编辑  收藏  举报
给我写信