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

我们两清 提交于 2019-12-02 15:48:27
ChristopheD

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 .*

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)

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.

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

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

Try this:

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

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" 
Shang Wu

Try file type flag and get rid of the appending @

ls -F /home/usr/foo | grep "@" | sed 's/@//'
supergra

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)

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/^/    /'
echo > linklist.found && $(for i in `find /path/to/dir/ -type l`; do echo `ls -d --color=always  $i` `echo " -> "`  $(ls -d --color=always `readlink -f $i`) >> linklist.found; echo >> linklist.found;  done;) && cat linklist.found | more

This works good for me however if you will be searching / the filesystem root you will need to omit the proc directory

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!