Get the App
SLTechnology News&Howtos  ›  Servers  › 

Linux basic part Learning-- the principle of linux File deletion

Shulou Source: shulou.com Published: 2022-06-02 18:22:46 09月18日 Update

Principle of file deletion

Click to view knowledge of soft links and hard links

Link counter

Linux controls file deletion through the number of link

  A file will be deleted only if no link exists.

  generally speaking, each file has two link counters: i_count and i_nlink

  i_count   memory reference counter

The number of current file users (or called) of  . When a file is referenced by a process, the number of corresponding i_count will increase.

  i_nlink   disk reference counter

The number of   media connections (the number of hard links). When you create a hard link to a file, the corresponding number of i_nlink increases

Delete the command rm

Rm is actually reducing the disk reference count i_nlink

Rm deletes common sense

What happens if a file is being called by a process and the user performs a rm operation to delete the file?

   when the user executes the rm operation to delete the file, and then executes ls or other file management commands, the file can no longer be found, but the process of calling the deleted file continues to execute normally and can still read and write correctly from the file.

   this is because the rm operation only reduces the i_nlink of the file, and if there are no other links, the i_nlink will be 0; but because the file is still referenced by the process, the corresponding i_count of the file is not 0.

The condition that the file is actually deleted is i_nlink=0 and i_count=0

Tags: File link quantity counter process command time user disk principle generally speaking media user memory content just actually that is common sense Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft OPPO Reno MariaDB Apple Huawei