1

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

Cheers

0

2 Answers 2

1

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.

0
1

this is how I'd do it using bash only

cd movie_directory
ls -1 | while read line
do
  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}"
done

assumptions:

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.

1
  • 2
    You might prefer for line in * rather than your ls | while read... construct Commented Feb 3, 2016 at 23:43

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.