How to list only symbolic links in a directory on a Linux filesystem.

Posted: February 4, 2018. At: 10:29 PM. This was 3 weeks ago. Post ID: 11809
Page permalink.
WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters. These cookies expire two weeks after they are set.

How to list all symlinks in a Linux directory

To list only the symbolic links on a Linux filesystem, use ls and grep as shown below. This will easily do this for you.

ubuntu ~/Documents $ ls -hula /lib | grep "\->"
lrwxrwxrwx  1 root root   21 Feb  4 02:48 cpp -> /etc/alternatives/cpp
lrwxrwxrwx  1 root root   20 Feb  4 02:48 ld-linux.so.2 -> /lib32/ld-linux.so.2
lrwxrwxrwx  1 root root   22 Feb  4 02:48 libcryptsetup.so.4 -> libcryptsetup.so.4.5.0
lrwxrwxrwx  1 root root   17 Feb  4 02:48 libip4tc.so.0 -> libip4tc.so.0.1.0
lrwxrwxrwx  1 root root   17 Feb  4 02:48 libip6tc.so.0 -> libip6tc.so.0.1.0
lrwxrwxrwx  1 root root   16 Feb  4 02:48 libiptc.so.0 -> libiptc.so.0.0.0
lrwxrwxrwx  1 root root   20 Feb  4 02:48 libxtables.so.10 -> libxtables.so.10.0.0

Another way is to use this trick.

ubuntu ~/Documents $ ls -hula /lib | grep ^l
lrwxrwxrwx  1 root root   21 Feb  4 02:48 cpp -> /etc/alternatives/cpp
lrwxrwxrwx  1 root root   20 Feb  4 02:48 ld-linux.so.2 -> /lib32/ld-linux.so.2
lrwxrwxrwx  1 root root   22 Feb  4 02:48 libcryptsetup.so.4 -> libcryptsetup.so.4.5.0
lrwxrwxrwx  1 root root   17 Feb  4 02:48 libip4tc.so.0 -> libip4tc.so.0.1.0
lrwxrwxrwx  1 root root   17 Feb  4 02:48 libip6tc.so.0 -> libip6tc.so.0.1.0
lrwxrwxrwx  1 root root   16 Feb  4 02:48 libiptc.so.0 -> libiptc.so.0.0.0
lrwxrwxrwx  1 root root   20 Feb  4 02:48 libxtables.so.10 -> libxtables.so.10.0.0

How to list only directories in a Linux filesystem.

ubuntu ~/Documents $ ls -hula /var/log | grep ^d
drwxrwxr-x 14 root      syslog   4.0K Feb  4 02:56 .
drwxr-xr-x 14 root      root     4.0K Feb  4 02:46 ..
drwxr-x---  2 root      adm      4.0K Feb  3 06:53 apache2
drwxr-xr-x  2 root      root     4.0K Dec 27 21:46 apt
drwxr-xr-x  2 root      root     4.0K Feb  4 00:00 atop
drwxr-xr-x  2 root      root     4.0K Dec 27 21:46 dist-upgrade
drwxr-xr-x  2 root      root     4.0K Dec 27 21:46 fsck
drwxr-xr-x  2 landscape root     4.0K Dec 27 21:46 landscape
drwxr-s---  2 mysql     adm      4.0K Jan 25 17:45 mysql
drwxrwxr-t  2 root      postgres 4.0K Feb  3 06:53 postgresql
drwxr-s---  2 snort     adm      4.0K Feb  3 06:25 snort
drwxr-xr-x  2 root      root     4.0K Feb  3 06:53 sysstat
drwxr-xr-x  2 root      root     4.0K Dec 27 21:46 unattended-upgrades
drwxr-xr-x  2 root      root     4.0K Feb  3 06:53 upstart

This trick will only list files.

ubuntu ~/Documents $ ls -hula /home/ubuntu/Documents | grep ^-
-rwxrwxr-x  1 ubuntu ubuntu 8.7K Oct 30 08:45 a.out
-rw-rw-r--  1 ubuntu ubuntu 101K Jan 27 22:06 documents.lst
-rw-rw-r--  1 ubuntu ubuntu 1.2K Nov 18  2015 hashes.out
-rw-rw-r--  1 ubuntu ubuntu 2.5K Jun 18  2015 hole.o
-rw-------  1 ubuntu ubuntu 1.7K Jul 23  2017 id_rsa
-rw-r--r--  1 ubuntu ubuntu  404 Jul 23  2017 id_rsa.pub
-rw-rw-r--  1 ubuntu ubuntu 1.1K Jul 25  2017 my.c
-rw-rw-r--  1 ubuntu ubuntu  995 Oct  3 23:02 nikto-pinterest-log.log
-rwxrwxr-x  1 ubuntu ubuntu 8.7K Oct 30 08:45 read
-rw-rw-r--  1 ubuntu ubuntu 1.3K Oct 30 08:45 read.c
-rwxrwxr-x  1 ubuntu ubuntu 7.6K Mar 20  2015 recipe-502268-1.py
-rw-rw-r--  1 ubuntu ubuntu 256K Oct 15  2016 SAM
-rw-rw-r--  1 ubuntu ubuntu  64K Mar 23  2015 sam.save
-rwxrwxr-x  1 ubuntu ubuntu  63K Nov 17  2015 secretsdump.py
-rw-rw-r--  1 ubuntu ubuntu 256K Nov 18  2015 SECURITY
-rw-rw-r--  1 ubuntu ubuntu  32K Mar 23  2015 security.save
-rw-rw-r--  1 ubuntu ubuntu 9.3M Oct  3 23:25 SYSTEM
-rw-rw-r--  1 ubuntu ubuntu  20M Mar 23  2015 system.save
-rw-rw-r--  1 ubuntu ubuntu 7.6K Oct  3 23:11 virii.S

Use it like this to include the . and .. entries in the directory listing.

Johns-MacBook-Air:~ jason$ ls -hula | grep ^dr
drwxr-xr-x+ 18 jason  staff   612B  4 Feb 22:12 .
drwxr-xr-x   5 root   admin   170B  4 Feb 22:11 ..
drwx------   2 jason  staff    68B  4 Feb 22:03 .Trash
drwx------  43 jason  staff   1.4K 17 Jan 21:40 .bash_sessions
drwx------+  7 jason  staff   238B  4 Feb 22:11 Desktop
drwx------+ 31 jason  staff   1.0K  4 Feb 22:11 Documents
drwx------+ 12 jason  staff   408B  4 Feb 22:11 Downloads
drwx------@ 54 jason  staff   1.8K  4 Feb 22:11 Library
drwx------+  5 jason  staff   170B  4 Feb 22:11 Movies
drwx------+  6 jason  staff   204B  4 Feb 22:11 Music
drwx------+  3 jason  staff   102B  4 Feb 22:11 Pictures
drwxr-xr-x+  5 jason  staff   170B  4 Feb 22:11 Public
<pre>
 
To find out where you are in the directory structure, use the pwd command. It is very useful. Whether you are using Darwin or Linux.
 
<pre lang="bash">
Johns-MacBook-Air:~ jason$ pwd
/Users/jason

Another way to list all directories.

Johns-MacBook-Air:~ jason$ basename *
Desktop
Documents
Downloads
Library
Movies
Music
Pictures
Public

No comments have been made. Use this form to start the conversation :)

Leave a Reply