代码之家  ›  专栏  ›  技术社区  ›  ashish_k

Linux下的输出格式化

  •  -1
  • ashish_k  · 技术社区  · 8 年前

    ls -l 使用格式 find 命令,如下所示:

    find . -type f -name "sample[0-9]*?_bill[0-9]*?.pdf" -mtime 0 -exec ls -l {} \;

    上述命令的输出为:

    -rw-r--r--. 1 root root 17843 Aug 20 08:02 ./sample32_bill45.pdf
    -rw-r--r--. 1 root root 17840 Aug 20 08:02 ./sample80_bill46.pdf
    

    -rw-r--r--. 1 root root 17843 Aug 20 08:02  sample32_bill45.pdf
    -rw-r--r--. 1 root root 17840 Aug 20 08:02  sample80_bill46.pdf
    

    我想不出如何移除 ./ 从第9列输出的一部分,这样我就可以得到所需的输出。

    3 回复  |  直到 8 年前
        1
  •  1
  •   RavinderSingh13 Nikita Bakshi    8 年前

    尽管我试着用 find 命令本身无法获得仍在显示的选项 ./ 在输出中,添加 awk 具有 找到

    find  -type f -name "sample[0-9]*?_bill[0-9]*?.pdf" -mtime 0 -exec ls -l {} \+ | awk '{sub("./","",$NF)} 1'
    

    或者(如果您的文件名称中有空格,则可以使用以下命令)

    find  -type f -name "sample[0-9]*?_bill[0-9]*?.pdf" -mtime 0 -exec ls -l {} \+ | awk '{sub("./","")} 1'
    

    输出如下(示例/虚拟文件)

    -rw-rw-r-- 1 singh singh 0 Aug 20 04:08 sample32_bill4524242424.pdf
    -rw-rw-r-- 1 singh singh 0 Aug 20 04:08 sample32_bill452424.pdf
    -rw-rw-r-- 1 singh singh 0 Aug 20 04:08 sample32_bill45.pdf
    


    编辑: 因为使用了glob sample[0-9]*?_bill[0-9]*?.pdf 不是用空格捕捉文件名(我通过创建一个名为 sample 32_bill 4524242424.pdf sample 文件名中可以有空格,下面的内容可能会有所帮助(感谢tripleee sir,他在本答案的注释部分提到)。

    find  -type f -name "sample*" -mtime 0 -exec ls -l {} \+ | awk 'match($0,/.*\.\//){print substr($0,RSTART,RLENGTH-2) substr($0,RSTART+RLENGTH)}'
    
    OR
    
    find  -type f -name "sample*" -mtime 0 -exec ls -l {} \+ | sed 's%\./%%'
    

    输出如下。

    -rw-rw-r-- 1 singh singh 0 Aug 20 04:47 sample 32_bill 4524242424.pdf
    -rw-rw-r-- 1 singh singh 0 Aug 20 04:08 sample32_bill4524242424.pdf
    -rw-rw-r-- 1 singh singh 0 Aug 20 04:08 sample32_bill452424.pdf
    -rw-rw-r-- 1 singh singh 0 Aug 20 04:08 sample32_bill45.pdf
    
        2
  •  3
  •   PesaThe    8 年前

    现在的 目录 . ,您可以使用 %P 指示 -printf

    find . -type f ... -printf '%P\0' | xargs -0 ls -l
    

    man find :

    -printf format ( %第页 )

    文件名及其所处起点的名称 发现已移除。

        3
  •  2
  •   anubhava    8 年前

    为了 OSX find 仅限:

    使用 -execdir -exec :

    find . -type f -name "sample[0-9]*?_bill[0-9]*?.pdf" -mtime 0 -execdir ls -l {} +
    

    + 而不是 \;


    以上行为已启用 OSX查找 . 对于 gnu find 使用以下脚本删除 ./ :

    find . -type f -name "sample[0-9]*?_bill[0-9]*?.pdf" -mtime 0 \
    -exec bash -c 'ls -l "${@#./}"' - '{}' +
    

    或者你可以用这个循环 find 具有 过程替代 :

    while IFS= read -d '' -r file; do
        ls -l "${file#./}"
    done < <(find . -type f -name "sample[0-9]*?_bill[0-9]*?.pdf" -mtime 0 -print0)
    
    推荐文章