Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

how can i find top 10 files in unix with most links to it?

share|improve this question
What links are you talking about? Hard links, symlinks, html-links? – innaM Dec 8 '09 at 11:07

This seems to work:

$ find . -type l -exec readlink {} \; | sort | uniq -c | sort -r | head

If you want the number of files shown to be other than 10, then just add the number you want as an argument to head.

This will search for symlinks below the current directory, to search over another directory, replace the . with the path you want.

share|improve this answer
Needs to be find . -type l etc, or whatever directory you want to search in such as find / -type l – Rich Bradshaw Dec 8 '09 at 15:06
More efficient is -printf '%l\n' to print the link target without having to run a process for each file. And should the second sort be -nr, for numeric sort? – Peter Cordes Dec 10 '09 at 7:01
Just realized this doesn't really work unless all the links are absolute. If one link target is foo/bar, and another symlink to the same file is /home/peter/foo/bar, those won't get counted the same. – Peter Cordes Dec 10 '09 at 7:02

find -type f -ls | sort -rn -k 4 | head -10

Output will be sorted in order of decreasing link count. (-r = reverse, -n = numeric sort, -k 4 = sort on column 4)

If you want symlinks, then you'll have to look at the other answers. I answered the only interpretation that has an easy answer. :P

share|improve this answer

I don't believe there's anything about a file which tells you how many links there are to it.

If I'm right, you'll have to search your directory tree for links and sort them by the file they link to.

The find command can list links with -type l but unless you use find's special handling of links, you don't see what the link resolves to.

So try:

sudo find -L / -type l -exec ls {} \;

to list links. Then redirect the output to sort, uniq and so on.

I haven't tested this (it's not complete, anyway): I'm not sure if I have any duplicated links to find in my filesystem which would make a test meaningful.

share|improve this answer
This is all about symbolic links. ls -l will show you the number of hard links. – innaM Dec 8 '09 at 12:46
-exec ls {} will show the link but not what it's linked to. You can use -ls or -exec ls -l {} to see the destination. – Dennis Williamson Dec 8 '09 at 12:58
Also, -L is going to resolve links so type -l won't find any. – Dennis Williamson Dec 8 '09 at 13:03
As I said, I didn't try it. The idea was to say 'the answer may be over there...' – pavium Dec 8 '09 at 22:18
-L -type l is the way to find broken symlinks, BTW. – Peter Cordes Dec 10 '09 at 10:18

You must log in to answer this question.