How to get all the hard links that link to the same file quickly?

Leo "TheHobbit" Cacciari leothehobbit at gmail.com
Sat Apr 23 21:41:09 UTC 2011


Il 04/23/2011 02:40 PM, Peng Yu ha scritto:
> Hi,
> 
> I'm looking for a fast way to get all the hard links that point to the
> same file.
> 
> file="/blah/blah"
> find $(stat -c %m "$file") -inum $(stat -c %i "$file")
> 
> Currently, I have a slow way. It traverses the mount point that the
> file is on and look for all the files that have the same inum. But
> traversing the whole file system is a slow operation. Does the linux
> OS keep a table somewhere all the paths pointing to any file or the
> linux OS only keep the number of the paths that point to a given inum
> but does not keep the actual paths?
> 
The short answer is no, you can't get what you want in any other way.
The problem is that the kernel does not know anything about paths. A
directory is actually a file, containing the mapping between names and
inodes. When you ask the kernel to open a file given the path the kernel
walks the directory structure and found the inode associated to the
path. It is this inode it opens. Thus, when you do something like

$ ln /foo/bar/foobar.txt buzz.txt

the kernel found the inode corresponding to /foo/bar/foobar.txt, say
123, then adds an entry in the file representing the current directory.
This entry says that buzz.txt corresponds to inode 123. That's all is
needed, and all you have.

This means that you _must_ traverse the whole file system to found all
the files having the same inode that a given file. Sure, there are good
ways to do it, and there are that are not so good. But traverse you need
to do and traverse you will :)

Hoping it helps

-- 
Leo "TheHobbit" Cacciari
Aliae nationes servitutem pati possunt populi romani est propria libertas




More information about the ubuntu-users mailing list