现在的位置: 首页 > 综合 > 正文

df与du命令输出区别

2013年09月06日 ⁄ 综合 ⁄ 共 2544字 ⁄ 字号 评论关闭

1、正常情况下,df和du输出结果都会有差距BSD爱好者乐园,c:L(v%j4NOu(S

5z.{ I
i Tc
du -s命令通过将指定文件系统中所有的目录、符号链接和文件使用的块数累加得到该文件系统使用的总块数;BSD爱好者乐园#X;RG%sZ@
而df命令通过查看文件系统磁盘块分配图得出总块数与剩余块数。BSD爱好者乐园2h6@K8Mk B
qM�O9Uk

文件系统分配其中的一些磁盘块用来记录它自身的一些数据,如i节点,磁盘分布图,间接块,超级块等。这些数据对大多数用户级的程序来说是不可见的,通常称为Meta Data。
;D.]"g Vl#Odu命令是用户级的程序,它不考虑Meta Data,而df命令则查看文件系统的磁盘分配图并考虑Meta Data。BSD爱好者乐园(]S:_)nB"eUnC
BSD爱好者乐园6Z2d4M#p4tV5Sj
因此正常情况下,df计算的USED空间会比du计算的结果要稍大。BSD爱好者乐园-K1k U]3K"/

b#P)v!Cy#R#Ho2、异常情况下,df计算的USED空间会比du大很多BSD爱好者乐园&T
[W|
VsOy

BSD爱好者乐园B6n(M$O%O{
这也是Rocket说的之前碰到的问题,df查看结果文件系统100%使用了,而du的结果是还有6GB空闲的,就这么个问题硬件厂商一个SUPPORT居然不知道怎么解释,这也是让我好奇晚上回来查查看究竟的原因,结果GOOGLE一下就有了,呵呵。
1?5H;j,@{;v
PX!JT,Q.LuGE-X原因在于du是以文件名、目录名为依据计算空间使用的,而df是以硬盘块使用情况来计算空间使用的。
D*qE6D+D
'K3v?l CimA当一个应用程序正在写一个大文件的时候,我们RM或者MV了这个文件(UNIX是允许这么干的,WINDOWS在这一点上傻有傻福),应用程序会占有句柄,并根据句柄所指磁盘位置直接写磁盘,而不会检查该文件是否被删除。BSD爱好者乐园r&Wx:a#/!u

o$i1U!u+qV
此就会产生上述的问题。具体到Oracle层面,可能发生这种情况的有:Oracle因为某种原因在生成很大的TRACE文件,可能导致/oracle等
目录满,如果此时直接RM或MV掉该TRACE文件会发现空间并不会释放,进而可能导致Oracle数据库DOWN机。BSD爱好者乐园bgCo4a#Wb"m

[KQfH
gnSy~
解决办法:使用“> tracefile.trc”命令清空掉该文件,如果需要保留TRACE文件便于事后分析问题,可以使用CP先复制该文件到其他地方,然后清空掉原来的文件。BSD爱好者乐园7B(M*oJ&vG

/+aD]Y
yNw
关于df和du的输出差别原文解释如下:BSD爱好者乐园
rU*Q@f'B})v)p
u"g

}]GU&VNMybQProblem DefinitionBSD爱好者乐园@ ^,YIl#Cv!|
------- ----------
CS`M{ u*DP}
+C`za
w;F(?
This section gives the technical explanation of why du and df sometimes reportBSD爱好者乐园ro.i}'[
J
{
d{

different totals of disk space usage.BSD爱好者乐园iA S%a~)f�A^
BSD爱好者乐园 y3m6Yh9eO
When a program that is running in the background writes to a file while theBSD爱好者乐园 Wz1wBK5XX
process is running, the file to which this process is writing is deleted.
/Cp�xvd5q�C"K%URunning df and du shows a discrepancy in the amount of disk space usage. The
R�aO,MQ,M
PS;w`
df command shows a higher value.
P[A4JMnBSD爱好者乐园�WVFxnu
Explanation SummaryBSD爱好者乐园;QM _6QYx o
----------- -------BSD爱好者乐园'rKa]/~N6s

(H:yVQ�Xr RWhen you open a file, you get a pointer. Subsequent writes to this fileBSD爱好者乐园[+r&~q6cS�ww;d;H2Up
references this file pointer. The write call does not check to see if the fileBSD爱好者乐园1Pj)WT._7e,c
is there or not. It just writes to the specified number of characters startingBSD爱好者乐园eK _:u�v
at a predetermined location. Regardless of whether the file exist or not, disk
1[J$w@*Ptblocks are used by the write operation.BSD爱好者乐园J(}3u
|E;N;I.`(Q8q

ws.Nmo
gXb1]
The df command reports the number of disk blocks used, while du goes through the
ZRq^N@`ifile structure and reports the number of blocks used by each directory. As
h
QSUk2u-[Vvz
far as du is concerned, the file used by the process does not exist, so it doesBSD爱好者乐园e(t nKA$A|UG3j
P

not report blocks used by this phantom file. But df keeps track of disk blocks
"|(R&KeD/cv-s5M nused, and it reports the blocks used by this phantom file.

 原文地址
http://www.bsdlover.cn/html/12/n-12.html

抱歉!评论已关闭.