bashfilenameslong-filenamesshort-filenames

What is wrong with my list naming code?


I would like to change the file name from Sub****_Ses1 to HU_TT_12_****_UU; (**** numbered from 0001 to 1600)

I did the below

#!/bin/sh
#Change file name 

Subj_id=/Users/dave/biomark/dat

cd Subj_id

for abcd in Sub****_Ses1; do

mv Sub$a$b$c$d_Ses1 HU_TT_12_$a$b$c$d_UU;

done

Solution

  • for and wildcards don't work like this. Use cut to extract the number.

    $ touch Sub000{1,2,3,4}_Ses1 
    $ for f in Sub????_Ses1      
    do
        abcd=$(echo $f | cut -b4-7)
        mv $f HU_TT_12_${abcd}_UU
    done
    $ ls HU_TT_12_000*
    HU_TT_12_0001_UU  HU_TT_12_0002_UU  HU_TT_12_0003_UU  HU_TT_12_0004_UU