Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

RM delete file space release under linux

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "RM deletion file space release under linux". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Generate a random content file of a specified size

Let's first take a look at the current space size of each mount directory:

$df-h / dev/sda11 454M 280M 147M 66% / boot

I picked one of the results to show here (you can choose any mount directory), and then I'm going to generate a file under / boot.

First, we produce a file of 50m size:

$dd if=/dev/urandom of=/boot/test.txt bs=50M count=1

At this point, we have generated a file with a size of 50m. Let's take a look at boot:

$df-h / dev/sda11 454M 312M 115M 74% / boot

You don't have to worry about how much more you have here, you just need to pay attention to the increase in the number of files under / boot.

Test program:

# include # include int main (void) {FILE * fp = NULL; fp = fopen ("/ boot/test.txt", "rw+"); if (NULL = = fp) {perror ("open file failed"); return-1;} while (1) {/ / do nothing sleep (1);} fclose (fp); return 0;}

As for the program itself, it doesn't actually do anything, just open a file and loop it all the time. Compile and run:

$gcc-o openFile openFile.c $. / openFile

Open another window and delete test.txt:

$rm / boot/test.txt

Take another look at the boot space:

$df-h dev/sda11 454M 312M 115M 74% / boot

Huh? The size of the space hasn't changed at all! Did you delete it using rm?

Let's stop the openFile program and take a look at:

$df-h / dev/sda11 454M 280M 147M 66% / boot

Dear, the space will be released immediately, that is, as expected, our files have been deleted.

Under what circumstances will a file be deleted?

In fact, unlink deletion is only possible when the reference count of a file is 0 (including the number of hard links), and as long as it is not 0, it will not be deleted. The so-called deletion is just the deletion of the link from the file name to inode. As long as the new data is not rewritten, the block data blocks on the disk will not be deleted, so you will see that even if the deletion library runs away, some data can still be recovered. In other words, when a program opens a file (gets the file descriptor), its reference count will be + 1. Although the file looks like it has been deleted, it actually just subtracts the reference count by 1, but because the reference count is not 0, the file will not be deleted.

Struct inode {struct hlist_node ilist; / * pointer to hash list * / struct list_head ilist; / * backing dev IO list * / struct list_head ilist list; / * inode linked list of super blocks * / struct list_head ilist; / * directory item object chain header of inode * / unsigned long ionomino; / * Inode number * / atomic_t i_count / * reference count * / unsigned int iLink; / * number of hard links * /

There are a lot of details about it (such as the number of hard links will also affect whether the file is deleted), which will not be expanded here.

How do I free up the space that has been deleted?

With regard to release, as mentioned earlier, just restart the process that opened the file. But is there a way to find out which files have been deleted but still opened by some process?

Naturally, there is a way:

$lsof | grep deleted

The files marked as deleted are some of these files.

In fact, in the previous example, we can easily observe (the openFile program runs and the test.txt file is deleted):

$ls-al / proc/ `pidof openFile` / fd total 0 lrwx- 1 root root 64 May 4 09:27 0-> / dev/pts/25 lrwx- 1 root root 64 May 4 09:27 1-> / dev/pts/25 lrwx- 1 root root 64 May 4 09:27 2-> / dev/pts/25 lrwx- 1 root root 64 May 4 09:27 3-> / boot/test.txt (deleted)

See, there is the word deleted after test.txt.

Now that we have said that the file has not been deleted in this case, can it still be restored? It can actually be read.

Summary

In fact, such files are deleted, often appear in the log files of the program, you may have a scheduled task to clean up the log files generated by the program, but if the program itself forgets to close the handle, it will cause the disk space not to be freed. In the end, you think that the files have been deleted, but the disk is still occupied. So, get into the habit of opening the file, and remember to close the file descriptor when you don't use it.

If you find that a large number of files have been deleted, but the space has not returned to normal, then you might as well see if there are programs that have opened these files.

This is the end of the content of "RM delete file space release under linux". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report