【发布时间】: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有效。谢谢你。 =)