During server operation and maintenance (O&M), we often receive an alarm about the server disk space

The next step is to find the directory or file that is causing the disk space to be full

How do I find directories or files that take up a lot of space?

A clumsy approach is to list the space occupied by each directory in the root directory using the du-hs command

Then use the same method to continue to the corresponding directory to find

A more efficient method is to use du -d or –max-depth to set the directory depth. If the directory depth increases, more directories will be displayed. In this case, you can use grep to filter the directory

du -h -d 2|grep [GT] |sort -nr
du -h --max-depth=2|grep [GT] |sort -nr
Copy the code

In this way, you can search for large directories that occupy disk space in G or T and sort them

Or it can be queried through find

find / -type f -size +1G -exec du -h {} \;
Copy the code

In terms of efficiency, Find is faster and more flexible than DU

With both methods, we can quickly find the culprit of disk space

You think it’s that easy? A lot of times, you’ll find that if you look through find or du for a long time, you’ll find that the total footprint is very different from the disk footprint that DF sees, as shown in the two graphs above

Using df, the disk uses 37GB, but using du-hs in the root directory, the total space is almost 10GB, there is no hidden directory, then who ate the space?

Obviously, some space is occupied by deleted files, files are deleted, but resources are not freed

Lsof: lsof: lsof: lsof: lsof: lsof: lsof: lsof

lsof +L1

As can be seen from the result, there is a large log file of about 28GB, deleted, but the space is not released, this is a very common situation

The solution is to restart the Tomcat application to free up space

Is disk space being eaten?

Another frequently asked question is the disk viewed by df

It will be found that the sum of “Used” and “Avail” is not enough Size, and inexplicably part of it is eaten

This is a Linux file system security policy that reserves 5% of the disk space for root by default for emergency use. This ensures that some critical applications (such as databases) have some leeway when the hard drive is full and do not crash immediately

We can modify the proportion of reserved space through Tune2FS

tune2fs -m 1 /dev/vda1

You can see the before and after comparison below

So the space that was eaten, was spit out