56

I had created many symbolic links on various paths for a particular file or a directory. I want the whole list of created symbolic links paths (location).

Example:

I created symbolic links for ~/Pictures directory on many directories. How do I list all the symlinks to that ~/Pictures directory?

Is that possible? If yes, then how?

Avinash Raj
  • 80,446

4 Answers4

48

Here is an example:

find -L /dir/to/start -xtype l -samefile ~/Pictures

or, maybe better:

find -L /dir/to/start -xtype l -samefile ~/Pictures 2>/dev/null

to get rid of some errors like Permission denied, Too many levels of symbolic links, or File system loop detected which find throws them when doesn't have the right permissions or other situations.

  • -L - Follow symbolic links.

  • -xtype l - File is symbolic link

  • -samefile name - File refers to the same inode as name. When -L is in effect, this can include symbolic links.

Notes:

  • Use lowercase L in -xtype l, not the digit 1.
  • On macOS / Darwin, -xtype is -type.
wjandrea
  • 14,504
Radu Rădeanu
  • 174,089
  • 51
  • 332
  • 407
8

Very simple, use option -lname:

find / -lname /path/to/original/dir

From man find:

-lname pattern
       File is a symbolic link whose contents match shell pattern pattern.  The
       metacharacters do not treat `/' or `.' specially.  If the -L option or the
       -follow option is in effect, this test returns false unless the symbolic link
       is broken.

Note: Remember that symbolic links could be anywhere, which includes a remote system (if you’re sharing files), so you may not be able to locate them all.

wjandrea
  • 14,504
2

Try this :

ls -i ~/

277566 Pictures

find . -follow -inum 277566 ( find directories with the same inode number )

It will display all its symbolic links paths .

nux
  • 39,152
0

I like this one-liner the most:

find . -maxdepth 1 -type l -exec readlink -f '{}' \;

refs:

https://unix.stackexchange.com/questions/22128/how-to-get-full-path-of-original-file-of-a-soft-symbolic-link

https://unix.stackexchange.com/questions/21984/list-symlinks-in-current-directory

B.Kocis
  • 173