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

I have a folder on my server where I pointed out some symbolic links. Since then I have created a new folder, and I want to change all of these symbolic links to point to the new folder. I thought about replacing the original folder with a symbolic link to the new folder, but it seems that if I continued this practice, it could become very dirty very quickly.

What I did was manually change the symbolic links to point to a new folder, but I might have missed a couple.

Is there a way to check if there are any symbolic links pointing to a specific folder?

+60
linux symlink
Sep 19 '08 at 7:08
source share
9 answers

I would use the find command.

find . -lname /particular/folder 

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

 find . -lname '*folder' 

From there, you will need to weed out any false positives.

+76
Sep 19 '08 at 7:15
source share

You can check symbolic links using the symlinks program written by Mark Lord - it will scan the entire file system, normalize the symbolic link paths to the absolute form and print them in standard mode.

+7
Mar 13 2018-12-12T00:
source share

There is really no direct way to check for such symbolic links. Note that you may have a file system that is not permanently mounted (for example, an external USB drive), which may contain symbolic links to another volume in the system.

You can do something with:

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

I note that FreeBSD find does not support the -lname parameter, so I ended up above.

+5
Sep 19 '08 at 7:19
source share
 find . -type l -printf '%p -> %l\n' 
+3
08 Oct 2018-10-10
source share

Besides viewing all the other folders, if there are links pointing to the source folder, I don't think it is possible. If so, I would be interested.

+2
Sep 19 '08 at 7:19
source share
 find / -lname 'fullyqualifiedpathoffile' 
+1
Sep 19 '08 at 7:16
source share

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

Then a find with -inum will find all common hard links.

For programmatic links, you may need ls -l for all files that search for text after "->" and normalize it to make sure this is the absolute path.

+1
Sep 19 '08 at 7:17
source share
 find /foldername -type l -exec ls -lad {} \; 
+1
Jan 03 '14 at 12:50
source share

Great blog right here! I really enjoyed your article! Thank you very much! games

-four
Jul 09 '19 at 7:34
source share



All Articles