52

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?

  • You need to search exhaustive, there is no count stored like there is for hard-links. See one of the answers using find. – ctrl-alt-delor Mar 4 '14 at 12:33
41

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.
  • Could the command be modified to find symbolic link that contains a path? e.g. files across the system that may link to ~/Pictures/A, ~/Pictures/A/B/C, or any files in the subdirectories of ~/Pictures> – snowbound Jan 23 '15 at 12:30
7

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.

  • Note also, if the symlink is a relative path like ../dir then it won't find the absolute /path/to/original/dir you can instead use a pattern and weed out false positives -lname \*dir – Jason S Dec 2 '17 at 20:55
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 .

  • 4
    This will find hard links, not symbolic links. Hard links share inode numbers. symbolic links have different inode numbers. the inode of a symbolic link has a path instead of a block list. – hildred Mar 4 '14 at 17:14
  • the question didn't mention hard or soft symbolic link – nux Mar 4 '14 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.