Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename files using regular expression in linux

I have a set of files named like:

Friends - 6x03 - Tow Ross' Denial.srt Friends - 6x20 - Tow Mac and C.H.E.E.S.E..srt Friends - 6x05 - Tow Joey's Porshe.srt 

and I want to rename them like the following

S06E03.srt S06E20.srt S06E05.srt 

what should I do to make the job done in linux terminal? I have installed rename but U get errors using the following:

rename -n 's/(\w+) - (\d{1})x(\d{2})*$/S0$2E$3\.srt/' *.srt 
like image 423
orezvani Avatar asked Aug 04 '12 15:08

orezvani


People also ask

Which command is used to rename a file in Linux?

Use the mv command to move files and directories from one directory to another or to rename a file or directory.

How do I rename a file in a directory in Linux?

A file can be renamed during a move process using the mv command. You simply give the target path a different name. When mv moves the file, it will be given a new name. For example, to move a file named student1.


1 Answers

You forgot a dot in front of the asterisk:

rename -n 's/(\w+) - (\d{1})x(\d{2}).*$/S0$2E$3\.srt/' *.srt 

On OpenSUSE, RedHat, Gentoo you have to use Perl version of rename. This answer shows how to obtain it. On Arch, the package is called perl-rename.

like image 148
Thor Avatar answered Sep 19 '22 15:09

Thor