In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "when rm deletes the file space in Linux?". In the operation of actual cases, many people will encounter such a dilemma. Next, 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!
Delete a file
At Linux, did you ever naively think that when you delete a file using rm, the space you take up is freed? Things may not always be what they want.
Don't you believe me? Let's look at the following example.
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
Among them, the dd command can refer to the "practical detailed explanation of the dd command", while / dev/urandom has been introduced in "do you know the Linux special device file".
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.
"does the rm delete the file space in Linux free?" this is the end of the introduction. Thank you for your 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.