I've a series of media files I'm trying to rename en mass.

001 - 01x01 - mediaTitle1.avi
002 - 01x02 - mediaTitle2.avi
003 - 01x03 - mediaTitle3.avi

What I'd like is to output:

Series Title S01 E01 - mediaTitle1.avi
Series Title S01 E02 - mediaTitle2.avi
Series Title S01 E03 - mediaTitle3.avi

I'm trying this:

for f in *01x*; do echo mv -v "$f" "Series Title S01 E{f#1}"; done

But it's failing. So I want to keep the end of the file and replace the beginning for each file

  • You can use the rename command. Filenames have spaces?
    – Sreeraj
    Commented Jan 22, 2015 at 15:56

2 Answers 2


You are close, but forgot(?) to put $ at variable expansion, try:

for f in  *01x*; do mv -v -- "$f" 'Series Title S01 E'"${f#*01x}"; done

Notice single quotes in target file due to spaces in filename.

  • Holy moly... It worked.. but I had to remove: "-v --" Not sure why...
    – Beertastic
    Commented Jan 22, 2015 at 16:05
  • @Beertastic -v is just verbose flag, and -- informs mv that it is the end of options and all next arguments will be filenames. This allow to pass files with - in their names, e.g. -abc.txt. It really should work with both those flags.
    – jimmij
    Commented Jan 22, 2015 at 16:08
  • @jimmij Neither -v nor -- are specified by POSIX, so it's possible Beertastic is on a platform that doesn't support them (e.g. Busybox, Solaris). Commented Jan 22, 2015 at 18:04
  • Isn't -- part of a general POSIX specification for all commands that do normal option processing? Probably it's just -v that had to be removed.
    – Barmar
    Commented Jan 22, 2015 at 20:19
cd /path/to/avidir; mkdir ../avidir2
pax -rwls '|.* - \(..\)x\(.. - \)|Series Title S\1 E\2|' *.avi ../avidir2

The above command will create hard-links to all of your files in another directory. This enables you to verify all of the changes are to your satisfaction before removing the files with the unwanted names.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .