简体   繁体   中英

linux command: show content of all files

I tried the two following cmd to show content of all files under current directory. I want to know why one works, the other does not.

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

find . | xargs cat # works

cat is just an example, it can be any cmd which takes a file name as its parameter.

---------------------------------Update---------------------------------

Here are some observation from my PC.

$ 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

For others that might see this, we found the issue in the comments. Hao's problem was that ls was an alias, causing issues piping xargs to cat.

With 'type ls', they saw it was aliased, and using '\\ls' to remove the alias solved the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM