Search code examples
bashunixawksedfile-rename

Renaming multiple files in a folder in unix


I have some multiple files in a folder I want to shorten the names. Here are the input files

Input

S_12_O_319_K27ac_S12818.sorted.bam
S_12_O_319_K27me3_S12815.sorted.bam
S_12_O_319_K4me1_S12816.sorted.bam
S_12_O_319_K4me3_S12817.sorted.bam
S_14_AS_11_K27ac_S12843.sorted.bam
S_14_AS_11_K27me3_S12840.sorted.bam
S_14_AS_11_K4me1_S12841.sorted.bam
S_14_AS_11_K4me3_S12842.sorted.bam
S_12_O_319_K27ac_S12818.sorted.bam.bai
S_12_O_319_K27me3_S12815.sorted.bam.bai
S_12_O_319_K4me1_S12816.sorted.bam.bai
S_12_O_319_K4me3_S12817.sorted.bam.bai
S_14_AS_11_K27ac_S12843.sorted.bam.bai
S_14_AS_11_K27me3_S12840.sorted.bam.bai
S_14_AS_11_K4me1_S12841.sorted.bam.bai
S_14_AS_11_K4me3_S12842.sorted.bam.bai

Output

S_12_O_319_K27ac.bam
S_12_O_319_K27me3.bam
S_12_O_319_K4me1.bam
S_12_O_319_K4me3.bam
S_14_AS_11_K27ac.bam
S_14_AS_11_K27me3.bam
S_14_AS_11_K4me1.bam
S_14_AS_11_K4me3.bam
S_12_O_319_K27ac.bam.bai
S_12_O_319_K27me3.bam.bai
S_12_O_319_K4me1.bam.bai
S_12_O_319_K4me3.bam.bai
S_14_AS_11_K27ac.bam.bai
S_14_AS_11_K27me3.bam.bai
S_14_AS_11_K4me1.bam.bai
S_14_AS_11_K4me3.bam.bai

Note that my files have two different extensions, one is *.bam another is *.bam.bai. I want to rename of all of them at once to shorten the name. Remove the portion _S12843.sorted from all of them. Note that this is the 5th underscore while the number following _S12843 is different for different files. Only similar pattern is the string sorted. So would like to truncate that entire portion to shorten the name. How can I achieve that with a bash or rename or sed . Any help would be appreciated. I am able to remove the string with sorted but not the numbers.


Solution

  • Using rename utility you can do:

    rename 's/_[^_.]+\.sorted//' *.sorted.*
    

    If you don't have rename then use this for loop:

    for f in *.sorted.*; do
       mv "$f" "${f/_S[[:digit:]]*.sorted}"
    done