2
votes

I have a set of folders, inside each folder I have several sub-folders. In each sub folder there is a file called result.txt I need all the result.txt to be copied to other location renaming result files.

folders:

 abc1
 efg1
 doc2
 ghih

sub-folders: in aaa1

1.abc1.merged
2.abc1.merged
3.abc1.merged
4.abc1.merged

in efg1

1.efg1.merged
2.efg1.merged
3.efg1.merged
4.efg1.merged
5.efg1.merged
...
...

so on all of the sub-folders contain result.txt in a single different folder with all the result files renamed to result1.txt,result2.txt etc.

I tried to set name of sub-folder as a variable in shell script and made a loop to go in to the sub-folder and copy the result.txt to other path and rename it by mv command.But only the result.txt file from one subdirectory each is copied not the all.

I tried with following commands:

cp  $folder/$subfolder/resu*txt ../tsv/$newfolder/

(I previously assigned variables)

mv  ../tsv/$newfolder/resu*txt ../tsv/$newfolder/results$tts.txt

(I defined $tts as number of subfolders in the folder)

This copied the result.txt from only first sub-folder in each of parent folders.

1
batch-file is related to Windows, so please adapt the tags! perhaps you meant spring-batch or bash, or even batch-processing?aschipfl
batch-processing actually. I will correct that. Thanks.anonymous

1 Answers

1
votes

I would say something like:

i=1
for dir in **/*;  # loop through files
do
    cp "$dir"/result.txt $your_path/result$i.txt
    ((i++))       # increment variable $i
done

The syntax **/* goes through all the subdirectories.

Test

$ mkdir a
$ cd a
$ mkdir a{1..4}
$ mkdir a{1..4}/a{1..4}
$ for f in **/*; do echo "$f --"; done

The last command returns

a1/a1 --
a1/a2 --
...
a4/a4 --