Reputation: 13
I have a directory containing 227 subdirectories, each of which contains a file with the same name (in.BV) that I would like to concatenate.
i.e.
Directory
---------Subdirectory1
---------------in.BV various other files
---------Subdirectory2
---------------in.BV various other files
etc
These files called in.BV contain only text, and I would like to simply concatenate them into a single long text file.
I'm a novice, and this is what I have tried so far:
for d in */
do
(cd $d cat *in.BV >> Newin.txt)
done
This gave me a blank Newin.txt
. This exceeds my current understanding of bash scripting.
Thanks in advance!
Richie
Upvotes: 1
Views: 2596
Reputation: 15613
Assuming you are in the Directory
directory:
cat */in.BV >Newin.txt
Assuming you are somewhere above Directory
:
shopt -s globstar
cat **/in.BV >Newin.txt
shopt -u globstar
This sets the globstar
shell option in bash
. With the option set, you may use **
to match any pathname (*
only matches up to the next /
in a pathname, but **
matches across /
).
After the cat
, which creates Newin.txt
in the current directory, I unset the globstar
option.
If you have a lot of files and if the shell has issues executing cat
because the command line becomes too long, then see varlogtim's answer.
With new information given in comments below (files are to be concatenated in order depending on the subdirectory name):
Assuming you are in Directory
:
cat Subdirectory{1..227}/in.BV >Newin.txt
Upvotes: 2
Reputation: 2187
Example:
root@dib:~# find ./ -type f -name in.BV
./test1/in.BV
./test1/test2/in.BV
Find with Exec:
root@dib:~# find ./ -type f -name in.BV -exec cat {} + |tee your_new_file.txt
asdf
asdf
Upvotes: 2