Display only files and folders that are symbolic links in tcsh or bash
Asked Answered
P

10

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 '\->'
Petition answered 11/9, 2009 at 18:2 Comment(0)
I
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 .*
Irbm answered 11/9, 2009 at 18:6 Comment(2)
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'Petition
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.Woolridge
F
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)

Forfeiture answered 11/9, 2009 at 18:27 Comment(2)
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 ...Hemo
"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.Crouse
F
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.

Florri answered 17/7, 2014 at 17:11 Comment(0)
G
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
Gensler answered 19/5, 2014 at 19:15 Comment(0)
M
6

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

Try this:

ls --color=always -ltra | grep '->'
Marconigraph answered 25/9, 2009 at 21:58 Comment(2)
Clever. I needed to escape the dash in the grep statement: grep '\->'Capitalize
@JesseFisher: Or grep -e '->'Hemo
L
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" 
Lobbyism answered 21/1, 2015 at 9:46 Comment(0)
A
2

Try file type flag and get rid of the appending @

ls -F /home/usr/foo | grep "@" | sed 's/@//'
Asmara answered 13/3, 2012 at 21:8 Comment(0)
S
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)
Sequestrate answered 14/12, 2013 at 22:40 Comment(0)
A
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/^/    /'
Arrio answered 30/1, 2014 at 20:9 Comment(0)
P
-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
Philomenaphiloo answered 29/12, 2014 at 1:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.