Home > Software engineering >  bash script to rearrange multiple file names correctly(go pro)
bash script to rearrange multiple file names correctly(go pro)

Time:06-16

I have multiple GoPro files in a directory that all need to be renamed and would like a bash script to do it. Basically I want to move the 3rd and 4th characters back to the 12th and 13th spot with dashes around it. The only other GoPro post I found puts it at the end of the file but I really need it in the middle like that. Thank you

Example

Original filename is:

GX010112_1647792633961.MP4

and I need it to look like:

GX0112_164-01-7792633961.MP4

CodePudding user response:

A possible answer with the rename command-line. It allows you to rename multiple files according to PERL regex.

#installation of rename
apt install rename

#rename your files
rename 's/^(.{2})(.{2})(.{8})(.*)/$1$3-$2-$4/' *

REGEX explanation:

The regex is structured as s/ MATCHING PATTERN / REPLACEMENT /

^: set position at the beginning of string.

(.{2}) : Match the first 2 characters and store it in $1.

(.{2}) : Match the following 2 characters (the 3th and 4th) and store it in $2.

(.{8}) : Match the following 8 characters (from the 5th to 12th included) and store it in $3.

(.*) : Match the rest of you string and store it in $4.

Hope it helps.

CodePudding user response:

After many hours I figured it out based on this thread: Rename several gopro files

for file in GX*; do
  file1="${file#*??}"
  file2=${file1#*??}
  file3=${file1#*??????}
  file4=${file1%*$file2}
  file5=${file2%*?????????????????}
  mv -v "$file" "${file5}${file4}${file3}"
done

I hope it comes in handy for any other GoProers out there!

  •  Tags:  
  • bash
  • Related