Why does this not work? “ls *.txt | xargs cat > all.txt” (all files into single txt document)

catpipexargs

Why does this not work?

ls *.txt | xargs cat > all.txt

(I want to join the contents of all text files into a single 'all.txt' file.)
find with -exec should also work, but I would really like to understand the xargs syntax.

Thanks

Best Answer

ls *.txt | xargs cat >> all.txt

might work a bit better, since it would append to all.txt instead of creating it again after each file.

By the way, cat *.txt >all.txt would also work. :-)

Related Question