Causes and treatment of inconsistent disk space usage for DF and du display

Source: Internet
Author: User
Tags disk usage

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
    1. [Email protected] testdu]# df-h/dev/sda1
    2. File system capacity has been used with available percent mount points
    3. /DEV/SDA1 49G 776M 45G 2%/var
(2) Create a large 1GB file [Plain]View Plaincopy
    1. [[email protected] var]# dd If=/dev/zero of=myfile.iso bs=1024k count=1000
    2. Recorded 1000+0 read-in
    3. Recorded the writing of 1000+0.
    4. 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
    1. [Email protected] var]# df-h/dev/sda1
    2. File system <span style= "White-space:pre" > </span> capacity already used with available% mount points
    3. /DEV/SDA1 49G 1.8G 44G 4%/var
Du results: [Plain]View Plaincopy
    1. [Email protected] var]# du-sh/var/
    2. 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
    1. [Email protected] var]# tail-f Myfile.iso &
    2. [1] 23277
    3. [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
    1. [Email protected] var]# Lsof | grep Myfile.iso
    2. Tail 23955 root 3r REG 8,1 1048576000 7999/var/myfile.iso (Deleted)
[Plain]View Plaincopy
    1. [Email protected] var]# du-sh/var/
    2. 596m/var/
    3. [Email protected] var]# df-h/dev/sda1
    4. File system capacity has been used with available percent mount points
    5. /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
    1. [Email protected] var]# Lsof | grep Myfile.iso
    2. [Email protected] var]#
[Plain]View Plaincopy
    1. [Email protected] var]# du-sh/var/; Df-h/dev/sda1
    2. 596m/var/
    3. File system capacity has been used with available percent mount points
    4. /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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.