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
You can use rnm:
Explanation:
-rs
: replace string of the form/search_regex/replace_part/modifier
(\d)
and(\d+)
in(\d)x(\d+)
are two captured groupes (\1
and\2
respectively).More examples here.
Really cool lil diddy.
xargs -n2
makes it possible to print two arguments per line. When combined with Perl'sprint $_
(to print the $STDIN first), it makes for a powerful renaming tool.Results of
perl -pe 'print $_; s/OldName/NewName/' | xargs -n2
end up being:I did not have Perl's
rename
readily available on my system.How does it work?
find . -type f
outputs file paths (or file names...you control what gets processed by regex here!)-p
prints file paths to be processed by regex,-e
executes inline scriptprint $_
prints the original file name first (independent of-p
)-n2
prints two elements per linemv
gets the input of the previous lineif your linux does not offer rename, you could also use the following:
i use this snippet quite often to perform substitutions with regex in my console.
i am not very good in shell-stuff, but as far as i understand this code, its explanation would be like: the search results of your find will be passed on to a bash-command (bash -c) where your search result will be inside of $1 as source file. the target that follows is the result of a substitution within a subshell, where the content of $1 (here: just 1 inside your parameter-substituion {1//find/replace}) will also be your search result. the {} passes it on to the content of -execdir
better explanations would be appreciated a lot :)
please note: i only copy-pasted your regex; please test it first with example files. depending on your system you might need to change \d and \w to character classes like [[:digit:]] or [[:alpha:]]. however, \1 should work for the groups.
You forgot a dot in front of the asterisk:
On OpenSUSE, RedHat, Gentoo you have to use Perl version of
rename
. This answer shows how to obtain it. On Arch, the package is calledperl-rename
.Use mmv (mass-move?)
It's simple but useful: It uses
*
for any string and?
for any character in the match string and#X
in the replace string to refer to the X-th match.In your case:
Here
#1#2
represent the two digits which are captured by??
(match #1 and #2).So the following replacement is made:
mmv
also offers matching by[
and]
and;
.You can not only rename, but also move, copy, append and link files.
Read the man page linked above for more!
Personally, I use it for padding numbers such that numbered files appear in correct order when sorted:
file_?.ext
→file_0#1.ext
Edit: found a better way to list the files without using
IFS
andls
while still beingsh
compliant.I would do a shell script for that:
Previous script: