How to solve the difference in the used space detected by the Linux df and du commands?

  

When a user logs in to the Linux system server to view it, it is found that the results detected by df and du are different. The actual use space detected by du is significantly smaller than the used space detected by df. A lot, what is this all about? Is some space hidden? Let's take a look at the different solutions used by the df and du commands.

The method is as follows:

First, we use df to view the used space:

As you can see from the above figure, the root partition has used 5456700KB (contract 5.20GB).

Then, we use du to find the size of each directory under the root partition:

Exclude the two directories in the red box (because they are mounted), the total of all directories The size is 3075332KB (contract 2.93GB), which is 2.27GB smaller than df.

Why is there so much space left?

Actually, because we deleted some files that the process is writing, although it has been deleted by us, as long as the process is still, the file will not be deleted, but it will be temporarily stored in the system. Somewhere, it's a bit like the Windows recycle bin.

To solve this problem, the easiest way is to restart the server, but the production environment is generally not easy to restart, so we can solve it in other ways.

First use the lsof command with the grep command to find out which ones have been deleted, but still occupy the system space "rogue”.

The file in the red box above is a large file I deleted before. I am trying to restart this program (node). I killed the process directly because I did autostart, so I You don't have to manually start it.

The used space has dropped to 3226324KB (contract 3.08GB), which is very close to the total space of du statistics.

Next, I restarted the other deleted files one by one. The size of the final df is almost the same as the total space of the du statistics.

At this point, it solves the different files found by df and du, and drives away the "rogue" in the system, and it is our space.

The df and du commands have been found to have different solutions for using the space. If you have this problem, you can follow the above steps to solve the problem.

Copyright © Windows knowledge All Rights Reserved