Find symlinks to certain directory or one of its subdirs
Asked Answered
M

3

5

Is there an easy way to show whether there are any symlinks in a specified path pointing to a certain directory or one of its children?

Maiden answered 10/12, 2009 at 20:37 Comment(0)
B
10

A simple and fast approach, assuming that you have the target as absolute path (readlink(1) may help with that matter):

find $PATH -type l -xtype d -lname "$DIR*"

This finds all symlinks (-type l) below $PATH which link to a directory (-xtype d) with a name starting with $DIR.


Another approach, which is O(n*m) and therefore may take ages and two days:

find $DIR -type d | xargs -n1 find $PATH -lname

The first find lists $DIR and all its subdirectories which are then passed (xargs), one at a time (-n1), to a second find which looks for all symlinks originating below $PATH.


To sum things up: find(1) is your friend.

Beason answered 10/12, 2009 at 20:41 Comment(2)
find $PATH -type l -lname "$DIR*"Xylene
find $PATH -type l -lname "$DIR*" -exec ls -l {} \;Xylene
C
5

Following up on the answer given by earl:

-xtype does not work on Mac OSX, but can be safely omitted:

find $PATH -type l -lname "$DIR*"

Example:

find ~/ -type l -lname "~/my/sub/folder/*"
Corvine answered 4/1, 2016 at 10:44 Comment(1)
-xtype is used to find symbolic links when -L (old days -follow) is on. -xtype is afaik is a GNU extension. And you are correct. It can be safely omitted since -L is not used. One think though -type d will give directories only and -type l -type d will give symbolic links to directories only.Angkor
A
0

Have a look at the findbl (bad links) script in fslint. It might give you some hints: http://code.google.com/p/fslint/source/browse/trunk/fslint/findbl

Ade answered 10/12, 2009 at 21:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.