Detailed explanation of the problem that the space is not released after the Linux file is deleted

Detailed explanation of the problem that the space is not released after the Linux file is deleted

Preface

When the system space usage is too large and needs to be cleared or a certain file needs to be cleared, sometimes the disk space is not released after the deletion command is executed. Many people will be confused when they encounter this situation for the first time, wondering whether it is like the Recycle Bin of the Windows system, where deletion is just a logical deletion to the Recycle Bin? In fact, this is not the case. If you want to know more about the Recycle Bin function of Linux, you can communicate with me or look up information to learn about it. It is also a more practical method. Here we mainly practice the problem of space not being released after file deletion.

1. Experimental Preparation

There are many ways to simulate this scenario, mainly to realize the scenario where the file is occupied. The simplest way is to copy the file. Another common way is for students who are good at programming or shell skills to write a program or script to continuously write content to a file. This time we mainly reproduced it quickly by copying files.

1.1 Create a slightly larger file

/* Mainly for this file operation */
[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

/* At this time, the root directory usage is 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 Use the scp command to occupy the file

/* Start remote copying to other hosts*/
[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
 /* Run in the background*/
[root@c7_2 local]# bg %1    
[1]+ scp all_backup.tar.gz [email protected]:/home/test &

1.3 Deleting Files

/* delete file */
[root@c7_2 local]# rm -f all_backup.tar.gz 
/* Check disk space, no change*/
[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
/* The file no longer exists */
[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. Treatment methods

You can use the lsof (list opened files) command to view the opened files and which process is using them.

The lsof command is not available in the minimally installed system. You can install it through yum first.

 yum install -y lsof

2.1 lsof view files

View all opened files and filter out deleted files

[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 opened by that process

2.2 View the process

Through lsof, it is found that the all_backup.tar.gz file is occupied by process 1798. You can check what the specific 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

It is the copy command we executed before

2.3 Free up space

For this kind of query status, you need to end the corresponding program

/* Kill the corresponding process */
[root@c7_2 local]# kill -9 1799 1798
/* Space released*/
[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 while it is being written continuously, you can use the echo " ">filename command to clear the file online without violently ending the process. If this method does not work, try ending the process.

3. Conclusion

This article is mainly applicable to the following scenarios:

  • Deleting file space is not released
  • The disk is full but the file cannot be found

Let me remind you again that if you can use the echo " ">filename command to clear the file online, there is no need to violently end the process. If this method does not work, then try ending the process.

This is the end of this article about the problem of space not being released after Linux file deletion. For more related content about space not being released after Linux file deletion, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Linux file management command example analysis [permissions, create, delete, copy, move, search, etc.]
  • How to deal with the problem that the file is deleted but the space is not released in Linux
  • Linux unlink function and how to delete files
  • Linux implements scheduled backup of MySQL database and deletes backup files older than 30 days
  • Linux regularly backs up the MySQL database and deletes previous backup files (recommended)
  • How to delete folders, files, and decompress commands on Linux servers
  • 5 Ways to Clear or Delete Large File Contents in Linux
  • Comparison of the efficiency of different methods of deleting files in Linux

<<:  HTML form submission method case study

>>:  MySQL sliding aggregation/year-to-date aggregation principle and usage example analysis

Recommend

web.config (IIS) and .htaccess (Apache) configuration

xml <?xml version="1.0" encoding=&qu...

Linux MySQL root password forgotten solution

When using the MySQL database, if you have not lo...

Implementation of whack-a-mole game in JavaScript

This article shares the specific code for JavaScr...

How to configure Linux CentOS to run scripts regularly

Many times we want the server to run a script reg...

MySQL storage engine basics

In the previous article, we talked about MySQL tr...

Detailed explanation of desktop application using Vue3 and Electron

Table of contents Vue CLI builds a Vue project Vu...

Implementation process of row_number in MySQL

1. Background Generally, in a data warehouse envi...

Vue custom optional time calendar component

This article example shares the specific code of ...

A Brief Analysis of CSS Selector Grouping

Selector Grouping Suppose you want both the h2 el...

H tags should be used reasonably in web page production

HTML tags have special tags to handle the title of...

How to implement the King of Glory matching personnel loading page with CSS3

Those who have played King of Glory should be fam...

RGB color table collection

RGB color table color English name RGB 16 colors ...