Joh*_*eid 5 ls shell bash text-processing wc
我希望能够列出显示每个文件的行数和日期的文件。我可以很高兴地使用wc -l *. 不是问题。我可以使用ls -l.
有没有办法将这两个命令结合起来给我一个列中的输出?
下面是一些与find+ wc+ date。
find . -maxdepth 1 -exec sh -c '[ -f "$0" ] && \
printf "%6s\t\t%s\t%s\n" "$(wc -l<"$0")" "$(date -r "$0")" "$0"' {} \;
Run Code Online (Sandbox Code Playgroud)
代替date -r一个也可以使用例如stat -c%y。
输出如下所示:
394 Thu Oct 16 22:38:14 UTC 2014 ./.zshrc
7 Thu Oct 30 11:19:01 UTC 2014 ./tmp.txt
2 Thu Oct 30 06:02:00 UTC 2014 ./tmp2.txt
40 Thu Oct 30 04:16:30 UTC 2014 ./pp.txt
Run Code Online (Sandbox Code Playgroud)
以此为起点,可以创建一个接受目录和模式作为参数的函数:
myls () { find "$1" -maxdepth 1 -name "$2" -exec sh -c '[ -f "$0" ] && \
printf "%6s \t\t%s\t%s\n" "$(wc -l<"$0")" "$(date -r "$0")" "$0"' {} \;; }
Run Code Online (Sandbox Code Playgroud)
之后myls /tmp '*.png'将仅列出来自的图像/tmp(注意模式周围的单引号以防止 shell 扩展 glob 运算符*)。