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

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

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

1 Answer

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.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like