Coder Perfect

Is there a method to see if a directory has any symbolic links to it?

Problem

On my server, I have a folder to which a number of symbolic links lead. I’ve since built a new folder, and I’d like to redirect all of those symbolic links to it. I considered creating a symlink from the original folder to the new folder, but it appears that if I did so, things would get extremely messy very quickly.

I’ve been manually updating the symlinks to point to the new location, but I may have overlooked a few.

Is there a method to see if there are any symlinks that refer to a specific folder?

Asked by nickf

Solution #1

I’d use the find command to find what I’m looking for.

find . -lname /particular/folder

This will recursively look for symlinks to /particular/folder in the current directory. It’s worth noting that it’ll only look for absolute symlinks. A similar command may be used to find all symlinks linking to “folder” objects:

find . -lname '*folder'

Then you’d have to filter out any false positives.

Answered by skymt

Solution #2

The symlinks program, written by Mark Lord, can be used to audit symlinks. It will scan an entire filesystem, normalize symlink paths to absolute form, and print them to stdout.

Answered by JJK

Solution #3

There isn’t a straightforward approach to look for such symlinks. Consider the possibility that you have a filesystem that isn’t always mounted (e.g., an external USB drive) that contains symlinks to another volume on your system.

You might be able to make use of:

for a in `find / -type l`; do echo "$a -> `readlink $a`"; done | grep destfolder

The -lname option is not supported by FreeBSD’s search, which is why I ended up with the above.

Answered by Greg Hewgill

Solution #4

find . -type l -printf '%p -> %l\n'

Answered by no1uknow

Solution #5

I don’t believe it is possible, aside than checking all other directories for links to the original folder. If that’s the case, I’d be intrigued.

Answered by stephanea

Post is based on https://stackoverflow.com/questions/100170/is-there-a-way-to-check-if-there-are-symbolic-links-pointing-to-a-directory