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 you find all symbolic links to a given file / directory?

This previous question only applies to hardlinks (if I read correctly).

share|improve this question
up vote 5 down vote accepted

Use find /dir -lname /link/target. It searches link contents with shell pattern; e.g. you can use * and ? wildcards in target specification.

One drawback of this method is that it searches contents of links, not their expanded paths, so if you need to find relative links to absolute paths (if e.g. there are more than one file with same name) you need a more complicated script.

Also you can use other apporoach: find -L /dir -samefile /link/target. This causes find to dereference symlinks and after check the dereferenced path that is expanded by OS to be same as provided, so both relative and absolute links will be handled by Linux. This solves problem of previous method.

share|improve this answer
awesome, thanks! – warren Jan 20 '10 at 22:37
Is it possible to do this with busybox find? It doesn't seem to have -samefile or -lname. – g.rocket Jul 20 at 21:21
@g.rocket It's harder. You will need to use something like find ... -exec sh -c 'stat -c %N | egrep '-> yourlink$', replacing yourlink with the link you're looking for. Also, much, much slower. – whitequark Jul 21 at 4:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .