Rename multiple files with /usr/bin/rename using regex
I have a lot of pdfs that I want to rename with /usr/bin/rename.
The files are named in the following pattern:
<rating> <a pretty long title> (<author> <year>).pdf
e.g.: +++ The discovery of some very interesting stuff (Dude 1999).pdf
rating: 1 to 5 '+' signs
year: numerical
They should be renamed into the following pattern:
<author>, <year> <rating> <a pretty long title>.pdf
e.g.: Dude, 1999 +++ The discovery of some very intere开发者_开发百科sting stuff.pdf
I tried to use /usr/bin/rename and wrote this command:
rename 's/(.*)\ (.*)\ \((.*)\ (.*)\).pdf/$3, $4 $1 $2.pdf/' *.pdf
However, the command does not consider that the rating always contains '+' signs and that the year is always numerical. How can I achieve this? I tried something like ([+]{1,5}) and ([0-9]{4}), but it didn't work.
Is rename actually able to interpret something other than (.*) as the input for the variables $1 ... $n?
Thanks for your help!
This works fine for me:
rename 's/(\+{1,5}) (.*) \((.*) ([0-9]{4})\).pdf/$3, $4 $1 $2.pdf/' -- \
'Dude, 1999 +++ The discovery of some very interesting stuff.pdf'
... however your question doesn't quote the error message, so it's hard to tell what might be wrong in your situation.
Just as a warning, there are two different versions of /usr/bin/rename that are widely found on Linux systems, and which have different syntaxes. I assume that you are using the Perl one, however, since your original command worked at all. That means that you can use any Perl expression to modify the name - see perlre for more details.
Unfortunately Fedora (it's my distro) has worthless version of rename.
But I have changed it for perl version of replace utility.
You can find it at CPAN get and untar archive and then:
# ./Build installdeps
# sudo ./Build install
!!! It actions replace original fedora rename: bin file and manual, but it can be reverted by yum reinstall and may be reverted at next fedora update Also you can install it separately or use alternatives.
加载中,请稍侯......
精彩评论