如何用xargs将ls管道传输到cat中,以便列出文件名



对于使用cat输出特定目录中的*.txt文件,如何将文件名插入为"中断";在每个文件之前?

file #1
contents of file
file #2
contents of file

理想情况下是作为几个易于记忆的bash命令

另请参阅:

为什么不将文件名的列表导入cat?

@0stone0答案的另一个选项你可以用tail代替cat:

tail -n +1 *

输出:

==> file-1.txt <==
contents of file

==> file-2.txt <==
contents of file

改进的答案;

只使用-print将显示文件名:,而不是单独的-exec basename

find . -type f -print -exec cat {} ;

您可以将find命令与多个-exec一起使用;

find . -type f -iname '*.txt' -exec basename {} ; -exec cat {} ;
-> ls
a.txt  b.txt
-> find . -type f -iname '*.txt' -exec basename {} ; -exec cat {} ;
b.txt
b
a.txt
a

如果您想在文件列表中搜索字符串,那么这应该是最好的选择。这将打印文件名以及

grep";一些字符串"*"。日志

最新更新