Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Say I have a directory structure that looks like this:

file1.txt
folder1/
    file2.txt
folder2/
    file3.txt

How can I concatenate file1.txt, file2.txt, and file3.txt into a single file?

Edit: This directory structure is only an example. The question is about how to concatenate files from subfolders given any directory structure and any number of files.

share|improve this question

5 Answers 5

cat file1.txt folder1/file2.txt folder2/file3.txt > single.txt
share|improve this answer
    
+1 faster than I could get it –  MaQleod Aug 30 '11 at 20:35
1  
Perhaps I wasn't clear. I don't mean that I want to concatenate these exact three files. That directory structure was just a simplistic example. Imagine I have 50 subfolders and each of them has 10 subfolders. How would I concatenate all files from those subfolders without knowing their exact locations? –  Joseph Mornin Aug 31 '11 at 12:55

copy /y file1.txt+folder1\file2.txt+folder2\file.txt destination.txt should do the trick

FYI the /y disables prompting

share|improve this answer

What OS are we talking about?

Assuming Windows, use the following commands (substituting file/foldernames as necessary):

C:\> ren > TARGET.TXT 2> nul
C:\> cd MyFolder
C:\MyFolder> for /R %i in (*) do copy /b \TARGET.TXT + "%i" \TARGET.TXT

The first line creates (or wipes out) the file C:\TARGET.TXT using built-in commands. The third command recurses through every file in the current directory and its subdirectories and appends them to the target file (note, it will not include hidden files). It uses the /b switch to copy them in binary format in case some files are not just quite plain-text.

share|improve this answer

Assuming that you wanted every file in every sub-directory, or that there is some distinguishing searchable identifier, I just got the following to work. Note that in this example I was using a port of the unix find and cat commands ported to a windows system. I don't know how old this particular Windows port is so mileage may vary.

find . -type f -exec cat {} ; | cat > bigfile.txt

When using Ubuntu 10.10 and the bash shell I found that the following syntax worked better both because find "found" bigfile.txt if it is created in the search path and because certain parts of the command needed to be escaped. Note the differences between this and the windows port are the single quotes around the brackets, a backslash before the semi-colon and creating the container file one directory up.

find . -type f -exec cat '{}' \; | cat > ../bigfile.txt
share|improve this answer
    
This give me a syntax error: -bash: syntax error near unexpected token `|' –  Joseph Mornin Sep 5 '11 at 12:21
    
I thought I had typed it the same, but the two computers were in different parts of the room so I must have been editing in my head and forgot what I did. The new edited version above just worked on Ubuntu 10.10, cat version 8.5, and find version 4.4.2. Sorry for the mix-up. –  Dennis Sep 6 '11 at 9:08
    
I also just realised that you could use -mindepth 2 (before the -type) and it will not find bigfile.txt in the search but would also probably miss other files in the top level directory. Not positive because it worked, but I'm not really familiar with mindepth –  Dennis Sep 6 '11 at 9:16
up vote 1 down vote accepted

I was able to get the following to work on Mac OS X:

find . -type f -exec cat {} > ../results.txt \;

However, I don't fully understand why it works, so it would be helpful if someone could explain.

share|improve this answer
1  
find in the current directory (.) all files with type file and execute cat [current filename]. cat send the file contents to the standard output which is redirected to ../results.txt. Finally, \; closes the -exec option. It would be more logical if > ../results.txt would be placed behind \;, the shell does the redirection to the ../results.txt file, not find. You can confirm that by running set -x before in a bash shell (it might work for your shell too) –  Lekensteyn Sep 8 '11 at 15:39
    
Thanks! Very helpful! –  Joseph Mornin Sep 21 '11 at 12:55

Your Answer

 
discard

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

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