分类:
2012-04-10 20:37:18
原文地址:linux下统计文件夹、文件的大小--du 作者:jerichen
Linux下统计文件夹大小
du -sh ./
统计文件夹占用的空间
find ./ -type f xargs ls -l awk ‘BEGIN { size=0;}{size+=$5};END{print size}’
统计所有文件的大小
du == disk usage (磁盘使用量,占用的磁盘空间)
一个文件占用的磁盘空间和一个文件的大小是两码事情。占用空间取决于文件系统的块(block)的大小,linux一般默认是4k(4096) ,因此,一个大小为1个字节的文件,最小也要
占用4k,如果你创建文件系统的时候制定块大小是16K,那么即便一个文件只有1个字节,占用空间也是 16K。
如果一个分区上主要放大文件,那么block可以大一些,有利于减少磁盘碎片,如果主要放小文件,那么block设置小一下,否则太浪费磁盘空间。
通常情况下,ls 显示的文件大小比du显示的磁盘占用空间小,比如文件系统的block是4K,一个13K的文件占用的空间是 13k/4k = 3.25 个block,一个block只能被一个文件占用
,因此实际占用空间就是4个block,就是16K。
如果一个文件有比较大的黑洞,那么会出现文件大小比磁盘空间占用大的情况。
du -s s参数是可以统计硬盘空大小的,
如 du -skh web
-k或–kilobytes 以1024 bytes为单位。
-h或–human-readable 以K,M,G为单位,提高信息的可读性
-s或–summarize 统计目录或文件
-bash-3.2$ du –help
Usage: du [OPTION]… [FILE]…
or: du [OPTION]… –files0-from=F
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 (`sparse’) 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 only symlinks that are listed on the
command line
–files0-from=F summarize disk usage of the NUL-terminated file
names specified in file F
-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
-m like –block-size=1M
-L, –dereference dereference all symbolic links
-P, –no-dereference don’t 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 file systems
-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
–time show time of the last modification of any file in the
directory, or any of its subdirectories
–time=WORD show time as WORD instead of modification time:
atime, access, use, ctime or status
–time-style=STYLE show times using style STYLE:
full-iso, long-iso, iso, +FORMAT
FORMAT is interpreted like `date’
–help display this help and exit
–version output version information and exit
SIZE may be (or may be an integer optionally followed by) one of following:
kB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.
Report bugs to
[root@dhcp ~]# tune2fs -l /dev/mapper/VolGroup00-LogVol00 |grep ‘Block size’
Block size: 4096
[root@dhcp ~]#
tune2fs – adjust tunable filesystem parameters on ext2/ext3 filesystems
大部分人都搞错了 du 的作用,du 不是显示文件大小,而是显示文件所占用的 block 大小,
你的分区的 block size 是 4k ,也就是说即使文件只有1个字节,也会占用 4KB 。
ls 默认是显示文件大小,-s 就可以达到和 du 一样的效果
测试结果如下
-bash-3.2$ touch a
-bash-3.2$ echo “1″>>a
-bash-3.2$ ll a
-rw-r–r– 1 Zianed member 2 2009-12-03 19:25 a
-bash-3.2$ ll -ks a
4 -rw-r–r– 1 Zianed member 1 2009-12-03 19:25 a
-bash-3.2$ du a
4 a
-bash-3.2$
-bash-3.2$ find ./ -type f xargs ls -l awk ‘BEGIN { size=0;}{size+=$5};END{print size}’
23107050
-bash-3.2$ du -sh ./
28M .
-bash-3.2$
References