Until recently, I thought that on ext file system, inodes have reference counters which count the number of times the file is referenced by a directory entry or a file descriptor.

Then, I learned that the reference counter only counts the number of directory entries referencing it. To falsify this, I read the reference count of a video file using ls -l. It was 1 as I expected because I didn't create any additional hard links to it. I then opened the video file with a video player and executed the same command again. To my surprise, the reference count was still 1. Therefore, I failed at falsifying.

However, I can definitely continue watching the video after removing its only directory entry. When opening a big video file and deleting its directory entry, the amount of free storage space on the file system does not change. It only changes (by the size of the video file) when the player reached the end of the video and closes the file descriptor or the player terminates itself (depending on the video player used).


What are the exact conditions for a file to be freed on an ext file system? I'm interested in how it is handled in ext2, ext3, and ext4. Are there differences depending on the kernel used or other parts of the operating system?


You are confusing two different counters: the file system link counter and the file descriptor reference counter.

  1. The file system link counter counts how many links to an inode are in the file system itself. The inode is the structure that contains the file metadata. In ext* file systems this counter is stored in the file system itself.

    You can verify how many links has a inode using ls -l. In addition, you can use ls -i to get the inode number of a file. E.g. try to multiply the links to a file using ln and verify that all links have the same inode number.

    andcoz@tseenfoo:~/refcount> ls -li
    total 40
    2248813 -rw-r--r-- 1 andcoz users 40960  7 feb 21.34 test
    andcoz@tseenfoo:~/refcount> ln test test2
    andcoz@tseenfoo:~/refcount> ln test test3
    andcoz@tseenfoo:~/refcount> ls -li
    total 120
    2248813 -rw-r--r-- 3 andcoz users 40960  7 feb 21.34 test
    2248813 -rw-r--r-- 3 andcoz users 40960  7 feb 21.34 test2
    2248813 -rw-r--r-- 3 andcoz users 40960  7 feb 21.34 test3
  2. The file descriptor reference counter counts how many times a file is open by a process or, more formally, how many file descriptors reference that inode. This information is stored in kernel memory.

    You can get an approximation of this value using fuser command. This command lists all the processes that have a file open. Note that a single process could open the same file multiple times, so fuser list size is less or, usually, equal to reference counter.

    andcoz@tseenfoo:~/refcount> tail -f test &
    [3] 4226
    andcoz@tseenfoo:~/refcount> fuser test
    /home/andcoz/refcount/test:  4226
    andcoz@tseenfoo:~/refcount> tail -f test2 &
    [4] 4354
    andcoz@tseenfoo:~/refcount> fuser test
    /home/andcoz/refcount/test:  4226  4354

A file is removed from the file system when both the counters are zero.

  • What happens if the computer crashes while the link count is zero but the reference counter is nonzero? Will the file be freed the next time the filesystem is mounted or only when fsck is run? – cg909 Feb 7 '17 at 21:51
  • 2
    I suppose it depends on file system implementation. ext2 will require an fsck to recover. I do not know how other file systems will recover. – andcoz Feb 7 '17 at 21:59
  • @andcoz Is there a name for the sum of the link count and the reference count? – UTF-8 Mar 9 '17 at 14:12
  • @UTF-8 As far I know, No, it doesn't exist. – andcoz Mar 10 '17 at 7:49

The file is freed when the link count goes to zero, i.e. the last directory entry is removed AND the last process with a file descriptor open on the file closes it either explicitly or implicitly by exiting. This is standard Unix semantics, and applies to all Linux file systems, including Ext, Ext 2, Ext 3 and Ext 4.

  • Can I check the link count of the file? Preferably without going through the open file table. – UTF-8 Feb 7 '17 at 20:22
  • Yes, the link count is what ls -l reports, what you called "reference count" above. To get how many processes have the file open is a bit trickier. – Johan Myréen Feb 7 '17 at 20:31
  • Please have a look at stat(1). – schaiba Feb 7 '17 at 20:31
  • @JohanMyréen Now you used the term "link count" to refer to the number of hard links a file has. In your answer you use it to describe the number of hard links a file has plus the number of open file descriptors it has. – UTF-8 Feb 7 '17 at 20:35
  • 1
    I'm sorry, I see now that my response was ambiguous. What I meant to say was that the link count is the number is the number of hard links to the file, but even if the link count is zero, the file is not freed until all processes have closed all the descriptors. – Johan Myréen Feb 7 '17 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.