0

I tried to open a lot of files (10,000+) with cat and them merging them into one like this:

cat * > ~/Desktop/lol.xml 

But it returned this error:

-bash: /bin/cat: Argument list too long 

This means that its too long/large, what other way could I do this?

2
  • You could check the answer here. Commented Nov 10, 2014 at 16:25
  • 1
    Also, some more useful read from here as well. Commented Nov 10, 2014 at 16:30

2 Answers 2

4
find . -maxdepth 1 -type f --exec cat {} + > ~/Desktop/lol.xml 

This calls cat with the maximum possible number of arguments. For the remaining arguments new instances of cat are started.

4
  • Huh, that was quick, could you provide an explanation for this? It seems like it launches another cat process for every file. Commented Nov 10, 2014 at 16:25
  • 2
    -maxdepth is not POSIX, you can use find ! -name . -prune -type f instead. Commented Nov 10, 2014 at 16:28
  • Is this compatible with FreeBSD (OS X) tools? Just checking before. Commented Nov 10, 2014 at 16:33
  • @DisplayName I don't know. -exec + is part of the standard but has not always been. If that doesn't work then you can get a similar effect with find ... -print | xargs -d '\n' cat >... assuming there are no newlines in the file names. Commented Nov 10, 2014 at 17:21
3

There's a limit for the number of arguments a command can take. A workaround is to use a for loop :

for file in *; do cat "$file"; done 

The maximum can be displayed with :

$ getconf ARG_MAX 
2
  • 1
    One cat instance per file, really? Commented Nov 10, 2014 at 16:26
  • For a one-shot interactive command, this is sufficient I think. Commented Nov 10, 2014 at 16:45

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.