I'm trying to concatenate files from three different subdirectories into one file. The names of the files in each subdirectory are exactly the same. I'd like to use a loop to iterate through the subdirectories, and then place an iteration number into the newly named concatenated file, in a new directory. For example a directory structure like:
Foo
|||
||Bar3
|Bar2
Bar1
Inside each Bar(?) folder are files named: File1, File2, File3
I'd like to concatenate the Files with the same names to a larger file with a new name including a number:
cat Foo/Bar1/File1 Foo/Bar2/File1 Foo/Bar3/File1 > /combined_files/all_file1
cat Foo/Bar1/File2 Foo/Bar2/File2 Foo/Bar3/File2 > /combined_files/all_file2
cat Foo/Bar1/File3 Foo/Bar2/File3 Foo/Bar3/File3 > /combined_files/all_file3
From the Foo
directory I can use:
for number in {1..3}
do
cat Bar1/File$number\_* Bar2/File$number\_* Bar3/File$number\_* > combined_files/'all_files'$number
done
exit
But I need to have a more universal script, for larger numbers of Bar directories, and Files. I want something like
files=`ls ./Run1/ | wc -l` #to count the number of files and assign a number
For n in {1..$files}
do
cat Bar1/File$n\_* Bar2/File$n\_* Bar3/File$n\_* > combined_files/'all_files'$n
done
But I'm stuck.
Bar1/File2
always appear in the concatenated output beforeBar2/File1
? This will affect whether it can be accomplished using a simple one-liner, or whether a more complicated (perhaps recursive) result will be required.