1833704 Members
3433 Online
110062 Solutions
New Discussion

Finding hardlinked file.

 
SOLVED
Go to solution
echong
Regular Advisor

Finding hardlinked file.


I want to list all files in a directory that is hardlinked to any other files anywhere in my system. Is there a simple command to do that besides writing a script?

Thanks.
4 REPLIES 4
Antoanetta Naghiu
Esteemed Contributor

Re: Finding hardlinked file.

find / -name -exec ls -F |grep @ {} ;
(see man ls, F option).
Anthony deRito
Respected Contributor

Re: Finding hardlinked file.

Eric, i can give you what I know. The difference between the listing of a hard link vs a soft link is in the second field of a long list. The second field shows you the number of links if any.

For example:

#touch /tmp/test
#ll /tmp/test
-rw-rw-r-- 1 root sys 0 Aug 21 15:26 test

#ln /tmp/test /tmp/test5
#ll /tmp/test*
-rw-rw-r-- 2 root sys 0 Aug 21 15:26 test
-rw-rw-r-- 2 root sys 0 Aug 21 15:26 test5

Notice field 2 changed from 1 to 2. You know that a hard link has been created and there are now 2 filenames pionting to the same data.

I won't get into using this info in a script but just thought I would share it.

Tony

RikTytgat
Honored Contributor
Solution

Re: Finding hardlinked file.

Hi,

Hard linked files must always be on the same file system.

First, determine the inode number of the file for which you want to determine the hard linked files:

ls -i your_filename

The inode is listed.

Then, use find to find all files having that same inode number:

find /your/directory -xdev -inum your_inode

That should do the trick.

Bye,
Rik.
Wodisch
Honored Contributor

Re: Finding hardlinked file.

Eric,
how about something like:

dev=`devnm .| cut -d' ' -f1`
ls -i1 * | while read inum name ; do echo $name; ncheck -a -i $inum $dev ";"; done

that should find all hardlinks of all files in your working directory.
HTH,
Wodisch