139

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!

skaffman
  • 398,947
  • 96
  • 818
  • 769
lukmac
  • 4,617
  • 8
  • 33
  • 34
  • possible duplicate of [Linux: Find all symlinks of a given 'original' file? (reverse 'readlink')](http://stackoverflow.com/questions/4532241/linux-find-all-symlinks-of-a-given-original-file-reverse-readlink) – Vality Aug 01 '14 at 09:59

3 Answers3

166

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
Adam K Dean
  • 7,387
  • 10
  • 47
  • 68
DigitalRoss
  • 143,651
  • 25
  • 248
  • 329
  • problem solved beautifully. Thanks to all people that replied. – lukmac May 31 '11 at 09:08
  • 9
    As I commented on a deleted answer, you can use `-xtype l` to have find only list symlinks – Hasturkun May 31 '11 at 09:08
  • you may want to recurse, as any of the files pointing to foo.txt may themselves be pointed-at by some other links... ex: A->B->foo.txt, /tmp/C->B->foo.txt, etc. – Olivier Dulac Nov 07 '13 at 17:31
  • this can work too if your name component is a parent directory named in the link, by searching `find . -lname '*foo.dir*'` (matches e.g. `file.txt -> ../foo.dir/file.txt`) – Yonatan Jun 07 '17 at 14:48
  • This answer is really helpful, thanks so much. By the way, if we want to get more info about the results of "find", we can pass the output of "find" to "ls": `find -L /usr -samefile /usr/share/pyshared/lsb_release.py 2>/dev/null | xargs ls -al` – MadHatter Nov 25 '22 at 06:21
25

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
dogbane
  • 266,786
  • 75
  • 396
  • 414
  • 4
    If using a recent version of GNU find, you can also use the `-samefile` option with `-L` for the same effect, without having to look up the inode yourself – Hasturkun May 31 '11 at 08:45
  • 5
    But this will also find files on other file systems that happen to have the same inode number. – DigitalRoss May 31 '11 at 08:50
  • If `foo` is a directory, use `ln -di`, in one line: `find . -follow -inum $(ls -di foo.txt |cut -d" " -f1)` – rubo77 Aug 07 '16 at 05:17
  • 1
    This answer is wrong. A symlink doesn't have the same inode as its target. This will only work for a file inside a symlinked folder. But this is not what was asked. – Jules Lamur Jan 05 '18 at 22:34
9

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
Nik
  • 2,902
  • 2
  • 16
  • 11
  • 1
    Never heard of [symlinks](https://linux.die.net/man/8/symlinks) before ([repo](https://github.com/brandt/symlinks)). 'A useful utility for maintainers of FTP sites, CDROMs, and Linux software distributions. It scans directories for symbolic links and lists them on stdout, often revealing flaws in the filesystem tree.' Great little ancient tool by ancient kernel hacker [Mark Lord](https://github.com/torvalds/linux/search?p=2&q=Mark+Lord), the 'original developer and maintainer of the IDE Performance Package for linux, the Linux IDE Driver subsystem, hdparm', now maintained by J. Brandt Buckley. – cachius Nov 27 '20 at 15:08
  • Works great and is fast. Note, however, that you'll need to do a search in each discrete partition of interest (as `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... – Digger Nov 23 '21 at 21:12