Finding hard links 

Newsgroups:  gmane.linux.debian.user
Date:        Tue, 17 Oct 2006 22:52:25 +0100

On Tue, 17 Oct 2006 15:46:46 -0700, Bob McGowan wrote:

>> Is it possible to find the hard links of the same file? Ie, group the
>> above finding into same file groups?
>
> find . -type f -links +1 -ls | sort -n -k 1
>
> This command line will [...]

Bingo! thanks a lot.

Finding hard links 

> How can I find hard linked files?

All "regular" files are hard links. See http://en.wikipedia.org/wiki/Hard_link

the stat(1) command tells you how many files point at a given inode (so, if "Links:" > 1, you have two filenames and one file):

$ touch a
$ ln a b
$ stat a
  File: `a'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fe01h/65025d    Inode: 1287087     Links: 2
Access: (0644/-rw-r--r--)  Uid: ( 1000/     jon)   Gid: ( 1000/     jon)
Access: 2006-10-17 22:47:53.000000000 +0100
Modify: 2006-10-17 22:47:53.000000000 +0100
Change: 2006-10-17 22:47:54.000000000 +0100
> Is it possible to find the hard links of the same file?
> Ie, group the above finding into same file groups?

Parse the above (if you are writing a shellscript) or use the equivalent system call in C: stat(2)

Jon Dowland

Finding hard links 

> How can I find hard linked files?

using for example:

[ "`stat -c %h filename`" -gt 1 ]  && echo hard linked
> Is it possible to find the hard links of the same file? Ie, group the
> above finding into same file groups?

AFAIK it's not possible using general purpose tool. Maybe some filesystem offer a specific interface to get the file name(s) given the inode number (as ncheck on aix), but I don't have made any deep investigation.

Usually general purpose tools (tar, rsync) use an hash table and repeat the check for every file.

You could make your own simple map using this expensive command

find /somepath -xdev -type f -printf '%n %i% %p\n' | grep -v '^1 ' | sort -k2

Finding hard links 

> Is it possible to find the hard links of the same file? Ie, group the
> above finding into same file groups?
find . -type f -links +1 -ls | sort -n -k 1

This command line will find all regular files (-type f) that have 2 or more hard links (-links +1) and list them (-ls, format similar to ls -l, except that it includes the inode number in column one). The result is piped to a numeric sort on column one.

Bob McGowan