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

会有一股神秘感。 提交于 2019-12-04 07:48:42

问题


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 '\->'

回答1:


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



回答2:


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)




回答3:


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.




回答4:


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



回答5:


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

Try this:

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



回答6:


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" 



回答7:


Try file type flag and get rid of the appending @

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



回答8:


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)



回答9:


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/^/    /'



回答10:


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




回答11:


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


来源:https://stackoverflow.com/questions/1412423/display-only-files-and-folders-that-are-symbolic-links-in-tcsh-or-bash

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