簡體   English   中英

如何使用 xargs 將 pipe ls 放入 cat 以便列出文件名?

[英]how to pipe ls into cat with xargs so that filenames are listed?

對於使用cat到 output *.txt特定目錄中的文件,如何在每個文件之前插入文件名作為“中斷”?

file #1
contents of file

file #2
contents of file

理想情況下,作為一個容易記住的幾個bash命令。

也可以看看:

為什么不把 pipe 文件名列表轉換成 cat?

@0stone0 答案的另一個選項您可以使用 cat 的tail insted:

tail -n +1 *

Output:

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


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

改進的答案;

而不是單獨的-exec basename ,僅使用-print將顯示文件名:

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

您可以使用帶有多個-execfind命令;

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM