彩色 FIND 输出?

wes*_*wes 15 colors bash find

是否可以从find命令获得彩色输出?也就是说,在每个找到的项目的路径中,目录是蓝色的,可执行脚本是绿色的,等等?我正在使用GNU findutils 的4.4.2 版。

编辑 - 为了澄清,每个结果都将像这样突出显示:

./path/to/file.sh
  ^    ^  ^
  |    |  L green
   blue
Run Code Online (Sandbox Code Playgroud)

(例如,如果执行find . -type f)。

Pet*_*r.O 10

更新:我添加了一个新的(不同的)脚本......Ignacio Vazquez-Abrams 有一点:这个问题确实需要executable scripts are green, et cetera......好吧......你会在这个答案的末尾找到这样一个(原型)脚本。


第一个(原始)部分是关于grc和的grcat

这应该有效;grc...(正如enzotib指出的那样......包名称是grc......示例中使用的子实用程序是grcat

generic colouriser for everything

generic colouriser, can be used to colourise logfiles,
output of commands, arbitrary text....
configured via regexp's.
Run Code Online (Sandbox Code Playgroud)

以下示例打印

  • ./ 品红色
  • bin/cpp/ 青色
  • bigint 大胆的白色

我还没有完全弄清楚它是如何处理它的配置文件的,但这看起来它会做你想做的事(一旦你驯服了它)。例如。对于没有子目录的文件,颜色序列似乎与表达式的序列不同。
我认为这是可能的(但我现在有点忙)...

echo "# my config file
regexp=(\./)(.*/)([^/]+)
colours=bold white,magenta,cyan
">$HOME/.grc/findhi

find . -maxdepth 3 -name '*' | grcat findhi
Run Code Online (Sandbox Code Playgroud)

这是新的伊格纳西奥灵感脚本:)

如果您使用单个路径作为到find.
UNTESTED在这个脚本的问题。这只是概念。
一个问题是:符号链接……浑水……
原样,它ERROR在遇到未知类型(例如符号链接)时打印,然后继续处理过去。
谢谢你enzotibtput例子。

dircol=$(tput bold ;tput setaf 4)
coloff=$(tput sgr0)

root="$HOME"       # define path here, not in 'find` arg
root="${root:-.}"  # default to '.'
root="${root%/}/"  # add trailing '/'
#
find "$root" -maxdepth 1 -name '*' -printf "%y %P\n" | 
  while read -r line ;do
    case $line in 
       d   ) printf "%s\n" "$dircol$root$coloff";;  
       d\ *) printf "%s\n" "$dircol$root${line:2}$coloff";;  
       f\ *) l="$root${line:2}"
             d="${l%/*}/"
             f="${l##*/}"
             cd -P "$d" 
             printf "%s" "$dircol$d$coloff"  
             ls --color=always -R1 "$f"
             cd - >/dev/null
             ;; 
          *) printf "ERROR - type not yet catered for\n";;  
    esac
  done 
Run Code Online (Sandbox Code Playgroud)


Tim*_*edy 9

您可以-exec用来完成大部分工作(我的解决方案不会以不同的方式为目录部分着色)。如果您-printfind命令中有,请将其替换为-exec ls --color -d; 如果您使用的是隐式打印,请添加它。这假设您ls支持该--color选项。

find . -exec ls --color -d {} \;
Run Code Online (Sandbox Code Playgroud)


Vol*_*gel 5

这仅对路径和文件名进行两种颜色的高亮显示,而不是针对每个文件类型的 ls

grep以正确的方式为匹配和不匹配部分配置输出颜色,并匹配文件名:

$ export GREP_COLORS="sl=0;33;49:ms=1;34;49"
$ find /etc/ -type f | head | grep --color=always '^\|[^/]*$'
Run Code Online (Sandbox Code Playgroud)


屏幕灰色

您可能不想覆盖变量GREP_COLORS,因此仅将其设置为grep

$ find /etc/ -type f | head | GREP_COLORS="sl=0;33;49:ms=1;34;49" grep --color=always '^\|[^/]*$'
Run Code Online (Sandbox Code Playgroud)

(来自已弃用变量的定义的GREP_COLOR优先级低于 中的定义GREP_COLORS

对于彩色代码,请参见colortest-16从包装colortest
在与部分“设置图形绘制”的ANSI终端的命令序列