5

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.

5 Answers 5

7
cat file1.txt folder1/file2.txt folder2/file3.txt > single.txt
2
  • +1 faster than I could get it
    – MaQleod
    Aug 30, 2011 at 20:35
  • 2
    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?
    – Joe Mornin
    Aug 31, 2011 at 12:55
4

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.

3

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

FYI the /y disables prompting

3

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
3
  • This give me a syntax error: -bash: syntax error near unexpected token `|'
    – Joe Mornin
    Sep 5, 2011 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, 2011 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, 2011 at 9:16
2

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.

1
  • 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, 2011 at 15:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .