Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Find all symbolic links pointing to a file

How do I find all symbolic links pointing to a file?

iMac, Mac OS X (10.6.4), 3.2 GHz Intel Core i3, 4 GB RAM

Posted on Sep 16, 2010 5:15 PM

Reply
22 replies

Sep 16, 2010 8:28 PM in response to GaryKing

If you want to install the GNU 'find' command, you could use:

find -L / -samefile /path/to/real_file

You can install the GNU 'find' command via <http://www.gnu.org/software/findutils/>, or fink.sf.net findutils, or MacPorts.org findutils.

Note, searching from the root directory / through all the files you have will take lots of time.

Sep 16, 2010 8:43 PM in response to GaryKing

If you do not want to install the GNU 'find' command, here is a script that, while even slower than the 'find' command should also locate all symlinks that point to a specific file:

#!/usr/bin/env bash
if [[ $# != 2 ]]; then
echo 1>&2 "Usage: ${0##*/} /starting_directory /path/to/reference_file"
exit 1
fi
START="$1"
export REFFILE="$2"
find $START -type l | perl -ne '
if ( ! $initialized ) {
$initialized = 1;
$reffile = $ENV{"REFFILE"};
@ref = stat($reffile);
}
chomp;
@st = stat($_);
if ( $st[0] == $ref[0] && $st[1] == $ref[1] ) {
print `ls -l "$_"`;
}
'

Sep 16, 2010 8:52 PM in response to GaryKing

Without having to install the GNU 'find' command, then from an admin-privileged login,

sudo -s
find / -type l -print0 | xargs -0 ls -TalkOs | sudo grep ' ->' | grep {filenameOfRealFile}

use just enough of the filename, to which any/all links point, to uniquely identify it. That is, I wouldn't include the filepath because, for example, links in the same directory won't display with the arrow followed by path.

As an example, on my computer I have a link in /etc/periodic/weekly that points to the real executable in /usr/local. When I type the above command to search for links, I get

iMac:jv imac$ sudo -s
Password:
iMac:jv root# find / -type l -print0 | xargs -0 ls -TalkOs | grep ' -> ' | grep diskPM
4 lrwxr-xr-x 1 root wheel - 17 Jul 12 21:42:30 2010 /private/etc/periodic/weekly/600.diskPM -> /usr/local/diskPM


the find -type l looks for links, the -print0 and xargs -0 handle white space in file names, and the two greps filter out everything but the real file name for which you are searching for its links.

Message was edited by: j.v.

....or, I guess you could run BobHarris' script.

Message was edited by: j.v.

Sep 16, 2010 9:02 PM in response to BobHarris

OK, I was having fun. So here is a different implementation that moves the 'find' inside of the perl script:

#!/usr/bin/env bash
if [[ $# != 2 ]]; then
echo 1>&2 "Usage: ${0##*/} /starting_directory /path/to/reference_file"
exit 1
fi
START="$1"
REFFILE="$2"
perl -e '
use File::Find;
@ref = stat($ARGV[1]);
find sub {
@st = stat($File::Find::name);
if ( $st[0] == $ref[0] && $st[1] == $ref[1] ) {
print `ls -l "$_"`;
}
}, $ARGV[0];
' "$START" "$REFFILE"

Sep 17, 2010 3:56 PM in response to Tony T1

I think that this is equivalent to

find / -type l -ls | grep ' -> ' | grep diskPM

If you want to optimize, then why 2 grep's?

sudo find / -type l -ls | grep ' -> .*diskPM'

But j.v. still had more straight forward solution, and easier to understand then my install GNU 'find', or use perl (but I had fun writing the perl 🙂 ).

Sep 17, 2010 4:03 PM in response to BobHarris

If you want to optimize, then why 2 grep's?

Just using j.v.'s example

But j.v. still had more straight forward solution, and easier to understand then my install GNU 'find', or use perl (but I had fun writing the perl 🙂 ).


Well, the OP wanted to "find all symbolic links pointing to a file", so

sudo find / -type l -ls

is the most straight-forward solution 🙂

Sep 17, 2010 8:27 PM in response to Tony T1

well yeah, I guess I interpreted his problem as wanting to find all symlinks pointing to a specific file of interest, hence all the added gobbledygook, rather than just find every instance of a symbolic link resident on his hard drive (otherwise, his subject line might have only been "Find all symbolic links").

And you're right, I guess I could do away with one of the greps ( +| grep ' -> '+ )

Sep 18, 2010 1:33 PM in response to corbaguy

The approaches that use pipes will break on names containing whitespace, whereas find's -exec switch will handle them correctly.

Actually j.v.'s version which used -print0 and xargs -0 would not break. The -print0 passes the filenames nul terminated, and xargs -0 accepts nul terminated file names and passes them to the command being invoked as a single ARGV argument.

Also the ones that just do "find | grep" do not really care about spaces in paths.

That being said, your approach using 'test -ef' combines the simplicity of a command line solution and getting the accuracy of my perl solution. Very well done.

At the very least, I think the OP (GaryKing) should award you a "Helpful" star, and since I think yours is the shortest, straight forward and accurate, if I were the OP, I would give you the "Solved" award.

Find all symbolic links pointing to a file

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.