How to concatenate files found by the find command in bash

Searching for files using the bash find command, I would like to concatenate the resulting files into a new file. For example, if the find command yields:

find . -name "configuration_dev.txt"
./tmp/configuration1/configuration_dev.txt
./tmp/configuration2/configuration_dev.txt

I would like to concatenate the contents of the two files into a new file directly as a bash command.

1

3 Answers

The command to achieve the desired result is:

find . -name "configuration_dev.txt" -exec cat > testing.txt {} +

A good explanation of the above line is provided here: What is meaning of {} + in find's -exec command?

4

One possible way to do this is by piping the output of find to xargs and concatenate the content of the files by cat, then you can redirect the output to a new file:

find . -name '*file*' | xargs -I{} cat "{}" > output

The above command will call cat for each file and then the entire output of the xargs statement will be redirected to the output file. The more effective way is to use null delimiter - thanks to @pLumo for this update:

find . -name '*file*' -print0 | xargs -0 cat > output
2

You can also open multiple files in cat and redirect the output to a new file. Since the names of multiple files would be returned by find, you can use that inside cat using `(backtick):

cat `find . -name "configuration_dev.txt"` > testing.txt

Command between backticks would be executed and replaced by the output of the command removing the trailing new line character and inserting a whitespace.

You can confirm this by running

A=`find . -name "configuration_dev.txt"`
echo $A

As you'll find that the output would be something like:

./tmp/configuration/configuration_dev.txt ./tmp/configuration/configuration_dev.txt

Thus the actual cat command under the hood will be:

cat ./tmp/configuration/configuration_dev.txt ./tmp/configuration/configuration_dev.txt > testing.txt
2

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