Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to find and list all symlinks in a short form?

I have a large set of directories and files, along with many symlinks that typically point to a directory called "shared" or to a directory or a file underneath the "shared" directory.

Here is a representation:

$ ls -lR
total 0
drwxr-xr-x  5 myself  staff  160 Oct 11 11:54 a
drwxr-xr-x  5 myself  staff  160 Oct 11 11:55 b

./a:
total 0
lrwxr-xr-x  1 myself  staff  2 Oct 11 11:54 a2 -> a1
lrwxr-xr-x  1 myself  staff  2 Oct 11 11:54 a3 -> a2
lrwxr-xr-x  1 myself  staff  2 Oct 11 11:54 a4 -> a3

./b:
total 0
lrwxr-xr-x  1 myself  staff  2 Oct 11 11:54 b1 -> b2
lrwxr-xr-x  1 myself  staff  2 Oct 11 11:54 b2 -> b3
lrwxr-xr-x  1 myself  staff  2 Oct 11 11:55 b3 -> b4

If I use find -type l to list the symlinks, I get this long output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$ find . -type l -ls
14800041 0 lrwxr-xr-x 1 myself staff 2 Oct 11 11:54 ./a/a4 -> a3
14800032 0 lrwxr-xr-x 1 myself staff 2 Oct 11 11:54 ./a/a3 -> a2
14799990 0 lrwxr-xr-x 1 myself staff 2 Oct 11 11:54 ./a/a2 -> a1
14800060 0 lrwxr-xr-x 1 myself staff 2 Oct 11 11:54 ./b/b2 -> b3
14800061 0 lrwxr-xr-x 1 myself staff 2 Oct 11 11:55 ./b/b3 -> b4
14800046 0 lrwxr-xr-x 1 myself staff 2 Oct 11 11:54 ./b/b1 -> b2

If I do a recursive listing with ls -lR, I get a similar output.

I just need the two bits in my output – the symlink name and what it points to, separated by ->. How can I get this output without having to use filters to parse ls or find output?

>Solution :

You could try

find . -type l -printf '%p -> %l\n'

Or if the -printf is not available you can also try

find . -type l -exec sh -c 'for link; do echo "$link -> $(readlink "$link")"; done' sh {} +

Hope it will help

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading