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.txtI would like to concatenate the contents of the two files into a new file directly as a bash command.
13 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?
4One 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 "{}" > outputThe 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.txtCommand 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 $AAs you'll find that the output would be something like:
./tmp/configuration/configuration_dev.txt ./tmp/configuration/configuration_dev.txtThus the actual cat command under the hood will be:
cat ./tmp/configuration/configuration_dev.txt ./tmp/configuration/configuration_dev.txt > testing.txt 2