38

Basically I want do the following:

ls -l[+someflags]

(or by some other means) that will only display files that are symbolic links

so the output would look

-rw-r--r--  1 username grp   size date-time    filename -> somedir
-rw-r--r--  1 username grp   size date-time    filename2 -> somsdfsdf

etc.

For example,

to show only directories I have an alias:

alias  lsd  'ls -l | grep ^d'

I wonder how to display only hidden files or only hidden directories?

I have the following solution, however it doesn't display the output in color :(

ls -ltra | grep '\->'
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
vehomzzz
  • 42,832
  • 72
  • 186
  • 216

10 Answers10

63

Find all the symbolic links in a directory:

ls -l `find /usr/bin -maxdepth 1 -type l -print`

For the listing of hidden files:

ls -ald .*
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
ChristopheD
  • 112,638
  • 29
  • 165
  • 179
  • find /usr/bin -type l -print | xargs ls -l doesn't print in color. when I do ls -l it does show colors as I have alias ls 'ls --color=auto' – vehomzzz Sep 11 '09 at 18:22
  • 4
    There is a little problem. If there is no symlinks under /usr/bin, the comamnd equals to ls -l, means to display current directory without filter. – Tanky Woo Oct 10 '13 at 02:21
13

For only "hidden" folders - dot folders, try:

ls -l .**

Yes, the two asterisks are necessary, otherwise you'll also get . and .. in the results.

For symlinks, well, try the symlinks program:

symlinks -v .

(shows all symlinks under current directory)

jmanning2k
  • 9,297
  • 4
  • 31
  • 23
  • 1
    In what shell does the expansion of `.**` exclude `.` and `..`? In bash, it's equivalent to `.*`, and it includes `.` and `..`. In zsh, both `.*` and `.**` *exclude* `.` and `..`. – Keith Thompson Dec 29 '14 at 01:58
  • "symlinks -v" worked perfect for me, nevertheless, I had to specify the directory, it doesn't work implicitly in the current directory, you have to specify it. – m4l490n Nov 07 '16 at 16:37
12
ls -l | grep lrw 

shows only symlinks (files and directories). Not sure how to get them colorful, though.

ls -lad .* 

shows only hidden files/directories

ls -l | grep drw

shows directories only.

John Shirey
  • 129
  • 1
  • 2
8

To display JUST the symlinks and what they link to:

find -P . -type l -exec echo -n "{} -> " \; -exec readlink {} \;

To limit to JUST THIS DIR

find -P .  -maxdepth 1 -type l -exec echo -n "{} -> " \; -exec readlink {} \;

Example output (after ln -s /usr/bin moo):

./moo -> /usr/bin
6

You were almost there with your grep solution; let's focus on getting you COLOR again.

Try this:

ls --color=always -ltra | grep '->'
pbr
  • 480
  • 3
  • 8
5

Improving a little on the accepted answer given by @ChristopheD (coudnt comment on the accepted answer since I dont have enough reputation)

I use an alias

findsymlinks <path> <depth> 

where the alias is

alias findsymlinks "find \!:1 -maxdepth \!:2 -type l -print | xargs ls -l --color=auto" 
Pranav Rai
  • 160
  • 4
  • 8
2

Try file type flag and get rid of the appending @

ls -F /home/usr/foo | grep "@" | sed 's/@//'
Rafał Rawicki
  • 22,324
  • 5
  • 59
  • 79
Shang Wu
  • 29
  • 2
1

For (t)csh:

ls --color=always -ltra | grep '\->'

(This is simply pbr's answer but with the hyphen escaped.)

Mac OSX

On OSX, ls works differently, so add this to your ~/.cshrc file:

setenv CLICOLOR_FORCE 1   # (equivalent of Linux --color=always)

And then call:

ls -G -ltra | grep '\->'  # (-G is equivalent of ls --color)
Community
  • 1
  • 1
supergra
  • 1,578
  • 13
  • 19
1

For bash:
This provides a nice output.

sl=`find -L /path/to/target -xtype l`; for links in $sl; do ls --color=always -ltra $links; done | sed 's/^/    /'
-1

Usage: foo $path

Uses current path if none specified.

#!/bin/bash

case "$1" in

    -r)
    find $2 -type l -print | while IFS= read line ; do ls -l --color=always "$line"; done
    ;;

    --help)
    echo 'Usage: foo [-r] [$PATH]'
    echo    
    echo '-r  Recursive'
    ;;


    *)
    ls --color=always -ltra $1 | grep '\->'
esac
Justin D.
  • 1
  • 2