Pekka
Pekka

Reputation: 449385

Find symlinks to certain directory or one of its subdirs

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?

Upvotes: 5

Views: 4974

Answers (3)

Søren
Søren

Reputation: 23911

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/*"

Upvotes: 5

pixelbeat
pixelbeat

Reputation: 31708

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

Upvotes: 0

earl
earl

Reputation: 41755

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.

Upvotes: 10

Related Questions