对于使用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";一些字符串"*"。日志