views:

500

answers:

2

How could I get the list of all linked files on my system or from a certain directory. I used to create links but they became unmanageable with time. I want the list of all such links from a directory. Can anyone help?

+2  A: 

Finding symlinks is easy:

% find . -type l

Finding hard links is tricky, because if a subdirectory of the directory in question also has subdirectories then those increase the hard link count. That's how subdirectories are linked to their parents in UNIX (it's the .. entry in each subdirectory).

If you only want to find linked files (and not directories), this will work:

% find . -type f -a \! -links 1

This works because a file that does have hard links will have a link count > 1, and unlinked file has a link count == 1, hence this command looks for all files whose link count <> 1

Alnitak
Wao, this was such a small command. Thanks Alnitak
DKSRathore
+1  A: 

@OP, If you have GNU find, you can find hard links using -printf "%n" eg

find /path -type f -printf "%f/%n/%i\n" | while IFS="/" read filename num_hlinks inum
do
 echo "Filename: $filename. Number of hard links: $num_hlinks, inode: $inum"
 # if 2 or more files have the same inode number, then they are hard links. 
 # you can therefore count how many $inum that are the same and determine those hard links, which 
 # you have to try doing yourself.
done