2009-09-11 50 views
28

符號鏈接僅顯示文件和文件夾基本上我想做到以下幾點:是在tcsh中或bash

ls -l[+someflags] 

(或通過其他方式),將只顯示是符號鏈接

文件所以輸出將看起來

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

例如,

,只顯示目錄我有一個別名:

alias lsd 'ls -l | grep ^d' 

我不知道怎麼只顯示隱藏文件或僅隱藏目錄?

我有以下的解決方案,但是它並沒有顯示在顏色:(輸出

ls -ltra | grep '\->' 

回答

51

查找目錄中的所有符號鏈接:

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

爲了隱藏列表文件:

ls -ald .* 
+0

find/usr/bin -type l -print | xargs ls -l不打印顏色。當我做ls -l它確實顯示顏色,因爲我有別名ls'ls --color = auto' – vehomzzz 2009-09-11 18:22:03

+0

我更新了我的答案以反映這一點。 – ChristopheD 2009-09-11 18:33:09

+3

有一個小問題。如果/ usr/bin下沒有符號鏈接,則comamnd等於ls -l,表示不顯示過濾器而顯示當前目錄。 – 2013-10-10 02:21:57

12

僅適用於「隱藏」文件夾 - 點文件夾,請嘗試:

ls -l .** 

是的,兩個星號是必要的,否則你也會得到。並在結果中。

對於符號鏈接,那麼,試試符號鏈接程序:

symlinks -v . 

(顯示當前目錄下的所有符號鏈接)

+1

在'。**'的擴展中排除'.'和'..'的shell是什麼?在bash中,它等同於'。*',它包含'.'和'..'。在zsh中,'。*'和'。**'*不包括*'.'和'..'。 – 2014-12-29 01:58:29

+0

「symlinks -v」對我來說非常完美,但是,我必須指定目錄,它不會隱式地在當前目錄中工作,您必須指定它。 – m4l490n 2016-11-07 16:37:50

5

你幾乎有你的grep的解決方案;讓我們專注於讓你再次獲得COLOR。

試試這個:

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

聰明。 grep'\ - >' – 2014-09-14 12:15:36

+0

@JesseFisher:或者'grep -e' - >'' – 2014-12-29 01:56:58

+0

Upvote for the color,but I leaan towards John Shirey's ls&grep ... – BuvinJ 2016-01-25 18:25:40

3

嘗試文件類型的標誌,並擺脫了追加的@

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

對於(T)CSH

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

(這是隻是pbr's answer但連字符逃脫。)

的Mac OSX

在OSX,ls原理不同,所以將它添加到您的~/.cshrc文件:

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

然後調用:

ls -G -ltra | grep '\->' # (-G is equivalent of ls --color) 
1

對於bash:
這提供了一個不錯的輸出。

 
sl=`find -L /path/to/target -xtype l`; for links in $sl; do ls --color=always -ltra $links; done | sed 's/^/ /' 
2
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 

這對我的工作好但如果你將搜索/文件系統的根目錄,您將需要省略proc目錄

7

只顯示符號鏈接並鏈接到什麼:

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

要限制對眼前這個DIR

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

實施例輸出(LN -s的/ usr/bin中MOO後):

./moo -> /usr/bin 
8
ls -l | grep lrw 

僅示出了符號鏈接(文件和目錄)。不知道如何讓它們變得豐富多彩。

ls -lad .* 

只顯示隱藏的文件/目錄

ls -l | grep drw 

只顯示目錄。

0

用法:foo $路徑

如果未指定,則使用當前路徑。

#!/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 
4

提高一點對@ChristopheD(上公認的答案,因爲我沒有足夠的聲譽coudnt評論)

我使用別名

findsymlinks <path> <depth> 

在給定的別名接受的答案是

alias findsymlinks "find \!:1 -maxdepth \!:2 -type l -print | xargs ls -l --color=auto"