df和du的区别以及磁盘空间使用问题

本文解释了du和df命令在报告文件系统磁盘使用情况和可用空间时可能出现不同结果的原因。du命令递归统计指定目录或文件的磁盘使用量,而df命令显示已挂载文件系统的磁盘占用、使用和可用空间等信息。当后台程序写入文件后该文件被删除,du和df报告的磁盘使用量会出现差异,df值更高。
solarissparc
root

df-kb结果
/dev/vx/dsk/home121972201108651598873392%/home

du-ks查看/home分区情况为
10dep
27lost+found
395517oracle
288bigml.lsof
212309wei
4041696lan
346074xue
520xue.log
du相加的结果和df的结果不同?!


下面这篇文章给出答案

DocumentId:26928Synopsis:duanddfDifferences(originallypublished8/91)
Updatedate:2001-05-13Description:duanddfDifferences
------------------

Thisarticleexplainshowreportingdiskusageduandreportingfreediskspace
onfilesystemsdfmayshowdifferentnumbers.

du
--

Theduusercommandgivesthenumberofkilobytescontainedinallfilesand,
recursively,directorieswithineachspecifieddirectoryorfile(filename).
Iffilenameismissing,`.'(thecurrentdirectory)isused.Afilewhich
hasmultiplelinkstoitisonlycountedonce.

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.

Notethatthelastnumber,1211isthegrandtotal(inkilobytes)forthe
directory.

df
--

Thedfusercommanddisplaysthefollowinginformation:

amountofdiskspaceoccupiedbycurrentlymountedfilesystems
theamountofusedandavailablespace
howmuchofthefilesystem'stotalcapacityhasbeenused

Usedwithoutarguments,dfreportsonallmountedfilesystems.

EXAMPLE:

system%df

FilesystemkbytesusedavailcapacityMountedon
/dev/ip0a74454714198670%/
/dev/ip0g4227735291275893%/usr

Note:usedplusavailislessthantheamountofspaceinthefilesystem
(kilobytes)becausethesystemreservesafractionofthespaceinthefile
systemtoallowitsallocationroutinestoworkwell.Theamountreservedis
typicallyabout10%.(Thismaybeadjustedusingthetunefscommand.Referto
themanpagesontunefs(8)formoreinformation.)Whenallthespaceonafile
system,exceptforthisreserve,isinuse,onlythesuper-usercanallocate
newfilesanddatablockstoexistingfiles.This,however,maycausethefile
systemtobeoverallocated.Whenafilesystemisoverallocatedinthisway,
dfmayreportthatthefilesystemismorethan100%utilized.

Ifargumentstodfarediskpartitions(forexample,/dev/ip0asorpathnames),
dfproducesareportonthefilesystemcontainingthenamedfile.Thus,df
showstheamountofspaceonthefilesystemcontainingthecurrentdirectory.

ProblemDefinition
-----------------

Thissectiongivesthetechnicalexplanationofwhyduanddfsometimesreport
differenttotalsofdiskspaceusage.

Whenaprogramthatisrunninginthebackgroundwritestoafilewhilethe
processisrunning,thefiletowhichthisprocessiswritingisdeleted.
Runningdfanddushowsadiscrepancyintheamountofdiskspaceusage.The
dfcommandshowsahighervalue.

ExplanationSummary
------------------

Whenyouopenafile,yougetapointer.Subsequentwritestothisfile
referencesthisfilepointer.Thewritecalldoesnotchecktoseeifthefile
isthereornot.Itjustwritestothespecifiednumberofcharactersstarting
atapredeterminedlocation.Regardlessofwhetherthefileexistornot,disk
blocksareusedbythewriteoperation.

Thedfcommandreportsthenumberofdiskblocksusedwhiledugoesthroughthe
filestructureandandreportsthenumberofblocksusedbyeachdirectory.As
farasduisconcerned,thefileusedbytheprocessdoesnotexist,soitdoes
notreportblocksusedbythisphantomfile.Butdfkeepstrackofdiskblocks
used,anditreportstheblocksusedbythisphantomfile.
<think>嗯,用户问的是Linux中dfdu命令的区别使用场景。这个问题在之前的历史对话中也提到过,看来用户对这两个命令的实际差异有点困惑。用户的身份可能是系统管理员或开发运维人员,或者也可能是Linux新手。用户可能是在管理服务器时遇到了磁盘空间问题,想搞清楚这两个工具分别该在什么场景使用。深层需求应该是想避免误用命令导致判断失误。关于这两个命令的区别,可以参考引用[1]引用[5]提到的核心差异:df是从文件系统层面统计,du是基于文件遍历计算。这一点非常重要,因为如果磁盘存在空间"黑洞",比如文件被删除但进程仍占用,dfdu的结果就会不一致。用户可能正是遇到了这种困惑的情况。实际回答时要强调四个关键差异点:1)统计维度不同;2)范围不同;3)来源数据不同;4)速度差异。使用场景要给出具体建议,比如当磁盘实际剩余空间告警时应该用df,而想知道某个目录占多大空间该用du。可以补充个常见陷阱:docker容器或lvm卷的情况,du可能无法统计挂载点外的空间。这个在引用[5]提到过但没有展开,用户如果是运维可能会需要知道这个细节。后续问题可以围绕几个实战场景:1)命令结果不一致时怎么排查;2)扩展工具如ncdu;3)自动化监控的脚本编写要点。这些都能帮助用户把知识转化为实际操作能力。</think> 在Linux系统中,**df**(Disk Free)**du**(Disk Usage)都是磁盘空间管理工具,但其核心逻辑适用场景有本质区别: --- ### **一、核心区别** | **维度** | **`df` 命令** | **`du` 命令** | |-------------------|----------------------------------------------|----------------------------------------------| | **统计对象** | 整个**文件系统**或磁盘分区 | 单个**文件/目录**的磁盘占用 | | **数据来源** | 文件系统的**超级块**元数据 | 递归**遍历目录**统计文件大小 | | **空间类型** | 报告**未分配空间**(剩余容量) | 只统计**已分配空间**(文件实际占用量) | | **性能** | 快速(直接读取元数据) | 较慢(需扫描文件树) | --- ### **二、原理详解** 1. **`df` 的工作机制** 通过查询文件系统元数据(如`/proc/mounts`或内核挂载信息),直接获取**分区级别的容量信息**[^5]。例如: ```bash df -h /dev/sda1 # 查看sda1分区的使用率 ``` 输出结果中的"可用空间"包含**未被任何文件占用的剩余容量**。 2. **`du` 的工作机制** 递归计算指定路径下所有文件的实际磁盘块占用,例如: ```bash du -sh /var/log # 统计/var/log目录的总大小 ``` 结果仅反映**已写入文件的物理空间**,不包含分区剩余空间[^2]。 --- ### **三、典型使用场景对比** | **命令** | **适用场景** | **典型示例** | |----------|----------------------------------------------------------------------------|---------------------------------------------| | **`df`** | 监控**磁盘分区是否即将耗尽** | `df -hT`(查看所有分区的使用率及文件系统类型) | | **`du`** | 定位**大文件/目录**,分析空间占用来源 | `du -h --max-depth=1 /home`(查看/home下子目录大小) | --- ### **四、结果不一致的常见原因** 当`df`显示磁盘已满,但`du`统计的目录总大小较小时,可能由以下问题导致: 1. **文件被删除但进程仍占用** 进程持有已删除文件的句柄时,空间不会释放(需重启进程)[^5]。 2. **挂载点覆盖** 目录被其他文件系统挂载覆盖,此时`du`无法统计挂载点外的空间(如Docker卷、LVM)。 3. **隐藏的系统文件** 日志文件、快照或临时文件可能被内核占用,普通用户无权限扫描。 > 📌 **处理建议**: > 使用`lsof -nP \| grep deleted`查找被删除但仍占空间的文件,或检查挂载点(`mount \| grep <路径>`)。 --- ### **五、命令组合使用案例** **问题定位**:磁盘空间不足,但找不到大文件 1. 用`df`确认分区使用率: ```bash df -h / # 查看根分区使用情况 ``` 2. 用`du`定位最大目录: ```bash du -h --max-depth=1 / 2>/dev/null | sort -hr # 忽略权限错误,按大小降序 ``` 3. 若`du`结果远小于`df`的已用量,检查隐藏占用: ```bash sudo du -shx /* # 跳过跨文件系统的挂载点 ``` --- ### **六、总结:如何选择工具** - ✅ **用 `df` 回答**: *"磁盘还剩多少可用空间?分区使用率是否超阈值?"* - ✅ **用 `du` 回答**: *"哪个目录最占空间?这个项目文件夹用了多少磁盘?"* > ⚠️ **注意**:在NFS、OverlayFS等复杂文件系统中,两者结果差异更常见,需结合具体环境分析[^3][^5]。 --- **相关问题** 1. 为什么 `df` 显示的磁盘使用率与 `du` 计算结果不同?如何排查? 2. 如何用 `du` 命令快速找出Linux系统中的前10大文件? 3. 在Docker容器中执行 `df` `du` 会有什么特殊行为? 4. 除了 `df`/`du`,还有哪些工具可以分析磁盘空间(如ncdu、baobab)?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值