linux 下df和du的区别

理解du与df命令在磁盘空间统计上的差异
本文详细解释了在使用du和df命令统计磁盘空间时可能出现的不同数值的原因,揭示了两者在统计逻辑上的差异,以及如何正确理解和解释这些差异。

df 命令是列出文件系统的整体磁盘使用量

命令使用方法

df [-ahikHTm] [目录或文件名]
参数:
-a     列出所有的文件系统
-k     以KB的容量显示文件系统
-m 以MB的容量显示各文件系统

-h 以人们较易阅读的GB、MB、KB等格式自行显示 (重要)

-H 以M=1000K代替M=1024K的换算方式
-T 连同该分区的文献系统名称(如ext3)也列出
-i 用inode的数量而不是硬盘的容量来显示


du  [-ahskm] 文件或目录名称

-a 列出所有的文件与目录容量
-h 以人们较易阅读的GB、MB、KB等格式自行显
-s列出总量
-k       以KB的容量显示文件系统
-m      以MB的容量显示各文件系统

如果文件或目录被省略的话,则会默认使用当前目录


当我们使用du和df命令时,有时会发现显示的差距比较大,这是为什么呢?

当进程要向文件中写入的时候,首先会得到指向该文件的指针,然后再向磁盘写入。接下来对该文件的写入都会用到这个指针,但写入并不会检查这个文件是否存在。它只会往已经确定好了的磁盘块中继续写操作。df命令检查的磁盘块的使用情况,然而du命令会先检查文件系统然后再统计每个目录使用的块的情况。由于du命令的这个特点,当进程所占有的文件不存在时,它就不会统计这些文件所占有的磁盘块。但是df 命令会一直对使用的磁盘块保持追踪,然后再报告磁盘块的使用情况。
如果有兴趣的话,可以看看下面的这篇文章
-- --- -- ----------- 

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.

<think>嗯,用户问的是Linuxdfdu命令的区别及使用场景。这个问题在之前的历史对话中也提到过,看来用户对这两个命令的实际差异有点困惑。用户的身份可能是系统管理员或开发运维人员,或者也可能是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、付费专栏及课程。

余额充值