Find all symbolic links pointing to a file
iMac, Mac OS X (10.6.4), 3.2 GHz Intel Core i3, 4 GB RAM
iMac, Mac OS X (10.6.4), 3.2 GHz Intel Core i3, 4 GB RAM
find -L / -samefile /path/to/real_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 "$_"`;
}
'
sudo -s
find / -type l -print0 | xargs -0 ls -TalkOs | sudo grep ' ->' | grep {filenameOfRealFile}
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
#!/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"
j.v. wrote:
find / -type l -print0 | xargs -0 ls -TalkOs | grep ' -> ' | grep diskPM
find / -type l -ls | grep ' -> ' | grep diskPM
find / -type l -ls
I think that this is equivalent to
find / -type l -ls | grep ' -> ' | grep diskPM
sudo find / -type l -ls | grep ' -> .*diskPM'
If you want to optimize, then why 2 grep's?
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 🙂 ).
sudo find / -type l -ls
corbaguy wrote:
From the command line:
find / -type l -exec test {} -ef /path/to/file \; -print
The approaches that use pipes will break on names containing whitespace, whereas find's -exec switch will handle them correctly.
find / -type l -ls | fgrep "filename"
Find all symbolic links pointing to a file