How to list all symbolic links in a directory
Solution 1
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 "\->"
Solution 2
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
Solution 3
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
Solution 4
POSIXly:
find ! -name . -prune -type l
Solution 5
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.
Related videos on Youtube
Isaac
Updated on September 18, 2022Comments
-
Isaac almost 2 years
I have a symbolic link in my
/var/www/
directory that links to WordPress. When I run the commandls -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? -
Sylvain Pineau almost 10 yearsIt will return false positive if you have a file containing "
->
". Try a simpletouch "foo->"
-
muru almost 10 years-1: KasiyA's answer already covers this.
-
Eliah Kagan almost 10 years
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. -
ahnbizcad about 9 years
find: Unknown argument to -type: 1
-
Sylvain Pineau about 9 years@ahnbizcad: It's not
1
(one) butl
(link) -
bgs over 8 yearsGreat answer! I adjusted mine to not descend down directory path like this:
find /<your_directory> -maxdepth 1 -type l -ls 2>/dev/null
Thank you! -
Joshua Pinter about 8 yearsFor only the current directory (i.e. not recursive) add
-maxdepth 1
. -
Eliran Malka over 7 yearswhy not
grep
ing with^l
? -
FractalSpace over 6 yearsAs usual, the best answer is the one with highest +
-
Frank Nocke about 6 yearsNice! → .bash_alias:
alias listlinks='ls -l --color | grep "\->"'
8-) -
Itachi about 6 yearsPass
-R
tols
to get recursive list. -
solr about 5 years
| awk -F' ' '{ print $11 }' | sort -n
provides a tidy listing of the symlinks ready to export to a file. -
sobi3ch almost 5 years@cig0 u do not need to use
awk
, u probably want just this:find . -maxdepth 1 -type l | sort -n
-
ErikE over 4 yearsJust 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
. -
Mtl Dev over 4 yearsCan you explain the
!
in this context? -
cuonglm over 4 years@MtlDev
!
negates the condition matching, here! -name .
means matching everything except current directory. -
Gabriel Staples over 4 yearsWhat does the
-ls
do at the end of the find commands? -
Sylvain Pineau over 4 years@GabrielStaples from man find:
-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
-
RichieHH over 3 yearsthis also lists non-syminks. Far better solutions that answer the Q already posted.
-
RichieHH over 3 yearsWay over complicating when basic shell commands can already do this.
-
Faither over 3 yearsPlease, do not use
ls
for scripting. Also mentioned in other answers. More: mywiki.wooledge.org/ParsingLs