How to find all symbolic links to a given file/directory?

7

1

How can you find all symbolic links to a given file / directory?

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

warren

Posted 2010-01-20T20:16:39.787

Reputation: 8 599

Answers

5

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.

whitequark

Posted 2010-01-20T20:16:39.787

Reputation: 14 146

Is it possible to do this with busybox find? It doesn't seem to have -samefile or -lname. – Gavin S. Yancey – 2016-07-20T21:21:33.947

@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 – 2016-07-21T04:36:09.800