Solve the problem that the disk space is not freed after deleting files in CentOS (lsof command)

created at 03-25-2022 views: 305


After a long time, the disk space under my cloud server is getting less and less, and I need to clear and delete invalid large files. I used the find command to find that there are several large files in the west of the nginx/logs/ directory. log file. So I want to clear it, but when I delete the file, I found that the disk space has not changed. The specific operation is as follows:

First check your system's current disk space available 2.1G:

[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 20G 17G 2.1G 89% /
tmpfs 498M 0 498M 0% /dev/shm

Go to find nginx's access.log log log file is larger than 100M

[root@localhost ~]# find /usr/local/nginx/logs/ -size +100M -exec ls -lh {} \;
-rw-r--r-- 1 root root 1.2G Sep  7 10:23 /usr/local/nginx/logs/access.log

Delete this large file (please backup or migrate first)

[root@localhost ~]# rm -rf /usr/local/nginx/logs/access.log

Check the disk space~~OMG is strange. Obviously deleted, the free space is still 2.1G

[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 20G 17G 2.1G 89% /
tmpfs 498M 0 498M 0% /dev/shm

Look under nginx/logs/, no large files have been found

[root@localhost ~]# find /usr/local/nginx/logs/ -size +100M -exec ls -lh {} \;

Restart Nginx--Why restart please see the principle below

[root@localhost ~]# service nginx restart
Stopping nginx: [ OK ]
Starting nginx: [ OK ]

Check the disk space again, and it is instantly cute. The available space has become 3.3G

[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 20G 16G 3.3G 83% /
tmpfs 498M 0 498M 0% /dev/shm

check disk occupation linux

reasons why disk space is not released after CentOS deletes files

On Linux or Unix systems, deleting a file via rm or a file manager will unlink it from the file system's directory structure. However, if the file is open (a process is using it), the process will still be able to Reading the file, disk space has been occupied. And what I delete is the log log file of nginx, the file should be in use when I delete it.

The full name of lsof is list opened files, which is to list the files that have been opened in the system. We all know that in the Linux environment, everything is a file, a device is a file, a directory is a file, and even sockets is a file. Using the lsof command well is very helpful for daily linux management.


First get a list of files that have been deleted but are still occupied by the application, like this:

[root@localhost logs]# lsof |grep deleted
nginx 24747 root 12w REG 202,1 95721 663021 /usr/local/nginx/logs/access.log (deleted)
nginx 24749 root 12w REG 202,1 95721 663021 /usr/local/nginx/logs/access.log (deleted)
[root@localhost logs]#

how to release the process?

One way is to kill the corresponding process, or stop the application that uses this file, and let OS automatically reclaim disk space.

created at:03-25-2022
edited at: 05-31-2022: