I have a symbolic link in my /var/www/
directory that links to WordPress. When I run the command ls -la
from the /var/www/
directory the link to WordPress doesn't show up. Is there a way to list all of the symbolic links that are in a directory?

- 62,169

- 2,955
- 3
- 14
- 13
10 Answers
Parsing ls
is a Bad Idea®, prefer a simple find
in that case:
find . -type l -ls
To only process the current directory:
find . -maxdepth 1 -type l -ls

- 62,169
You can use grep
with ls
command to list all the symbolic links present in the current directory.
This will list all the links present in the current directory.
ls -la /var/www/ | grep "\->"
-
12It will return false positive if you have a file containing "
->
". Try a simpletouch "foo->"
– Sylvain Pineau Sep 09 '14 at 18:32 -
7
-
1
-
-
1Please, do not use
ls
for scripting. Also mentioned in other answers. More: http://mywiki.wooledge.org/ParsingLs – Artfaith Mar 08 '21 at 17:36
grep
is your friend:
ls -lhaF | grep ^l # list links
ls -lhaF | grep ^d # list directories
ls -lhaF | grep ^- # list files
This will list lines starting with "l" which represent Links in the perms column in place of l
use d
for directories and -
for files
-
1Just don't do anything with this method programatically since malicious filenames can end up injecting shell code. To be safe, one should use the
find
command with-exec
, and if piping toxargs
, use the null-character separator output flag offind
combined with the null-character separator input flag ofxargs
. – ErikE Dec 19 '19 at 15:24
POSIXly:
find ! -name . -prune -type l

- 2,455
-
1
-
@MtlDev
!
negates the condition matching, here! -name .
means matching everything except current directory. – cuonglm Jan 15 '20 at 04:49
To view the symbolic links in a directory:
Open a terminal and move to that directory.
Type the command:
ls -la
This shall long list all the files in the directory even if they are hidden.
The files that start with
l
are your symbolic link files.

- 117,780
-
1
-
this also lists non-syminks. Far better solutions that answer the Q already posted. – RichieHH Oct 14 '20 at 05:09
This returns all symbolically linked items (both dirs & fns) in a directory:
find . -maxdepth 1 -type l -print | cut -c3- | grep -v "\#"
However, in order to distinguish between actual symbolically linked item types:
ls -lhaF | grep ^l | grep -v "\#" | cut -c42- | grep -v "/" | cut -d' ' -f1
Returns symbolically linked filename items only. And,
ls -lhaF | grep ^l | grep -v "\#" | cut -c42- | grep "/" | cut -d' ' -f1
Returns symbolically linked dirname items only.
using zsh
ls -l *(@)
lrwxrwxrwx 1 david david 15 Nov 18 22:35 gvimrc -> /etc/vim/gvimrc
lrwxrwxrwx 1 david david 13 Nov 18 22:19 mydomains.php -> mydomains.php

- 151
- 4
Kindly find below one liner bash script command to find all broken symbolic links recursively in any linux based OS
b=$(find / -type l); for i in $(echo $b); do file $i ; done |grep -i broken 2> /dev/null

- 189
Type ls -lai
,it will list all the files and subdirectories with corresponding inode numbers.You know files with same inode number are the links(hard or soft) and this solution also works for the symbolic links.

- 451
-
1
ls -lai
does not show the same inode number for a file and its symbolic links. Unlike hard links, symbolic links have their own separate inode entries. This is what it looks like. – Eliah Kagan Sep 12 '14 at 06:19
Can be done with python as well:
$ python -c "import os,sys; print '\n'.join([os.path.join(sys.argv[1],i) for i in os.listdir(sys.argv[1]) if os.path.islink(os.path.join(sys.argv[1],i))])" /path/to/dir
Sample run:
$ python -c "import os,sys; print '\n'.join([os.path.join(sys.argv[1],i) for i in os.listdir(sys.argv[1]) if os.path.islink(os.path.join(sys.argv[1],i))])" /etc
/etc/vtrgb
/etc/printcap
/etc/resolv.conf
/etc/os-release
/etc/mtab
/etc/localtime
This can be extended to be recursive via os.walk
function, but it's sufficient to use simple list generation for listing links in a single directory as I showed above.

- 105,154
- 20
- 279
- 497
find: Unknown argument to -type: 1
– ahnbizcad May 28 '15 at 16:391
(one) butl
(link) – Sylvain Pineau May 28 '15 at 17:51find /<your_directory> -maxdepth 1 -type l -ls 2>/dev/null
Thank you!
– bgs Feb 04 '16 at 18:47-maxdepth 1
. – Joshua Pinter Apr 08 '16 at 14:32| awk -F' ' '{ print $11 }' | sort -n
provides a tidy listing of the symlinks ready to export to a file. – solr Jun 16 '19 at 01:54awk
, u probably want just this:find . -maxdepth 1 -type l | sort -n
– sobi3ch Jun 28 '19 at 15:43-ls
do at the end of the find commands? – Gabriel Staples Feb 13 '20 at 07:22-ls True; list current file in ls -dils format on standard output.
Useful to see./os-release -> ../usr/lib/os-release
in /etc rather than just./os-release
– Sylvain Pineau Feb 13 '20 at 09:11