如何使用 xargs 将 ls 通过管道传输到 cat 以便列出文件名?

Thu*_*fir 5 linux ls pipe xargs cat

为了用于cat输出*.txt特定目录中的文件,如何在每个文件之前插入文件名作为“中断”?

file #1
contents of file

file #2
contents of file
Run Code Online (Sandbox Code Playgroud)

理想情况下,作为易于记住的几个bash命令。

也可以看看:

为什么不将文件名列表通过管道传输到 cat 中?

0st*_*ne0 8

改进的答案;

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

find . -type f -print -exec cat {} \;
Run Code Online (Sandbox Code Playgroud)

您可以使用find带有多个-exec;的命令。

find . -type f -print -exec cat {} \;
Run Code Online (Sandbox Code Playgroud)
find . -type f -iname '*.txt' -exec basename {} \; -exec cat {} \;
Run Code Online (Sandbox Code Playgroud)

  • 由于其他原因,“find”非常有用,熟悉语法可能非常有用。 (2认同)

Shm*_*uel 7

@0stone0 答案的另一种选择您可以使用tailcat 的 insted:

tail -n +1 *
Run Code Online (Sandbox Code Playgroud)

输出:

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


==> file-2.txt <==
contents of file
Run Code Online (Sandbox Code Playgroud)

  • `-n +1` 将跟踪所有文件内容。 (2认同)