Linux RM Delete File principle

Source: Internet
Author: User

Most of the time, we will find that a process is reading and writing to the current file, but we still can RM, is it strange? And under Windows, there will be an error, "The current file is being used", which has to be removed from the Linux under the principle of the file.
  
  
Linux is controlled by the number of link to delete the file, only if a file does not exist any link, the file will be deleted. In general, there are 2 link counters for each file: I_count and I_nlink.
  
The meaning of I_count is the number of users (or calls) of the current file, i_nlink the amount of media connections (number of hard links), which can be understood as I_count is the memory reference counter, and I_nlink is the reference counter for the disk.
  
When a file is referenced by a process, the corresponding number of i_count is incremented, and the corresponding I_nlink number is incremented when a hard link to the file is created.
  
For the Delete command RM, the actual reduction is the disk reference count I_nlink. There is a problem here, if a file is being called by a process and the user is performing an RM operation to delete the file, what will happen? When the user performs an RM operation to delete a file and then executes the LS or other file management commands, the file can no longer be found, but the process of invoking the deleted file continues to execute normally and the content can be read and written correctly from the file. Why is that?
  
This is because the RM operation only reduces the file I_nlink, if there is no other link i_nlink is 0, but because the file is still referenced by the process, so the file corresponding to the I_count is not 0, so even if the RM operation, but the system does not really delete the file, When only I_nlink and I_count are 0, the file is actually deleted. That is, you also need to dismiss the process's call to the file.
  
The above-mentioned I_nlink and I_count is the true condition of the file deletion, but when the file is not called, does the RM operation delete the file after the deletion can be retrieved?
  
Said before, RM Operation just reduce the file I_nlink, or 0, the actual is the file name to the Inode link deleted, at this time, and not delete the file entity (Block data block), at this time, if the machine to stop work, the data can be retrieved, If you continue to write data at this point, when the new data is likely to be allocated to the block blocks of data being deleted, the file is actually recycled.

Note: According to the above principle, the actual situation will be the following problems, the Web server disk space is not enough, delete all the useless log or first disk space is not enough, but with du-sh/* found disk space is much smaller than the total size of the hard disk, this is because only a i_nlink is deleted, There are other processes that use these log files, Apache or Tomcat, and reboot and look OK.

Static is a hard link---> Disk dynamic (called)---> memory.



Welcome to the Vomit Groove



This article is from the "lake and Laughter" blog, please make sure to keep this source http://hashlinux.blog.51cto.com/9647696/1760292

Linux RM Delete File principle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.