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 18, 2010 5:54 PM in response to BobHarris

BobHarris wrote:

Nothing wrong with your approach, and as shown above it would be a bit more efficient, and most likely the few false positives are noise (unless the name was not very unique).

However, since the OP (GaryKing) has not checked in since his first post, we do not know any more details on his needs. Whether good enough is all he wants, or if accuracy is important.


Why would there be any false positives?
Are you referring to my 'shortcut' of a partial file name?
If so, only need to fgrep the full path and filename and only that file will be filtered.

Sep 18, 2010 6:04 PM in response to Tony T1


sudo time find / -type l -ls | fgrep "test.txt"
...
238.97 real 3.81 user 51.58 sys
sudo time find / -type l -exec test {} -ef "test.txt" ; -print
...
243.92 real 8.71 user 71.88 sys

5 seconds difference (with the -exec getting the benefit of any caching).
That comes out to about 2%. I say noise.

In my opinion, a 2% performance sacrifice for accuracy, is a small price to pay.
And even if the cache is not warmed up, I do not think it would take all that much longer
as my testing shows.

And again, all this discussion is academic, as we do not know what the OP (GaryKing) really needs.
But it has been a fun discussion, and different tricks and approaches have been presented.
...running another shell with 'test' can only be inefficient

Actually 'find' would NOT run a shell, it would directly fork and exec the test command.
And test itself is a small program (46K).

Sep 18, 2010 6:11 PM in response to Tony T1

Why would there be any false positives?
Are you referring to my 'shortcut' of a partial file name?
If so, only need to fgrep the full path and filename and only that file will be filtered.

Symbolic links do NOT need to contain full paths. The can contain relative paths.

For example:

ln -s ../../bin/fred.real my.symlink.name
ln -s /Users/harris/bin/fred.read my.other.symlink

Both could be working symlinks, but only "/bin/fred.real" is unique, which could exist somewhere else.

Also a symlink can point to another symlink and still get to the real file, you would not see that with a find -ls.

Less likely, but the real file might have an alternate hardlink name with some symlinks pointer to the alternate hardlink name, but it is really the same file

ln /Users/harris/bin/fred.read fred.hardlink
ln -s fred.hardlink my.alternate.symlink

The 90% case would most likely be unique. But the more unique infomation put into the grep, the greater chance a symlink could be missed. The less unique information the more likely a false positive.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

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