foreword
When the system space usage is too large and you need to clear the space or clean up a certain file, sometimes the disk space is not released after the delete command is executed. Many people will be confused when they encounter this situation for the first time. They are thinking about whether it is like the Windows system. Like the Recycle Bin, delete is just tombstoned into the Recycle Bin? In fact, it is not the case. If you want to know about the recycle bin function of linux, you can communicate with me or check the information. It is also a more practical method. Here we mainly practice the problem that the space is not released after the file is deleted.
1. Experiment preparation
There are many ways to simulate this scenario, mainly to realize the scenario where the file is occupied. The easiest way is to copy files. Another common way is that students who have good programming or shell skills can write programs or scripts to continuously write content into a file. This time, it mainly reproduces quickly by copying files.
1.1 Create a slightly larger file
/* 主要针对此文件操作 */
[root@c7_2 local]# ll -h
total 3.0G
-rw-r--r--. 1 root root 2.5G May 4 17:43 all_backup.tar.gz
/* 此时根目录使用量6.5G */
[root@c7_2 ~]# df -lh
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 12M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 46G 6.5G 39G 15% /
/dev/sda1 1014M 150M 865M 15% /boot
tmpfs 378M 0 378M 0% /run/user/0
1.2 Occupy the file through the scp command
/* 开始远程拷贝至其他主机 */
[root@c7_2 local]# scp all_backup.tar.gz [email protected]:/home/test/
The authenticity of host '192.168.28.226 (192.168.28.226)' can't be established.
ECDSA key fingerprint is SHA256:QfJb1DogFmdZ0hkeVRvn2VHke+tkZ2+sNljhBBudooc.
ECDSA key fingerprint is MD5:2a:8a:63:80:35:17:f7:e9:2a:ea:13:98:eb:26:30:ba.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.28.226' (ECDSA) to the list of known hosts.
[email protected]'s password:
all_backup.tar.gz 0% 2432KB 2.4MB/s 17:53 ETA^Z
[1]+ Stopped scp all_backup.tar.gz [email protected]:/home/test
/* 放在后台运行 */
[root@c7_2 local]# bg %1
[1]+ scp all_backup.tar.gz [email protected]:/home/test &
1.3 Delete files
/* 删除文件 */
[root@c7_2 local]# rm -f all_backup.tar.gz
/* 查看磁盘空间,没有变化 */
[root@c7_2 local]# df -lh
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 12M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 46G 6.5G 39G 15% /
/dev/sda1 1014M 150M 865M 15% /boot
tmpfs 378M 0 378M 0% /run/user/0
/* 文件已不在 */
[root@c7_2 local]# ll -h
total 3.0G
drwxr-xr-x. 2 root root 6 Apr 11 2018 bin
drwxr-xr-x. 2 root root 6 Apr 11 2018 etc
drwxr-xr-x. 2 root root 6 Apr 11 2018 games
drwxr-xr-x. 2 root root 6 Apr 11 2018 include
drwxr-xr-x. 2 root root 6 Apr 11 2018 lib
drwxr-xr-x. 2 root root 6 Apr 15 05:56 lib64
drwxr-xr-x. 2 root root 6 Apr 11 2018 libexec
drwxr-xr-x. 2 root root 6 Apr 11 2018 sbin
drwxr-xr-x. 5 root root 49 Nov 17 16:46 share
drwxr-xr-x. 2 root root 6 Apr 11 2018 src
2. Processing method
You can use the lsof (list opened files) command to view the files that have been opened and which process the file is occupied by.
The lsof command is not available in the minimally installed system, it can be installed through yum first
yum install -y lsof
2.1 lsof view file
View all open files and filter out the files in deleted status (deleted)
[root@c7_2 local]# lsof |grep deleted
firewalld 818 root 6u REG 253,0 4096 36061750 /tmp/ffi0SEit6 (deleted)
gmain 818 998 root 6u REG 253,0 4096 36061750 /tmp/ffi0SEit6 (deleted)
tuned 1180 root 8u REG 253,0 4096 33554962 /tmp/ffio5Nu8r (deleted)
gmain 1180 1602 root 8u REG 253,0 4096 33554962 /tmp/ffio5Nu8r (deleted)
tuned 1180 1603 root 8u REG 253,0 4096 33554962 /tmp/ffio5Nu8r (deleted)
tuned 1180 1605 root 8u REG 253,0 4096 33554962 /tmp/ffio5Nu8r (deleted)
tuned 1180 1606 root 8u REG 253,0 4096 33554962 /tmp/ffio5Nu8r (deleted)
scp 1798 root 3r REG 253,0 2665433605 104181296 /usr/local/all_backup.tar.gz (deleted)
Found the file we just deleted and was opened by that process
2.2 View process
Through lsof, it is found that the all_backup.tar.gz file is occupied by process No. 1798, and you can check what the process is
[root@c7_2 local]# ps -ef|grep 1798
root 1798 1729 0 17:47 pts/0 00:00:00 scp all_backup.tar.gz [email protected]:/home/test
root 1799 1798 2 17:47 pts/0 00:00:03 /usr/bin/ssh -x -oForwardAgent=no -oPermitLocalCommand=no -oClearAllForwardings=yes -l test -- 192.168.28..226 scp -t /home/test
root 1868 1806 0 17:49 pts/1 00:00:00 grep --color=auto 1798
Exactly the copy command we executed earlier
2.3 Free up space
For this kind of query status, it is necessary to end the corresponding program
/* 杀掉对应进程 */
[root@c7_2 local]# kill -9 1799 1798
/* 空间已释放 */
[root@c7_2 local]# df -lh
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 12M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 46G 4.0G 42G 9% /
/dev/sda1 1014M 150M 865M 15% /boot
tmpfs 378M 0 378M 0% /run/user/0
Note: If you want to clear the log, you can use the echo " ">filename command to clear the file online. There is no need to end the process violently. If this method is invalid, try ending the process again.
3. Conclusion
This article is mainly applicable to the following scenarios:
- delete file space not freed
- The disk is full but the file cannot be found
Prompt again, if you can use the echo " ">filename command to clear the file online, there is no need to end the process violently. If this method is invalid, try ending the process again.