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.

Asked By: aleksander_si

||

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?

Answered By: aleksander_si

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
Answered By: pa4080

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
Answered By: Kulfy

The Rust tool fd, which is a user-friendly alternative to find, makes this quite easy.

fd -X cat ; "configuration_dev.txt" . > output

-X is a batch executation of a command on all search results at once. The nice thing about this (since v8.4.0) is that it will not fail with the error Argument list too long.

Answered By: Michael Hall
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.