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?
I have a symbolic link in my
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
Credits: How do I make the shell to recognize the file names returned by a `ls -A` command, and these names contain spaces?
17@ahnbizcad: It's not
l(link) May 28, 2015 at 17:51
3Great answer! I adjusted mine to not descend down directory path like this:
find /<your_directory> -maxdepth 1 -type l -ls 2>/dev/nullThank you!– bgsFeb 4, 2016 at 18:47
3For only the current directory (i.e. not recursive) add
-maxdepth 1. Apr 8, 2016 at 14:32
1@cig0 u do not need to use
awk, u probably want just this:
find . -maxdepth 1 -type l | sort -n– sobi3chJun 28, 2019 at 15:43
2@GabrielStaples from man find:
-ls True; list current file in ls -dils format on standard output.Useful to see
./os-release -> ../usr/lib/os-releasein /etc rather than just
./os-releaseFeb 13, 2020 at 9:11
You can use
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 simple
touch "foo->"Sep 9, 2014 at 18:32
^l? Jan 18, 2017 at 12:13
4As usual, the best answer is the one with highest + Dec 10, 2017 at 19:19
1Nice! → .bash_alias:
alias listlinks='ls -l --color | grep "\->"'8-)– Frank NApr 11, 2018 at 3:08
lsto get recursive list.– ItachiApr 27, 2018 at 7:10
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
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
-exec, and if piping to
xargs, use the null-character separator output flag of
findcombined with the null-character separator input flag of
xargs.– ErikEDec 19, 2019 at 15:24
find ! -name . -prune -type l
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.
To view the symbolic links in a directory:
Open a terminal and move to that directory.
Type the command:
This shall long list all the files in the directory even if they are hidden.
The files that start with
lare your symbolic link files.
1-1: KasiyA's answer already covers this.– muruSep 10, 2014 at 5:45
this also lists non-syminks. Far better solutions that answer the Q already posted.– RichieHHOct 14, 2020 at 5:09
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
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.
ls -laidoes 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. Sep 12, 2014 at 6:19
Can be done with python as well:
$ python -c "import os,sys; print '\n'.join([os.path.join(sys.argv,i) for i in os.listdir(sys.argv) if os.path.islink(os.path.join(sys.argv,i))])" /path/to/dir
$ python -c "import os,sys; print '\n'.join([os.path.join(sys.argv,i) for i in os.listdir(sys.argv) if os.path.islink(os.path.join(sys.argv,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.
2Way over complicating when basic shell commands can already do this.– RichieHHOct 14, 2020 at 5:10
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