175

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?

| improve this question | | | | |
93

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 "\->"
| improve this answer | | | | |
  • 6
    It will return false positive if you have a file containing "->". Try a simple touch "foo->" – Sylvain Pineau Sep 9 '14 at 18:32
  • 4
    why not greping with ^l? – Eliran Malka Jan 18 '17 at 12:13
  • 3
    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
  • Pass -R to ls to get recursive list. – Itachi Apr 27 '18 at 7:10
333

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?

| improve this answer | | | | |
  • 17
    @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
  • 1
    @cig0 u do not need to use awk, u probably want just this: find . -maxdepth 1 -type l | sort -n – sobi3ch Jun 28 '19 at 15:43
  • 1
    What does the -ls do at the end of the find commands? – Gabriel Staples Feb 13 at 7:22
5

POSIXly:

find ! -name . -prune -type l
| improve this answer | | | | |
  • Can you explain the ! in this context? – Mtl Dev Jan 14 at 15:42
  • @MtlDev ! negates the condition matching, here ! -name . means matching everything except current directory. – cuonglm Jan 15 at 4:49
5

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

| improve this answer | | | | |
  • Just 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 to xargs, use the null-character separator output flag of find combined with the null-character separator input flag of xargs. – ErikE Dec 19 '19 at 15:24
3

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.

| improve this answer | | | | |
1

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.

| improve this answer | | | | |
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.

| improve this answer | | | | |
  • 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 6:19
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.

| improve this answer | | | | |

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