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

Peng Yu pengyu.ut at gmail.com
Mon Apr 25 01:09:30 UTC 2011


On Sat, Apr 23, 2011 at 4:41 PM, Leo "TheHobbit" Cacciari
<leothehobbit at gmail.com> wrote:
> 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 :)

OK. Is there any OS other than linux that can get all paths pointing
to the same file quickly?

Also, is there a library that can help keep a database that bookkeeps
all the hardlinks and symbolic links so that I don't have always
search for the whole file system to find all the hard links and
symbolic links that point to the same file?

-- 
Regards,
Peng




More information about the ubuntu-users mailing list