Merge all files from directory tree

I have a directory called /home/foobar/ which contain numerous text files in numerous sub directories. I need to generate a file which has the merged content of all the files inside this directory structure.

The order in which the files are merged does not matter. I can copy all the files to one directory first and merge then, but in that case the solution must take into account that the files do not have unique names.

3

2 Answers

It's a simple task for find and cat:

 find <path_to_files> -type f -print0 | xargs -0 -I {} cat {} >> merged

The content of all files in <path_to_files> and in all sub-folders will be added to merged. Therefore remove merged for each new run of the command.

0

This should work (replacing path/to/dir with the path to the base directory):

for i in $(find /path/to/dir -type "f" ); do cat "$i">>OUTPUT.txt;done
5

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like