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

link|improve this question

feedback

3 Answers

up vote 5 down vote accepted

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. :-)

link|improve this answer
2  
The cat *.txt >all.txt is naturally better. Thanks – ajo Sep 28 '10 at 11:11
However, the ... | xargs cat >> all.txt or > all.txt always return error with xargs: unmatched single quote ... Is it because xargs takes everything after it as the command? – ajo Sep 28 '10 at 11:12
Do you have filenames with spaces? If so, then use something like "find /your/path -iname '*.txt' -print0 | xargs -0 cat >>all.txt" instead – Janne Pikkarainen Sep 28 '10 at 11:17
no, I replaced all the filename spaces with . But thinking of it, some filenames are likely to include single quotes as in listing_O'Connor.txt, this might be the problem! – ajo Sep 28 '10 at 11:29
Yes, that's the problem then. :) The easiest and the sanest way is to use find with -print0 combined with xargs -0 -- then the whole chain will use NULL character as a separator and whitespace and special characters will be taken care of automatically. – Janne Pikkarainen Sep 28 '10 at 11:37
show 6 more comments
feedback

If some of your file names contain ', " or space xargs will fail because of The separator problem http ://en.wikipedia.org/wiki/Xargs#The_separator_problem

In general never run xargs without -0 as it will come back and bite you some day.

Consider using GNU Parallel instead:

ls *.txt | parallel cat > tmp/all.txt

or if you prefer:

ls *.txt | parallel cat >> tmp/all.txt

Learn more about GNU Parallel http://www.youtube.com/watch?v=OpaiGYxkSuQ

link|improve this answer
feedback

all.txt is a file in the same directory, so cat gets confused when it wants to write from the same file to the same file.

On the other hand:

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

This will read from textfiles in your current directory into the all.txt in a subdirectory (not included with *.txt).

link|improve this answer
Still the following error: xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option – ajo Sep 28 '10 at 11:29
Do you have a .txt file with a singlequote in its name? – Jeremy Smyth Sep 28 '10 at 12:53
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.