简体   繁体   中英

Linux rename function not being used correctly

I'm trying to use the rename command in a Terminal in Ubuntu to append a string to the beginning of some avi file names as follows.

rename -n 's/(\w)\.avi$/String_to_add__$1\.avi/' *.avi

So I expect the following:

String_to_add_MyMovie.avi

Problem is that when I run the command it appends the string to the end of the file name, so I end up with the following:

MyMovie_String_to_add_.avi

I'm not sure if I have the perlexpr syntax wrong or something else. Any insight is appreciated.

UPDATE:

Thanks for the suggestions, I tried the suggestions from alno and plundra and made the following modification:

rename -n 's/(\w+)\.avi$/String_to_add__$1\.avi/' *.avi

But now the file gets the string inserted in the middle of the name as follows:

My_String_to_add_Movie

My apologies though, I neglected to mention that the titles are preceded by 3 numeric values, so the file name nomenclature is {3 numbers}-My_Movie.avi so for example 001-My_Movie.avi . But I didn't think this would make a difference since I'm assuming \\w+ matches alphanumeric characters, might the '-' be the issue?

Haven't tried Christian's approach yet, I want to be able to use the rename command, or at least understand why it's not working before I try a different approach.

I don't think rename -n is standard. You could do this:

for i in *.avi; do mv $i String_to_add_$i; done

你只需要将单个字符与\\w匹配,你需要\\w+ ,所以完整的行将是:

rename -n 's/(\w+)\.avi$/String_to_add__$1\.avi/' *.avi

Correct version:

rename -n 's/(\w+)\.avi$/String_to_add__$1\.avi/' *.avi

You simply forgot + after \\w, so it tried to match only one character.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM