Sunday, October 30, 2022

[SOLVED] cat-ing files of certain extensions in subdirectories

Issue

I have a directory parallel/ that contains subdirectories that contains files ending with *.en extensions.

Doing this will get me the list of files i need.

find parallel/ -name "*.en" -type f

Now I would need to cat all these files to get a combined file, i.e.

cat *.en > all.en

I've tried the following but it didn't work:

$ for i in (find parallel/ -name "*.en" -type f): do cat $i ; done
-bash: syntax error near unexpected token `('

$ for i in ((find parallel/ -name "*.en" -type f)): do cat $i ; done
-bash: syntax error near unexpected token `('

Is there a way for me to iterate through all subdirectories and "cat" all of them into a single file?


Solution

You are very close; just lacking a dollar sign.

To make bash evaluate a command and get the output; use $():

for i in $(find parallel/ -name "*.en" -type f); do cat $i ; done

$() is equivalent to, but better and safer than the older backtics

var=`cmd` #do not use!


Answered By - Chris Maes
Answer Checked By - David Goodson (WPSolving Volunteer)