df -h統計的信息與du -sh不一致的原因(轉)


 

有時候會遇到這樣的問題:
df -h統計一個目錄,顯示有約100M可用空間,使用了5G;
而用du -sh統計該目錄下的文件大小,卻發現總共才占用了1G。
也就是說,二者統計結果差距巨大。

例如:

1、df -h /tmp/

結果:

Size Used Avail Use% Mounted on
3.9G 3.5G 220M 95% /tmp

2、du -sh /tmp/

結果:

132K    /tmp/

文章來源:http://www.codelast.com/

結果差異巨大。

引用網上的一段話,原因是這樣的:

(1)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.2)The difference is that whenever an application has an open file, but the file is already deleted, 
then it is counted in the df output (because the space is certainly not free) 
but not in du (because it is not being used by a file).

如何查看是什么進程導致的問題?

假設你發現是 /tmp/ 目錄不對勁,那么就這樣就可以查看:

lsof | grep /tmp/

輸出的結果中,注意某些含有“(deleted)”字樣的記錄,它們中的一部分就是罪魁禍首,將它們kill掉即可(如果可以重啟這些進程所對應的服務的話,也有可能解決問題)。

http://www.codelast.com/?p=1066

 


匯總一些原理分析
實現原理:
du -s命令通過將指定文件系統中所有的目錄、符號鏈接和文件使用的塊數累加得到該文件系統使用的總塊數;
df命令通過查看文件系統磁盤塊分配圖得出總塊數與剩余塊數。
du是用戶級程序,不考慮Meta Data(系統為自身分配的一些磁盤塊)

ps:應用程序打開的文件句柄沒有關閉的話,會造成df命令顯示的剩余磁盤空間少。而du則不會。


[root@hadoop01 ~]# du -sh . [對當前目錄下所有的目錄和文件的大小進行匯總,-s表示匯總,-h表示以KB, MB, GB, TB格式進行人性化顯示]
[root@hadoop01 ~]# du -sh *|grep [MG]|sort -nr  [篩選出大小為MB和GB級別的目錄和文件,並降序排序]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM