how to shift few words of filenames at a time using shell script

Hello everybody,

I have some files in directory. I want to shift 3 characters of filenames to the right at a same time.

for example, I have filenames like $ls -l
01_2000.G3.input.txt
02_2000.G3.input.txt
...,
...,
04_2010.G3.input.txt

I want to change the filenames like
2000_01_MFM_input.txt
2000_02_MFM_input.txt
...,
...,
2010_04_MFM_input.txt

Thins means I want to shift first 3 characters to the 8th position and I want to replace .G3. to _MFM_ also.

Any suggestions are welcome.

using bash:

for f in *input.txt; do echo "${f:3:4}_${f::3}MFM_${f: -9}"; done

Thanks for the reply.

This works fine. I could only see the changed filenames in a prompt mode.
Actual filenames are not changed.

I think I have to add something like >&

Please suggest. and if possible explain few steps. particularly about {f:3:4} and {f: -9}

I don't know what you want to do, so I just showed how to transform your filenames. You have to use cp or mv.

it's ${var:begin:length}, where begin is the beginning position, length is the number of characters.
you can read more about Parameter Expansion in the bash's manpage.

1 Like