Raviteja
Raviteja

Reputation: 1

Append to a filename basing upon a pattern

I have files in Unix like

IMG_02052015.txt
IMG_12022015.txt

Now i want to rename the files to

IMG_02022015_01.txt

Can you provide a unix command for this. Itried grep and cut commands, but didnt work.

Please help me.

Upvotes: 0

Views: 28

Answers (1)

cnicutar
cnicutar

Reputation: 182684

This can be done with rename(1):

$ rename -n 's/\.txt/_01.txt/' * 
IMG_02052015.txt renamed as IMG_02052015_01.txt
IMG_12022015.txt renamed as IMG_12022015_01.txt

Actually the date pattern DDMMYYYY is common

You can use something like this:

$ rename -n 's/([0-9]{8})/$1_01/' *

You should be able to find a better pattern than [0-9]{8} for your date.

Upvotes: 1

Related Questions