I have a bunch of files in a single directory that I would like to rename as follows, example existing names:

1937 - Snow White and the Seven Dwarves.avi
1940 - Pinocchio.avi

Target names:

Snow White and the Seven Dwarves (1937).avi
Pinocchio (1940).avi



If you have the Perl based rename (sometimes known as prename) this is indeed possible. If you understand Regular Expresssions it's even straightforward.

rename -n 's!^(\d+) - (.*)\.(...)$!$2 ($1).$3!' *.avi

What this does is split the source filename into three components. Using your first example these would be

  • 1937
  • Snow White and the Seven Dwarves
  • avi

These are assigned to $1, $2, $3 within the rename command. (These are not bash variables.) It then puts them back together again in the different order.

When you are happy with the proposed result, change the -n to -v, or even remove it entirely.

| improve this answer | |

this is how I'd do it using bash only

cd movie_directory
ls -1 | while read line
  year=$(echo ${line} | awk '{print $1}')
  name=$(echo ${line} | cut -d " " -f 3- | cut -d"." -f 1)
  ext=$(echo ${line} | cut -d " " -f 3- | cut -d"." -f 2)
  newname=${name}" ("${year}")."${ext}
  mv "${line}" "${newname}"


file names do not contain .
the space characters between the year and - and the file name are actually white spaces, not tab or some other unprintable character.

| improve this answer | |
  • 2
    You might prefer for line in * rather than your ls | while read... construct – roaima Feb 3 '16 at 23:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.