75

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?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
nickf
  • 537,072
  • 198
  • 649
  • 721

9 Answers9

90

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.

skymt
  • 3,199
  • 21
  • 13
  • 4
    thanks, that worked well. for anyone else trying this one: using the -maxdepth switch really helped speed up the search, since i knew at what depth the links would be. – nickf Sep 19 '08 at 08:25
  • 1
    Thanks. Just be careful that presence of a trailing slash or not in the link may alter result. So you may need a `find . -lname '*folder?'` – regilero Jul 02 '13 at 09:30
  • ... and for each match, find also what points to that match ! (ex: /a/A -> /some/b -> /particular/folder ... the chain could be even longer) – Olivier Dulac Nov 07 '13 at 17:33
7

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.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
JJK
  • 71
  • 1
  • 1
6

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.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
5
find . -type l -printf '%p -> %l\n'
LaurentG
  • 11,128
  • 9
  • 51
  • 66
no1uknow
  • 51
  • 1
  • 1
2

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
stephanea
  • 1,108
  • 8
  • 10
1
find / -lname 'fullyqualifiedpathoffile'
Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
bfabry
  • 1,874
  • 1
  • 13
  • 21
1

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • This would work well for finding all hardlinks to a file. But hardlinks to a directory are a big no-no. The filesystem driver will not let you create such a thing, and if you are masochistic enough to go in and edit the raw partition by hand to create one, it makes many things very unhappy. – Thomee Sep 19 '08 at 16:41
  • I don't recommend hard links to folders either, but TimeMachine on OS X uses them. E.G: http://belkadan.com/blog/2011/07/rm-vs-Time-Machine/ – Tom Andersen Oct 29 '12 at 17:06
1
find /foldername -type l -exec ls -lad {} \;
LaurentG
  • 11,128
  • 9
  • 51
  • 66
Lunar Mushrooms
  • 8,358
  • 18
  • 66
  • 88
  • This works great on the Mac. Thanks. Found some malicious symlinks on a site that was done by a wordpress hack. really. thanks. – Vik Sep 24 '14 at 12:30
0

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).

Thomas Tempelmann
  • 11,045
  • 8
  • 74
  • 149