我尝试了以下两个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
最佳答案
对于其他可能看到此内容的人,我们在评论中发现了该问题。 hao 的问题是 ls 是一个别名,导致将 xargs 管道传输到 cat 时出现问题。
使用“type ls”,他们发现它是别名,使用“\ls”删除别名解决了问题。
关于Linux命令: show content of all files,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31997536/