Home > OS >  How to rename a group of similar files with only part of file name?
How to rename a group of similar files with only part of file name?

Time:05-09

I have a group of files:

Ursus_fibroblast_ACT_34_CT0_adak_HJ_EV_S45_L002_R1_001_val_1.fq.gzAligned.out.sam
Ursus_fibroblast_ACT_34_CT12_adak_HJ_EV_S49_L002_R1_001_val_1.fq.gzAligned.out.sam
Ursus_fibroblast_ACT_34_CT0_adak_HJ_EV_S45_L002_R1_001_val_1.fq.gzAligned.out.sam
Ursus_fibroblast_ACT_34_CT12_adak_HJ_EV_S49_L002_R1_001_val_1.fq.gzAligned.out.sam
Ursus_fibroblast_ACT_34_CT15_adak_HJ_EV_S50_L002_R1_001_val_1.fq.gzAligned.out.sam

I would like to rename them with only this part of the name plus the extension "Aligned.out.sam":

"ACT_34_CT15_adak_HJ_EV_S50" "ACT_34_CT12_adak_HJ_EV_S49" and so on.

I have a start of a script so far but I am unsure of what to do now....I am obviously new to coding so if you need more information please let me know.

files={*.sam}
for i in $files
    echo $i
    mv -i $i

CodePudding user response:

If your files are in the same directory this command will do the job.

for x in *.sam; do mv $x ${x%.sam}.Aligned.out.sam;done

CodePudding user response:

while read file
do
  target=${file//Ursus_fibroblast_}
  target=${target//_L002*}
  echo mv "$file" "$target.Aligned.out.sam" # this is an echo
done < <(find . -type f -name "*.sam")

mv ./Ursus_fibroblast_ACT_34_CT0_adak_HJ_EV_S45_L002_R1_001_val_1.fq.gzAligned.out.sam ./ACT_34_CT0_adak_HJ_EV_S45.Aligned.out.sam
mv ./Ursus_fibroblast_ACT_34_CT12_adak_HJ_EV_S49_L002_R1_001_val_1.fq.gzAligned.out.sam ./ACT_34_CT12_adak_HJ_EV_S49.Aligned.out.sam
mv ./Ursus_fibroblast_ACT_34_CT15_adak_HJ_EV_S50_L002_R1_001_val_1.fq.gzAligned.out.sam ./ACT_34_CT15_adak_HJ_EV_S50.Aligned.out.sam

CodePudding user response:

You could use a bash regex for capturing the part of interest:

#!/bin/bash

for f in *.sam
do
    [[ $f =~ ACT_[0-9] _CT[0-9] _adak_HJ_EV_S[0-9]  ]] &&
    mv "$f" "${BASH_REMATCH[0]}.Aligned.out.sam"
done
  • Related