Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have hundreds of files in one directory, is there a simple command or pipes of command I can use to append them together? I don't want to use any loops.

share|improve this question

migrated from Jun 12 '11 at 21:24

This question came from our site for professional and enthusiast programmers.

whats wrong with for loop? you anyway will have a complexity of O(n) to get all the files in the folder. – zengr Jun 12 '11 at 21:10
@zengr: The difference is n−ceil(sum(length(name)+1 for name in names)/(ARG_MAX−4)) additional cat processes. – grawity Jun 13 '11 at 13:58
...Except that any sane shell has cat implemented as a builtin which is run when someone asks to run "/bin/cat", so the new process overhead isn't relevant within a shell loop. – dannysauer Jun 21 '11 at 19:56
cat * >/path/to/somewhere

don't do

cat * > toall.txt

because "toall.txt" is created before cat is started and you will get strange result, "cat"ing toall.txt into toall.txt.

if want cat in the current directory, you should use

cat [some_globbing] > file #or
cat * > .dotted_file

.dotted_file is not expanded by * globbing.

or for example

(ls *.txt | xargs cat ) > /some/file
share|improve this answer

If there aren't too many files:

cat * > /some/new/file


find . -exec cat {} + > /some/new/file
find . -exec cat {} \; > /some/new/file
share|improve this answer
It'd probably be worth noting that the first one is GNU find-specific, and works similarly to "find|xargs". :) – dannysauer Jun 21 '11 at 19:59
BSD find understands -exec ... + as well. – Ignacio Vazquez-Abrams Jun 21 '11 at 20:07
Is the BSD you're using running a GNU find? :) "find --version" If not, then I learned something today. – dannysauer Jun 21 '11 at 20:11
FreeBSD OS X – Ignacio Vazquez-Abrams Jun 21 '11 at 20:12
I'm not shocked that OSX's command line is similar to that of FreeBSD. ;) But the man page looks suffieciently different. I'm glad to see some portability; I know it does not work on my HP-UX or AIX boxes using their POSIX-compliant find. :) – dannysauer Jun 21 '11 at 20:21

Similar to Ignatio's suggestion:

rm /the/output && find . -print0 | xargs -0 cat >> /the/output

Without using xargs, you're apt to blow your maximum command line length. Without using -print0, you'll potentially have problems with files that have weird chars (like spaces) in the names. But that's also GNU-specific.

share|improve this answer

This will append all files to outfile:

for file in !(outfile); do
        cat "$file" >> outfile

You need to delete outfile first if it exists. This will however not catch the dot-files. If you want to catch those as well you can use the two patterns

.??* !(outfile)

to create your file list. It requires a dot-file to have at least length 3, hence excludes . and ..!

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.