Centos下 du命令的使用,查看某个文件或目录占用磁盘空间的大小
一、du命令介绍
du
reports the amount of disk space used by the specified files and for each subdirectory (of directory arguments). with no arguments,du
reports the disk space for the current directory。
du命令用来查看目录或文件所占用磁盘空间的大小。常用选项组合为:du -sh
du 意思是 disk usage
二、du常用的选项
[root@localhost ftp_data]# du --help
用法:du [选项]... [文件]...
或:du [选项]... --files0-from=F
Summarize disk usage of each FILE, recursively for directories.
Mandatory arguments to long options are mandatory for short options too.
-0, --null end each output line with 0 byte rather than newline
-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 scale sizes by SIZE before printing them; e.g.,
'-BM' prints sizes in units of 1,048,576 bytes;
see SIZE format below
-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
-d, --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
--files0-from=F summarize disk usage of the
NUL-terminated file names specified in file F;
if F is -, then read names from standard input
-H equivalent to --dereference-args (-D)
-h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)
--inodes list inode usage information instead of block usage
-k like --block-size=1K
-L, --dereference dereference all symbolic links
-l, --count-links count sizes many times if hard linked
-m like --block-size=1M
-P, --no-dereference don't follow any symbolic links (this is the default)
-S, --separate-dirs for directories do not include size of subdirectories
--si like -h, but use powers of 1000 not 1024
-s, --summarize display only a total for each argument
-t, --threshold=SIZE exclude entries smaller than SIZE if positive,
or entries greater than SIZE if negative
--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, which can be:
full-iso, long-iso, iso, or +FORMAT;
FORMAT is interpreted like in 'date'
-X, --exclude-from=FILE exclude files that match any pattern in FILE
--exclude=PATTERN exclude files that match PATTERN
-x, --one-file-system skip directories on different file systems
--help 显示此帮助信息并退出
--version 显示版本信息并退出
所显示的数值是来自 --block-size、DU_BLOCK_SIZE、BLOCK_SIZE
及 BLOCKSIZE 环境变量中第一个可用的 SIZE 单位。
否则,默认单位是 1024 字节(或是 512,若设定 POSIXLY_CORRECT 的话)。
SIZE is an integer and optional unit (example: 10M is 10*1024*1024). Units
are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (powers of 1000).
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告du 的翻译错误
要获取完整文档,请运行:info coreutils 'du invocation'
-h:以人类可读的方式显示
-a:显示目录占用的磁盘空间大小,还要显示其下目录和文件占用磁盘空间的大小
-s:显示目录占用的磁盘空间大小,不要显示其下子目录和文件占用的磁盘空间大小
-c:显示几个目录或文件占用的磁盘空间大小,还要统计它们的总和
–apparent-size:显示目录或文件自身的大小
-l :统计硬链接占用磁盘空间的大小
-L:统计符号链接所指向的文件占用的磁盘空间大小
三、常用的方法
1、查看当前目录使用的总空间大小
du -h --max-depth=0
实战举例:
[root@localhost ftp_data]# pwd
/home/ftp_data
[root@localhost ftp_data]# du -h --max-depth=0
217G .
2、查看当前目录下某文件使用空间大小
du -h --max-depth=1 文件名
其中,--max-depth=
指定深入目录的层数。注意:如果不使用该参数,则会循环列出当前目录下所有文件及文件夹使用的空间大小,文件多时会很乱。
- –max-depth=0:查看当前目录使用的总空间大小;
- –max-depth=1:查看当前目录使用总空间的大小以及当前目录下一级文件及文件夹各自使用的总空间大小;
3、du -a
使用此选项时,显示目录和目录下子目录和文件占用磁盘空间的大小。
4、du -s
使用此选项时,du只显示目录所占用磁盘空间的大小,而不显示其下子目录和文件占用磁盘空间的信息。
[root@localhost ftp_data]# du -s
258903360 .
但是一般不会这样用,这样看会很累,所以和-h搭配使用。du -sh : 查看当前目录总共占的容量。而不单独列出各子项占用的容量 。
5、du --apparent-size
显示文件或目录自身大小,而不是它们占用的磁盘空间大小。文件或目录占用磁盘空间的大小与它们自身大小有时候并非完全一致;这种现象非linux所独有,windows里也是如此。
我们看这个选项的帮助文档的解释:
The apparent size of a file is the number of bytes reported by wc –c
regular files ,or more generally, ls –l –block-size=1
or stat –format=%s
.For example, a file containing the word zoo
with no newline would, of course, have an apparent size of 3. Such a small file may require anywhere from 0 to 16 kib or more of disk space, depending on the type and configuration of the file system on which the file resides.
6、du -c
使用此选项时,不仅显示几个文件或目录各自占用磁盘空间的大小,还统计它们的总和。
7、du -l(小写l)
这个选项主要是针对硬链接。在统计目录占用磁盘空间大小时,-l选项会把硬链接也统计进来。帮助文档是这样解释的:Count the size of all files,even if they have appeared already(as a hard link)。
8、du -L(大写l)
这个选项主要是针对符号链接。它会把符号链接所指向的文件占用磁盘空间的大小也统计进来。帮助文档是这样解释的:Dereference symbolic links(show the disk space used by the file or directory that the link points to instead of the space used by the link)。
最后,du命令在统计目录占用磁盘空间时,默认不统计链接文件(无论是硬链接或是符号链接),所以要用-l和-L选项特地指出来;此外,du命令默认不显示目录下文件占用磁盘空间的信息,但是却默认显示其下子目录所占用磁盘空间的信息。怪哉。