Find all symbolic links pointing to a file
iMac, Mac OS X (10.6.4), 3.2 GHz Intel Core i3, 4 GB RAM
Apple Event: May 7th at 7 am PT
iMac, Mac OS X (10.6.4), 3.2 GHz Intel Core i3, 4 GB RAM
Actually, running an exec and test for each link does not seem efficient at all.
time find $HOME -type l -exec test {} -ef /path/to/filename ; -print
real 1m18.780s
user 0m2.802s
sys 0m13.433s
time find $HOME -type l -ls | fgrep filename
real 1m12.950s
user 0m1.906s
sys 0m9.517s
time perl -e '...' # actually script posted above
real 2m16.701s
user 0m13.912s
sys 0m56.181s
find / -type l -ls | fgrep "filename"
is not only more efficient, but will list partial filenames (why test for "Very Long Path To/A Very Long File Created By Tony.txt" when an fgrep for "Tony" will do
MacBook:~ Tony$ sudo time find / -type l -ls | fgrep "test.txt"
Password:
find: /dev/fd/3: Not a directory
find: /dev/fd/4: Not a directory
10473:6413779 8 lrwxr-xr-x 1 Tony staff 8 Sep 18 20:29 /Users/Tony/test2.txt -> test.txt
238.97 real 3.81 user 51.58 sys
MacBook:~ Tony$ sudo time find / -type l -exec test {} -ef "test.txt" ; -print
Password:
find: /dev/fd/3: Not a directory
find: /dev/fd/4: Not a directory
/Users/Tony/test2.txt
243.92 real 8.71 user 71.88 sys
MacBook:~ Tony$
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.
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
...running another shell with 'test' can only be inefficient
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.
ln -s ../../bin/fred.real my.symlink.name
ln -s /Users/harris/bin/fred.read my.other.symlink
ln /Users/harris/bin/fred.read fred.hardlink
ln -s fred.hardlink my.alternate.symlink
BobHarris wrote:
Also a symlink can point to another symlink and still get to the real file, you would not see that with a find -ls.
Find all symbolic links pointing to a file