這個(gè)文檔能給你一個(gè)滿意的答復(fù):)
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命令都被用于獲得文件系統(tǒng)大小的信息:df用于報(bào)告文件系統(tǒng)的總塊數(shù)及剩余塊數(shù),du -s /<filesystem>用于報(bào)告文件系統(tǒng)使用的塊數(shù)。但是,我們可以發(fā)現(xiàn)從df命令算出的文件系統(tǒng)使用塊數(shù)的值與通過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命令實(shí)施上的不同: du -s命令通過將指定文件系統(tǒng)中所有的目錄、符號(hào)鏈接和文件使用的塊數(shù)累加得到該文件系統(tǒng)使用的總塊數(shù);而df命令通過查看文件系統(tǒng)磁盤塊分配圖得出總塊數(shù)與剩余塊數(shù)。
文件系統(tǒng)分配其中的一些磁盤塊用來記錄它自身的一些數(shù)據(jù),如i節(jié)點(diǎn),磁盤分布圖,間接塊,超級(jí)塊等。這些數(shù)據(jù)對(duì)大多數(shù)用戶級(jí)的程序來說是不可見的,通常稱為Meta Data。
du命令是用戶級(jí)的程序,它不考慮Meta Data,而df命令則查看文件系統(tǒng)的磁盤分配圖并考慮Meta Data。df命令獲得真正的文件系統(tǒng)數(shù)據(jù),而du命令只查看文件系統(tǒng)的部分情況。例如,一個(gè)frag=4096 并且 nbpi=4096的空的大小為4MB的日志文件系統(tǒng)中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
對(duì)于AIX 4.X版本:
執(zhí)行 du /foo返回的結(jié)果如下:
----8 -------/foo/lost+found
----16 ------/foo
要使du命令輸出的結(jié)果與df命令輸出的結(jié)果匹配,我們必須要加上Meta Data。首先,將41個(gè)4k的塊轉(zhuǎn)換為以512字節(jié)為單位的值:
41 * 8 = 328
328(meta data) + 16(from du) = 344
所以有344個(gè)以512字節(jié)為單位的塊分配給了這個(gè)空的文件系統(tǒng)。
而使用 df /foo命令我們可以得到下面的結(jié)果:
Filesystem --512-blocks --Free --%Used --Iused---%Iused --Mounted on
/dev/lv01 ------8192 -----7848 -----5% -----16 -----2% ----/foo
從中我們可以得到該文件系統(tǒng)使用的塊數(shù):8192(total blocks) - 7848(free blocks) = 344。該值與上面得出的值一致。
上面的換算方法對(duì)于空的文件系統(tǒng)很容易實(shí)現(xiàn),但是對(duì)于非空的文件系統(tǒng),由于Meta Data中文件間接塊的大小不定,因此較難實(shí)現(xiàn)。所以我們不需要查看du 與 df返回的值的匹配關(guān)系,而只需要了解du -s命令返回的值反映了分配給文件及目錄的磁盤塊數(shù),而df命令則反映了文件系統(tǒng)的實(shí)際分配情況。df命令反映的實(shí)際情況包含了用戶數(shù)據(jù)(文件及目錄)和Meta Data。
另一個(gè)表現(xiàn)出du與df命令不同之處的例子如下:
如果用戶刪除了一個(gè)正在運(yùn)行的應(yīng)用所打開的某個(gè)目錄下的文件,則du命令返回的值顯示出減去了該文件后的目錄的大小。但df命令并不顯示減去該文件后的大小。直到該運(yùn)行的應(yīng)用關(guān)閉了這個(gè)打開的文件,df返回的值才顯示出減去了該文件后的文件系統(tǒng)的使用情況。
列出一個(gè)目錄占用的空間
1.
du或du -s或du -k
du -S | sort -n 可以迅速發(fā)現(xiàn)那個(gè)目錄是最大的。
2.
用df可以看到已安裝的文件系統(tǒng)的空間大小及剩余空間大小。
3.
quota -v查看用戶的磁盤空間信息,如果你用quota限制了用戶空間大小的話。