When you look at disk space usage under Linux, the most common use is Du and DF. However, there is a big difference between the two, and sometimes the output is even very large.
1. How to memorize these two commands
Du-disk Usage
Df-disk Free
2. How df and du Works 2.1 du works the du command treats the statistics file one-by-call Fstat this system call to get the file size. Its data is based on file acquisition, so there is a lot of flexibility, not necessarily for a partition, can span multiple partitions operation. If there are many files in the target directory, the Du speed will be slow. 2.2 DF Works The DF command uses the thing STATFS this system call, directly reads the partition's Super block information to get the partition usage. Its data is based on partition metadata, so it is only for the entire partition. Because DF reads the super block directly, the speed is not affected by the number of files. 3 Du and DF inconsistencies simulation of common DF and du inconsistencies is the issue of file deletion. When a file is deleted, it is not visible in the file system directory, so du will not count it anymore. However, if there is a running process that holds the handle to the deleted file, the file will not actually be deleted on the disk and the information in the partition's Super block will not change. This way DF will still count the deleted files. (1) Usage of current partition sda1
[Plain]View Plaincopy
- [Email protected] testdu]# df-h/dev/sda1
- File system capacity has been used with available percent mount points
- /DEV/SDA1 49G 776M 45G 2%/var
(2) Create a large 1GB file
[Plain]View Plaincopy
- [[email protected] var]# dd If=/dev/zero of=myfile.iso bs=1024k count=1000
- Recorded 1000+0 read-in
- Recorded the writing of 1000+0.
- 1048576000 bytes (1.0 GB) Replicated, 24.0954 sec, 43.5 mb/sec
(3) The partition SDA1 usage DF results at this time:
[Plain]View Plaincopy
- [Email protected] var]# df-h/dev/sda1
- File system <span style= "White-space:pre" > </span> capacity already used with available% mount points
- /DEV/SDA1 49G 1.8G 44G 4%/var
Du results:
[Plain]View Plaincopy
- [Email protected] var]# du-sh/var/
- 1.6g/var/
At this point the results are basically the same. (4) Simulate a process to open this large file and then delete this large file
[Plain]View Plaincopy
- [Email protected] var]# tail-f Myfile.iso &
- [1] 23277
- [Email protected] var]# rm-f Myfile.iso
(5) At this point, compare the results of DU and DF first to confirm that there is a process holding the myfile.iso handle.
[Plain]View Plaincopy
- [Email protected] var]# Lsof | grep Myfile.iso
- Tail 23955 root 3r REG 8,1 1048576000 7999/var/myfile.iso (Deleted)
[Plain]View Plaincopy
- [Email protected] var]# du-sh/var/
- 596m/var/
- [Email protected] var]# df-h/dev/sda1
- File system capacity has been used with available percent mount points
- /DEV/SDA1 49G 1.8G 44G 4%/var
As you can see, the DF results are unchanged, and Du is no longer counting the deleted file Myfile.iso. (6) Stop the simulation process and compare the Du and DF results first to confirm that no process holds the myfile.iso handle.
[Plain]View Plaincopy
- [Email protected] var]# Lsof | grep Myfile.iso
- [Email protected] var]#
[Plain]View Plaincopy
- [Email protected] var]# du-sh/var/; Df-h/dev/sda1
- 596m/var/
- File system capacity has been used with available percent mount points
- /DEV/SDA1 49G 776M 45G 2%/var
At this point, Myfile.iso has no process to occupy it, also deleted from the disk, the partition of the Super block information has been changed, DF is also shown normal. 4 things to be aware of in your work (1) when there is a large gap between Du and DF, consider whether a deleted file is not completed, by lsof the command and then stopping the process. (2) You can use the empty file to replace the deletion of files, the way is: Echo > Myfile.iso. (3) For the log files that frequently occur delete problems, in the order of renaming, emptying, deleting the sequence of operations. (4) In addition to RM, some commands will be indirectly deleted files, such as the gzip command after the completion of the deletion of the original file, in order to avoid the deletion of the problem, before compressing to confirm that no process to open the file.
Causes and treatment of inconsistent disk space usage for DF and du display