Hello I have several directories of this tendency:
cluster1_bin.101_res/
cluster1_bin.102_res/
cluster1_bin.103_res/
cluster1_bin.104_res/
in each of these direcories I have several files with ".gbk"
suffix like the following ones:
cluster1_bin.101_res/k141_734976.region001.gbk
cluster1_bin.101_res/k141_753945.region001.gbk
cluster1_bin.101_res/k141_743978.region001.gbk
and I need to replace everything that is before .region001.gbk for it's parent directory name
this is the desired output:
cluster1_bin.101_res/cluster1_bin.101_res.region001.gbk
cluster1_bin.101_res/cluster1_bin.101_res.region001.gbk
cluster1_bin.101_res/cluster1_bin.101_res.region001.gbk
Is there a way I can do that?
Thanks for your time.
CodePudding user response:
With a for
loop and Parameter Expansion and mv
to rename the files.
#!/usr/bin/env bash
for file in cluster1_bin.[0-9]*_res/*.gbk; do
path_name=${file%/*}
file_name=${file##*/}
region_with_extension=${file_name#*.}
echo mv -v "$file" "$path_name/$path_name.$region_with_extension" || exit
done
With your given input/data the output is:
mv -v cluster1_bin.101_res/k141_734976.region001.gbk cluster1_bin.101_res/cluster1_bin.101_res.region001.gbk
mv -v cluster1_bin.102_res/k141_753945.region001.gbk cluster1_bin.102_res/cluster1_bin.102_res.region001.gbk
mv -v cluster1_bin.103_res/k141_743978.region001.gbk cluster1_bin.103_res/cluster1_bin.103_res.region001.gbk
- Remove the
echo
If you think the output is correct somv
can rename the files.