简体   繁体   中英

Bash: renaming files based on their names

(Bash newb here; my apologies.)

I have a folder of a couple hundred files like

"Cool Stuff - Some Movie Series #14.mp4"
"Other Stuff - Some Movie Series #49.mp4"
"Further Stuff - Some Movie Series #48.mp4"

and so on.

I'd like to mass rename them to be, respectively

"14 Cool Stuff - Some Movie Series.mp4"
"49 Other Stuff - Some Movie Series.mp4"
"48 Further Stuff - Some Movie Series.mp4"

and so on.

Is there a straightforward way to do this?

(One solution that may or may not be promising is to use rename . For example, rename 's/Movie/Mxvxx/g' * would replace the vowels with x s. But what I'm unable to do with that strategy is assign the numbers that come between the # and the .mp4 to a variable that I then prepend to the filename. So I fear that rename is a misguided strategy.)

I assume the quotation marks are not part of the filename.

prename -n 's/(.*) #(.*)\.mp4/$2 $1.mp4/' *.mp4

Output:

Cool Stuff - Some Movie Series #14.mp4 renamed as 14 Cool Stuff - Some Movie Series.mp4
Further Stuff - Some Movie Series #48.mp4 renamed as 48 Further Stuff - Some Movie Series.mp4
Other Stuff - Some Movie Series #49.mp4 renamed as 49 Other Stuff - Some Movie Series.mp4

If output looks okay, remove -n .

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