Is there a way to check if there are symbolic links pointing to a directory?

LinuxSymlink

Linux Problem Overview


I have a folder on my server to which I had a number of symbolic links pointing. I've since created a new folder and I want to change all those symbolic links to point to the new folder. I'd considered replacing the original folder with a symlink to the new folder, but it seems that if I continued with that practice it could get very messy very fast.

What I've been doing is manually changing the symlinks to point to the new folder, but I may have missed a couple.

Is there a way to check if there are any symlinks pointing to a particular folder?

Linux Solutions


Solution 1 - Linux

I'd use the find command.

find . -lname /particular/folder

That will recursively search the current directory for symlinks to /particular/folder. Note that it will only find absolute symlinks. A similar command can be used to search for all symlinks pointing at objects called "folder":

find . -lname '*folder'

From there you would need to weed out any false positives.

Solution 2 - Linux

You can audit symlinks with the symlinks program written by Mark Lord -- it will scan an entire filesystem, normalize symlink paths to absolute form and print them to stdout.

Solution 3 - Linux

There isn't really any direct way to check for such symlinks. Consider that you might have a filesystem that isn't mounted all the time (eg. an external USB drive), which could contain symlinks to another volume on the system.

You could do something with:

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

I note that FreeBSD's find does not support the -lname option, which is why I ended up with the above.

Solution 4 - Linux

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

Solution 5 - Linux

Apart from looking at all other folders if there are links pointing to the original folder, I don't think it is possible. If it is, I would be interested.

Solution 6 - Linux

find / -lname 'fullyqualifiedpathoffile'

Solution 7 - Linux

For hardlinks, you can get the inode of your directory with one of the "ls" options (-i, I think).

Then a find with -inum will locate all common hardlinks.

For softlinks, you may have to do an ls -l on all files looking for the text after "->" and normalizing it to make sure it's an absolute path.

Solution 8 - Linux

find /foldername -type l -exec ls -lad {} \;

Solution 9 - Linux

To any programmers looking here (cmdline tool questions probably should instead go to unix.stackexchange.com nowadays):

You should know that the Linux/BSD function fts_open() gives you an easy-to-use iterator for traversing all sub directory contents while also detecting such symlink recursions.

Most command line tools use this function to handle this case for them. Those that don't often have trouble with symlink recursions because doing this "by hand" is difficult (any anyone being aware of it should just use the above function instead).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnickfView Question on Stackoverflow
Solution 1 - LinuxskymtView Answer on Stackoverflow
Solution 2 - LinuxJJKView Answer on Stackoverflow
Solution 3 - LinuxGreg HewgillView Answer on Stackoverflow
Solution 4 - Linuxno1uknowView Answer on Stackoverflow
Solution 5 - LinuxstephaneaView Answer on Stackoverflow
Solution 6 - LinuxbfabryView Answer on Stackoverflow
Solution 7 - LinuxpaxdiabloView Answer on Stackoverflow
Solution 8 - LinuxLunar MushroomsView Answer on Stackoverflow
Solution 9 - LinuxThomas TempelmannView Answer on Stackoverflow