Hallo all, I need to do this in linux:
- Given: file name 'foo.txt'
- Find: all files that are symbolic links to 'foo.txt'
How to do it? Thanks!
Hallo all, I need to do this in linux:
How to do it? Thanks!
It depends, if you are trying to find links to a specific file that is called foo.txt,
then this is the only good way:
find -L / -samefile path/to/foo.txt
On the other hand, if you are just trying to find links to any file that happens to be named foo.txt
, then something like
find / -lname foo.txt
or
find . -lname \*foo.txt # ignore leading pathname components
-xtype l
to have find only list symlinks –
Embranchment find . -lname '*foo.dir*'
(matches e.g. file.txt -> ../foo.dir/file.txt
) –
Redvers find -L /usr -samefile /usr/share/pyshared/lsb_release.py 2>/dev/null | xargs ls -al
–
Antinucleon Find the inode number of the file and then search for all files with the same inode number:
$ ls -i foo.txt
41525360 foo.txt
$ find . -follow -inum 41525360
Alternatively, try the lname
option of find
, but this won't work if you have relative symlinks e.g. a -> ../foo.txt
$ find . -lname /path/to/foo.txt
-samefile
option with -L
for the same effect, without having to look up the inode yourself –
Embranchment foo
is a directory, use ln -di
, in one line: find . -follow -inum $(ls -di foo.txt |cut -d" " -f1)
–
Giraldo I prefer to use the symlinks
utility, which also is handy when searching for broken symlinks. Install by:
sudo apt install symlinks
Show all symlinks in current folder and subfolders:
symlinks -rv .
-r
: recursive-v
: verbose (show all symlinks, not only broken ones)To find a specific symlink, just grep
:
symlinks -rv . | grep foo.txt
symlinks
does not reliably (AFAIK) search across what is calls "different filesystems"). Also, symlinks -rv . 2>/dev/null | grep foo.txt
may result in "cleaner" output... –
Foamflower © 2022 - 2024 — McMap. All rights reserved.