120

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?

69

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 "\->"
  • 5
    It will return false positive if you have a file containing "->". Try a simple touch "foo->" – Sylvain Pineau Sep 9 '14 at 18:32
  • 7
    please don't copy&paste content of another answer to your own answer. -1 – αғsнιη Sep 10 '14 at 5:14
  • 1
    why not greping with ^l? – Eliran Malka Jan 18 '17 at 12:13
  • 2
    As usual, the best answer is the one with highest + – FractalSpace Dec 10 '17 at 19:19
  • Nice! → .bash_alias: alias listlinks='ls -l --color | grep "\->"' 8-) – Frank Nocke Apr 11 '18 at 3:08
223

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

Credits: How do I make the shell to recognize the file names returned by a `ls -A` command, and these names contain spaces?

  • find: Unknown argument to -type: 1 – ahnbizcad May 28 '15 at 16:39
  • 11
    @ahnbizcad: It's not 1 (one) but l (link) – Sylvain Pineau May 28 '15 at 17:51
  • 3
    Great 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! – bgs Feb 4 '16 at 18:47
  • 3
    For only the current directory (i.e. not recursive) add -maxdepth 1. – Joshua Pinter Apr 8 '16 at 14:32
10

the ls -la command show all files and folders and also symbolic linked directory, if this command doesn't show any symbolic directory it means you don't have a symbolic link to WordPress.

see the result of running ls -la:

kasiya@kasiya-pc:~$ cd /sys/devices/platform/sony-laptop
kasiya@kasiya-pc:/sys/devices/platform/sony-laptop$ ls -la
total 0
drwxr-xr-x  3 root root    0 Sep  9 19:57 .
drwxr-xr-x 14 root root    0 Sep 10  2014 ..
-r--r--r--  1 root root 4096 Sep  9 22:32 battery_care_health
-rw-r--r--  1 root root 4096 Sep  9 22:32 battery_care_limiter
lrwxrwxrwx  1 root root    0 Sep  9 19:57 driver -> ../../../bus/platform/drivers/sony-laptop
-r--r--r--  1 root root 4096 Sep  9 22:32 modalias
drwxr-xr-x  2 root root    0 Sep  9 22:32 power
lrwxrwxrwx  1 root root    0 Sep  9 22:32 subsystem -> ../../../bus/platform
-rw-r--r--  1 root root 4096 Sep  9 22:32 touchpad
-rw-r--r--  1 root root 4096 Sep  9 19:57 uevent

You will see all symbolic directory has l permission at the begging of permissions flags. and if you take a grep with ^l you can list only symbolic files or directory:

kasiya@kasiya-pc:/sys/devices/platform/sony-laptop$ ls -la |grep ^l
lrwxrwxrwx 1 root root    0 Sep  9 19:57 driver -> ../../../bus/platform/drivers/sony-laptop
lrwxrwxrwx 1 root root    0 Sep  9 22:32 subsystem -> ../../../bus/platform
kasiya@kasiya-pc:/sys/devices/platform/sony-laptop$ 

driver and subsystem directory are symbolic link to other directory in here.

  • 1
    Minor specification here... the first character of the permissions string isn't really a permission. It's the file type. As you've stated l means it's a symbolic link. – conner.xyz Apr 4 '16 at 16:49
4

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

3

POSIXly:

find ! -name . -prune -type l
0

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.

  • 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 6:19
0

To view the symbolic links in a directory:

  1. Open a terminal and move to that directory.

  2. Type the command:

    ls -la
    

    This shall long list all the files in the directory even if they are hidden.

  3. The files that start with l are your symbolic link files.

0

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.

0

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.

protected by Sylvain Pineau Jun 20 '16 at 11:58

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.