DU和DF(转)
这个文档能给你一个满意的答复:)
Document Id: 26928Synopsis: du and df Differences (originally published 8/91)
Update date: 2001-05-13Description: du and df Differences
-- --- -- -----------
This article explains how reporting disk usage du and reporting free disk space
on file systems df may show different numbers.
du
--
The du user command gives the number of kilobytes contained in all files and,
recursively, directories within each specified directory or file (filename).
If filename is missing, `.' (the current directory) is used. A file which
has multiple links to it is only counted once.
EXAMPLE:
system % du
5 ./jokes
33 ./squash
44 ./tech.papers/lpr.document
217 ./tech.papers/new.manager
401 ./tech.papers
144 ./memos
80 ./letters
388 ./window
93 ./messages
15 ./useful.news
1211 .
Note that the last number, 1211 is the grand total (in kilobytes) for the
directory.
df
--
The df user command displays the following information:
amount of disk space occupied by currently mounted file systems
the amount of used and available space
how much of the file system's total capacity has been used
Used without arguments, df reports on all mounted file systems.
EXAMPLE:
system % df
Filesystem kbytes used avail capacity Mounted on
/dev/ip0a 7445 4714 1986 70% /
/dev/ip0g 42277 35291 2758 93% /usr
Note: used plus avail is less than the amount of space in the file system
(kilobytes) because the system reserves a fraction of the space in the file
system to allow its allocation routines to work well. The amount reserved is
typically about 10%. (This may be adjusted using the tunefs command. Refer to
the man pages on tunefs(8) for more information.) When all the space on a file
system, except for this reserve, is in use, only the super-user can allocate
new files and data blocks to existing files. This, however, may cause the file
system to be over allocated. When a file system is over allocated in this way,
df may report that the file system is more than 100% utilized.
If arguments to df are disk partitions (for example, /dev/ip0as or path names),
df produces a report on the file system containing the named file. Thus, df
shows the amount of space on the file system containing the current directory.
Problem Definition
------- ----------
This section gives the technical explanation of why du and df sometimes report
different totals of disk space usage.
When a program that is running in the background writes to a file while the
process is running, the file to which this process is writing is deleted.
Running df and du shows a discrepancy in the amount of disk space usage. The
df command shows a higher value.
Explanation Summary
----------- -------
When you open a file, you get a pointer. Subsequent writes to this file
references this file pointer. The write call does not check to see if the file
is there or not. It just writes to the specified number of characters starting
at a predetermined location. Regardless of whether the file exist or not, disk
blocks are used by the write operation.
The df command reports the number of disk blocks used while du goes through the
file structure and and reports the number of blocks used by each directory. As
far as du is concerned, the file used by the process does not exist, so it does
not report blocks used by this phantom file. But df keeps track of disk blocks
used, and it reports the blocks used by this phantom file.
Document Id: 26928Synopsis: du and df Differences (originally published 8/91)
Update date: 2001-05-13Description: du and df Differences
-- --- -- -----------
This article explains how reporting disk usage du and reporting free disk space
on file systems df may show different numbers.
du
--
The du user command gives the number of kilobytes contained in all files and,
recursively, directories within each specified directory or file (filename).
If filename is missing, `.' (the current directory) is used. A file which
has multiple links to it is only counted once.
EXAMPLE:
system % du
5 ./jokes
33 ./squash
44 ./tech.papers/lpr.document
217 ./tech.papers/new.manager
401 ./tech.papers
144 ./memos
80 ./letters
388 ./window
93 ./messages
15 ./useful.news
1211 .
Note that the last number, 1211 is the grand total (in kilobytes) for the
directory.
df
--
The df user command displays the following information:
amount of disk space occupied by currently mounted file systems
the amount of used and available space
how much of the file system's total capacity has been used
Used without arguments, df reports on all mounted file systems.
EXAMPLE:
system % df
Filesystem kbytes used avail capacity Mounted on
/dev/ip0a 7445 4714 1986 70% /
/dev/ip0g 42277 35291 2758 93% /usr
Note: used plus avail is less than the amount of space in the file system
(kilobytes) because the system reserves a fraction of the space in the file
system to allow its allocation routines to work well. The amount reserved is
typically about 10%. (This may be adjusted using the tunefs command. Refer to
the man pages on tunefs(8) for more information.) When all the space on a file
system, except for this reserve, is in use, only the super-user can allocate
new files and data blocks to existing files. This, however, may cause the file
system to be over allocated. When a file system is over allocated in this way,
df may report that the file system is more than 100% utilized.
If arguments to df are disk partitions (for example, /dev/ip0as or path names),
df produces a report on the file system containing the named file. Thus, df
shows the amount of space on the file system containing the current directory.
Problem Definition
------- ----------
This section gives the technical explanation of why du and df sometimes report
different totals of disk space usage.
When a program that is running in the background writes to a file while the
process is running, the file to which this process is writing is deleted.
Running df and du shows a discrepancy in the amount of disk space usage. The
df command shows a higher value.
Explanation Summary
----------- -------
When you open a file, you get a pointer. Subsequent writes to this file
references this file pointer. The write call does not check to see if the file
is there or not. It just writes to the specified number of characters starting
at a predetermined location. Regardless of whether the file exist or not, disk
blocks are used by the write operation.
The df command reports the number of disk blocks used while du goes through the
file structure and and reports the number of blocks used by each directory. As
far as du is concerned, the file used by the process does not exist, so it does
not report blocks used by this phantom file. But df keeps track of disk blocks
used, and it reports the blocks used by this phantom file.
du和df命令都被用于获得文件系统大小的信息:df用于报告文件系统的总块数及剩余块数,du -s /<filesystem>用于报告文件系统使用的块数。但是,我们可以发现从df命令算出的文件系统使用块数的值与通过du命令得出的值是不一致的。如下例:
# du -s /tmp 返回如下值:
---12920 /tmp
而 df /tmp返回如下值:
Filesystem --512-blocks-- Free --%Used --Iused-- %Iused --Mounted on
/dev/hd3 --------57344 --42208--- 26% ----391 ------4% --/tmp
从上面的值我们可以算出<total from df> - <Free from df> = <used block count>: 57344 - 42208 = 15136. 而15136大于12920。该值差异的存在是由于du与df命令实施上的不同: du -s命令通过将指定文件系统中所有的目录、符号链接和文件使用的块数累加得到该文件系统使用的总块数;而df命令通过查看文件系统磁盘块分配图得出总块数与剩余块数。
文件系统分配其中的一些磁盘块用来记录它自身的一些数据,如i节点,磁盘分布图,间接块,超级块等。这些数据对大多数用户级的程序来说是不可见的,通常称为Meta Data。
du命令是用户级的程序,它不考虑Meta Data,而df命令则查看文件系统的磁盘分配图并考虑Meta Data。df命令获得真正的文件系统数据,而du命令只查看文件系统的部分情况。例如,一个frag=4096 并且 nbpi=4096的空的大小为4MB的日志文件系统中Meta Data的分配情况如下:
1 4k block for the LVM
2 4k super blocks
2 4k blocks for disk maps
2 4k blocks for inode maps
2 4k blocks for .indirect
32 4k blocks for inodes
-------------------------
41 4k blocks for meta data on an empty 4MB file system
对于AIX 4.X版本:
执行 du /foo返回的结果如下:
----8 -------/foo/lost+found
----16 ------/foo
要使du命令输出的结果与df命令输出的结果匹配,我们必须要加上Meta Data。首先,将41个4k的块转换为以512字节为单位的值:
41 * 8 = 328
328(meta data) + 16(from du) = 344
所以有344个以512字节为单位的块分配给了这个空的文件系统。
而使用 df /foo命令我们可以得到下面的结果:
Filesystem --512-blocks --Free --%Used --Iused---%Iused --Mounted on
/dev/lv01 ------8192 -----7848 -----5% -----16 -----2% ----/foo
从中我们可以得到该文件系统使用的块数:8192(total blocks) - 7848(free blocks) = 344。该值与上面得出的值一致。
上面的换算方法对于空的文件系统很容易实现,但是对于非空的文件系统,由于Meta Data中文件间接块的大小不定,因此较难实现。所以我们不需要查看du 与 df返回的值的匹配关系,而只需要了解du -s命令返回的值反映了分配给文件及目录的磁盘块数,而df命令则反映了文件系统的实际分配情况。df命令反映的实际情况包含了用户数据(文件及目录)和Meta Data。
另一个表现出du与df命令不同之处的例子如下:
如果用户删除了一个正在运行的应用所打开的某个目录下的文件,则du命令返回的值显示出减去了该文件后的目录的大小。但df命令并不显示减去该文件后的大小。直到该运行的应用关闭了这个打开的文件,df返回的值才显示出减去了该文件后的文件系统的使用情况。
列出一个目录占用的空间
1.
du或du -s或du -k
du -S | sort -n 可以迅速发现那个目录是最大的。
2.
用df可以看到已安装的文件系统的空间大小及剩余空间大小。
3.
quota -v查看用户的磁盘空间信息,如果你用quota限制了用户空间大小的话。
# du -s /tmp 返回如下值:
---12920 /tmp
而 df /tmp返回如下值:
Filesystem --512-blocks-- Free --%Used --Iused-- %Iused --Mounted on
/dev/hd3 --------57344 --42208--- 26% ----391 ------4% --/tmp
从上面的值我们可以算出<total from df> - <Free from df> = <used block count>: 57344 - 42208 = 15136. 而15136大于12920。该值差异的存在是由于du与df命令实施上的不同: du -s命令通过将指定文件系统中所有的目录、符号链接和文件使用的块数累加得到该文件系统使用的总块数;而df命令通过查看文件系统磁盘块分配图得出总块数与剩余块数。
文件系统分配其中的一些磁盘块用来记录它自身的一些数据,如i节点,磁盘分布图,间接块,超级块等。这些数据对大多数用户级的程序来说是不可见的,通常称为Meta Data。
du命令是用户级的程序,它不考虑Meta Data,而df命令则查看文件系统的磁盘分配图并考虑Meta Data。df命令获得真正的文件系统数据,而du命令只查看文件系统的部分情况。例如,一个frag=4096 并且 nbpi=4096的空的大小为4MB的日志文件系统中Meta Data的分配情况如下:
1 4k block for the LVM
2 4k super blocks
2 4k blocks for disk maps
2 4k blocks for inode maps
2 4k blocks for .indirect
32 4k blocks for inodes
-------------------------
41 4k blocks for meta data on an empty 4MB file system
对于AIX 4.X版本:
执行 du /foo返回的结果如下:
----8 -------/foo/lost+found
----16 ------/foo
要使du命令输出的结果与df命令输出的结果匹配,我们必须要加上Meta Data。首先,将41个4k的块转换为以512字节为单位的值:
41 * 8 = 328
328(meta data) + 16(from du) = 344
所以有344个以512字节为单位的块分配给了这个空的文件系统。
而使用 df /foo命令我们可以得到下面的结果:
Filesystem --512-blocks --Free --%Used --Iused---%Iused --Mounted on
/dev/lv01 ------8192 -----7848 -----5% -----16 -----2% ----/foo
从中我们可以得到该文件系统使用的块数:8192(total blocks) - 7848(free blocks) = 344。该值与上面得出的值一致。
上面的换算方法对于空的文件系统很容易实现,但是对于非空的文件系统,由于Meta Data中文件间接块的大小不定,因此较难实现。所以我们不需要查看du 与 df返回的值的匹配关系,而只需要了解du -s命令返回的值反映了分配给文件及目录的磁盘块数,而df命令则反映了文件系统的实际分配情况。df命令反映的实际情况包含了用户数据(文件及目录)和Meta Data。
另一个表现出du与df命令不同之处的例子如下:
如果用户删除了一个正在运行的应用所打开的某个目录下的文件,则du命令返回的值显示出减去了该文件后的目录的大小。但df命令并不显示减去该文件后的大小。直到该运行的应用关闭了这个打开的文件,df返回的值才显示出减去了该文件后的文件系统的使用情况。
列出一个目录占用的空间
1.
du或du -s或du -k
du -S | sort -n 可以迅速发现那个目录是最大的。
2.
用df可以看到已安装的文件系统的空间大小及剩余空间大小。
3.
quota -v查看用户的磁盘空间信息,如果你用quota限制了用户空间大小的话。