1

There are 4 folders in my current working directory. Each folder contains several files named with the following format :

HEXA DECA - Season 01 Episode 01 - name of episode
HEXA DECA - Season 01 Episode 02 - name of episode

I want to rename this to :

HE'XA DECA - S01E01 - name of episode
HE'XA DECA - S01E02 - name of episode

sed is bit too daunting for me. What's the best way to achieve this ?

1

I've found that vidir is the most flexible and transparent solution for these problems. There you just edit the text file with filenames by whatever means (in VIM or another editor), and changes apply.

1
0

Personally, I use mmv for this kind of task. Others use rename. My attempt (not tested, obviously, so try first with the -n option):

for dir in ....; do
    ( cd "$dir"; mmv -r "HEXA DECA - Season ?? Episode ?? - *" "HE'XA DECA - S#1#2E#3#4 - #5" )
done

I cd first into $dir instead of doing mmv "$dir/..." which is also possible. This is to avoid situations where your directory may contain mmv's special characters like ? or *.

0

rename command does what you need:

find . -type f -exec rename "s/^(.*\/)HEXA DECA - Season ([0-9]*) Episode ([0-9]*) (.*)\$/\$1HE'XA DECA - S\$2E\$3 \$4/" {} \; 

We need backslashes 'cos in single quotes ' can't be escaped, but in double quotes we must escape $ character.

Here we find all files in directory and sub-directories and trying rename them.

s/regexp/smth will substitute all regexp matches to smth.

^(.*\/) is first part of path, everything without base name, saved to \$1.

HEXA DECA - Season and Episode are parts what won't saved, but they are templates for matching.

([0-9]*)s are number of season and episode, saved to \$2 and \$3.

(.*)$ is the last part of string, contains episode name, \$4.

In second part of s/// we substitute \$Ns into pattern as we want see them.

1
  • Sorry for the first case without find — i didn't pay attention to subfolders.
    – ValeriyKr
    Jan 6 '17 at 10:18

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.