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?
- 63,229
- 3,105
- 3
- 15
- 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
- 63,229
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:
ls -laThis shall long list all the files in the directory even if they are hidden.
The files that start with
lare your symbolic link files.
- 119,640
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
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
- 195
- 5
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.
- 453
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.
- 107,582