Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find all of the symlinks in a directory tree?

Tags:

find

bash

symlink

I'm trying to find all of the symlinks within a directory tree for my website. I know that I can use find to do this but I can't figure out how to recursively check the directories.

I've tried this command:

find /var/www/ -type l

… and later I discovered that the contents in /var/www are symlinks, so I've changed the command to:

find -L /var/www/ -type l

it take a while to run, however I'm getting no matches.

How do I get this to check subdirectories?

like image 885
hafichuk Avatar asked Dec 14 '11 23:12

hafichuk


People also ask

How do I find all soft links in Linux?

Showing soft link using Find command in UnixWhen you use the find command with option type and specify the type as small L ( l for the link), it displays all soft links in the specified path.

How do I find all symlinks in Windows?

In Command Prompt, run this command: dir /AL /S c:\ A list of all of the symbolic links in the c:\ directory will be returned.

Where are the symlinks stored?

program directory in a file manager, it will appear to contain the files inside /mnt/partition/. program. In addition to “symbolic links”, also known as “soft links”, you can instead create a “hard link”. A symbolic or soft link points to a path in the file system.


3 Answers

This will recursively traverse the /path/to/folder directory and list only the symbolic links:

ls -lR /path/to/folder | grep ^l

If your intention is to follow the symbolic links too, you should use your find command but you should include the -L option; in fact the find man page says:

   -L     Follow symbolic links.  When find examines or prints information
          about files, the information used shall be taken from the  prop‐
          erties  of  the file to which the link points, not from the link
          itself (unless it is a broken symbolic link or find is unable to
          examine  the file to which the link points).  Use of this option
          implies -noleaf.  If you later use the -P option,  -noleaf  will
          still  be  in  effect.   If -L is in effect and find discovers a
          symbolic link to a subdirectory during its search, the subdirec‐
          tory pointed to by the symbolic link will be searched.

          When the -L option is in effect, the -type predicate will always
          match against the type of the file that a symbolic  link  points
          to rather than the link itself (unless the symbolic link is bro‐
          ken).  Using -L causes the -lname and -ilname predicates  always
          to return false.

Then try this:

find -L /var/www/ -type l

This will probably work: I found in the find man page this diamond: if you are using the -type option you have to change it to the -xtype option:

          l      symbolic link; this is never true if the -L option or the
                 -follow option is in effect, unless the symbolic link  is
                 broken.  If you want to search for symbolic links when -L
                 is in effect, use -xtype.

Then:

find -L /var/www/ -xtype l
like image 194
ztank1013 Avatar answered Oct 17 '22 02:10

ztank1013


One command, no pipes

find . -type l -ls

Explanation: find from the current directory . onwards all references of -type link and list -ls those in detail. Plain and simple...

Expanding upon this answer, here are a couple more symbolic link related find commands:

Find symbolic links to a specific target

find . -lname link_target

Note that link_target is a pattern that may contain wildcard characters.

Find broken symbolic links

find -L . -type l -ls

The -L option instructs find to follow symbolic links, unless when broken.

Find & replace broken symbolic links

find -L . -type l -delete -exec ln -s new_target {} \;

More find examples

More find examples can be found here: https://hamwaves.com/find/

like image 36
Serge Stroobandt Avatar answered Oct 17 '22 02:10

Serge Stroobandt


find already looks recursively by default:

[15:21:53 ~]$ mkdir foo
[15:22:28 ~]$ cd foo
[15:22:31 ~/foo]$ mkdir bar
[15:22:35 ~/foo]$ cd bar
[15:22:36 ~/foo/bar]$ ln -s ../foo abc
[15:22:40 ~/foo/bar]$ cd ..
[15:22:47 ~/foo]$ ln -s foo abc
[15:22:52 ~/foo]$ find ./ -type l
.//abc
.//bar/abc
[15:22:57 ~/foo]$ 
like image 13
jman Avatar answered Oct 17 '22 01:10

jman