How to list all symbolic links in a directory

902,949

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

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

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.

Share:
902,949

Related videos on Youtube

Isaac
Author by

Isaac

Updated on September 18, 2022

Comments

  • Isaac
    Isaac almost 2 years

    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?

  • Sylvain Pineau
    Sylvain Pineau almost 10 years
    It will return false positive if you have a file containing "->". Try a simple touch "foo->"
  • muru
    muru almost 10 years
    -1: KasiyA's answer already covers this.
  • Eliah Kagan
    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
    ahnbizcad about 9 years
    find: Unknown argument to -type: 1
  • Sylvain Pineau
    Sylvain Pineau about 9 years
    @ahnbizcad: It's not 1 (one) but l (link)
  • bgs
    bgs over 8 years
    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!
  • Joshua Pinter
    Joshua Pinter about 8 years
    For only the current directory (i.e. not recursive) add -maxdepth 1.
  • Eliran Malka
    Eliran Malka over 7 years
    why not greping with ^l?
  • FractalSpace
    FractalSpace over 6 years
    As usual, the best answer is the one with highest +
  • Frank Nocke
    Frank Nocke about 6 years
    Nice! → .bash_alias: alias listlinks='ls -l --color | grep "\->"' 8-)
  • Itachi
    Itachi about 6 years
    Pass -R to ls to get recursive list.
  • solr
    solr about 5 years
    | awk -F' ' '{ print $11 }' | sort -n provides a tidy listing of the symlinks ready to export to a file.
  • sobi3ch
    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
    ErikE over 4 years
    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.
  • Mtl Dev
    Mtl Dev over 4 years
    Can you explain the ! in this context?
  • cuonglm
    cuonglm over 4 years
    @MtlDev ! negates the condition matching, here ! -name . means matching everything except current directory.
  • Gabriel Staples
    Gabriel Staples over 4 years
    What does the -ls do at the end of the find commands?
  • Sylvain Pineau
    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
    RichieHH over 3 years
    this also lists non-syminks. Far better solutions that answer the Q already posted.
  • RichieHH
    RichieHH over 3 years
    Way over complicating when basic shell commands can already do this.
  • Faither
    Faither over 3 years
    Please, do not use ls for scripting. Also mentioned in other answers. More: mywiki.wooledge.org/ParsingLs