【问题标题】:linux command: show content of all fileslinux命令:显示所有文件的内容
【发布时间】:2015-08-15 22:14:09
【问题描述】:

我尝试了以下两个 cmd 来显示当前目录下所有文件的内容。我想知道为什么一个有效,另一个无效。

ls | xargs cat # does not work, No such file or directory

find . | xargs cat # works

cat只是一个例子,它可以是任何以文件名为参数的cmd。

---------------------------------更新------------- --------------------

这是我电脑上的一些观察结果。

$ echo 1 > test1.txt
$ echo 2 > test2.txt
$ echo 3 > test3.txt

$ ls
test1.txt  test2.txt  test3.txt

$ ls *.txt | xargs cat
cat: test1.txt: No such file or directory
cat: test2.txt: No such file or directory
cat: test3.txt: No such file or directory

$ find .  -name '*.txt' | xargs cat
2
1
3

【问题讨论】:

  • 我无法重现您报告的行为。我猜这是您正在使用的文件名的一个功能。如果我在(例如)中使用带空格的文件名,那么我可以让ls | xargs cat 不起作用,但你给出的 find 命令也不起作用!所以你需要做更多的工作来弄清楚这个问题是在什么条件下发生的
  • @Vorsprung 感谢您的回复。我在原始帖子中添加了update,其中包含从我的电脑观察到的结果。请检查一下。
  • 是 ls 的别名吗?你得到的格式表明不是,但也许可以尝试使用 \ls 来确定......
  • @timofiend 你是对的......这是一个别名问题。 type ls 显示 ls is aliased to 'command ls --color'\ls 有效。谢谢你。 =)

标签: linux find ls xargs


【解决方案1】:

对于其他可能看到这一点的人,我们在 cmets 中发现了问题。 Hao 的问题是 ls 是别名,导致将 xargs 传递到 cat 的问题。

使用 'type ls',他们看到它有别名,使用 '\ls' 删除别名解决了问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-28
    • 2015-07-22
    • 2013-10-09
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-08
    相关资源
    最近更新 更多