【问题标题】:how to pipe ls into cat with xargs so that filenames are listed?如何使用 xargs 将 ls 导入 cat 以便列出文件名?
【发布时间】:2021-03-20 21:29:09
【问题描述】:

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

file #1
contents of file

file #2
contents of file

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

另见:

Why not pipe list of file names into cat?

【问题讨论】:

  • 请澄清问题,'将文件名插入'中断'是什么意思?
  • 我只是想以某种方式打印文件名,因为ls foo 会导致。沿着这些线。

标签: linux pipe cat ls xargs


【解决方案1】:

改进的答案;

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

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

您可以将find 命令与multiple -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

【讨论】:

  • find 由于其他原因非常有用,熟悉语法可能非常有用。
【解决方案2】:

@0stone0 答案的另一个选项 你可以使用tailinsted of cat:

tail -n +1 *

输出:

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


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

【讨论】:

  • 优秀。我可以使用cat 或nl 而不是尾巴吗?或者,我想可能只tail 整个文件。
  • -n +1 将拖尾所有文件内容。
猜你喜欢
  • 2017-02-06
  • 2021-12-26
  • 1970-01-01
  • 2018-03-28
  • 2012-10-03
  • 2021-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多