Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find all files which are basically soft or hard links of other directories or files on linux?

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?

like image 649
DKSRathore Avatar asked Nov 27 '09 19:11

DKSRathore


People also ask

How do I see all hard links in Linux?

You can learn a file's inode number and the number of links to it by running ' ls -li ', ' stat ' or ' find -ls '. You can search for hard links to inode number NUM by using ' -inum NUM '.

How do I find soft link and hard link in Unix?

If you find two files with identical properties but are unsure if they are hard-linked, use the ls -i command to view the inode number. Files that are hard-linked together share the same inode number. The shared inode number is 2730074, meaning these files are identical data.

How do I find all soft links in Linux?

Showing soft link using Find command in UnixWhen you use the find command with option type and specify the type as small L ( l for the link), it displays all soft links in the specified path.


1 Answers

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 \! -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

Alternatively, on newer versions of find you could use:

% find . -type f -links +1

This works for the same reason as above; however, newer versions of find can take +n or -n instead of just a number. This is equivalent to testing for greater than n or less than n, respectively.

like image 195
Alnitak Avatar answered Sep 28 '22 05:09

Alnitak