On Linux, mv commands are commonly used to rename files, which is convenient for renaming individual files.However, if we want to rename a set of files, mv is a bit tired.But that's okay. Today we're going to introduce a useful command for batch renaming, the rename command.
Here's how to use the rename command in more detail.
Unlike the mv command, the rename command does not simply specify the old and new file names.Instead, it uses regular expressions similar to Perl.Let's start with an example.
$ rename 's/old/new/' this.old $ ls this* this.new
Where s is used to specify that we replace the first string with the second string, thereby changing this.old to this.new.
One might ask, is it not more convenient for us to use the mv this.old this.new command in the example above?Yes, but such a command can only rename one file at a time, and what we want to do today is rename a set of files at once.
What to do?Simply, here's an example:
$ ls *.old report.old schedule.old stats.old this.old $ rename 's/old/new/' *.old $ ls *.new report.new schedule.new stats.old this.new
As you can see from the above results, with this simple command, we can rename all the files at the end of.old to those at the end of.new in the current directory. This is simple and efficient!
If you think that's all the rename command is that the Tucson is broken.The rename command is not limited to changing file extensions, but can also change any string in the file name.For example, if you want to change the file named report. * to review. *, you can use the following command:
$ rename 's/report/review/' *
Note that the rules provided in regular expressions can change any part of a file name, whether it is a file name or an extension.
$ rename 's/123/124/' * $ ls *124* status.124 report124.txt
If you want to interactively rename to see what changes have been made to avoid incorrect modifications, you can use the -v option.
$ rename -v 's/123/124/' * status.123 renamed as status.124 report123.txt renamed as report124.txt
-vThe option is to preview a text when you want to change it, and a preview when you change it, which is inefficient.What if I want to preview it as a whole and make all the changes at once when I'm sure there's no problem?
We can use
renameThe command fulfills the above requirements.
$ rename -n 's/old/save/' * rename(logger.man-old, logger.man-save) rename(lyrics.txt-old, lyrics.txt-save) rename(olderfile-, saveerfile-) rename(oldfile, savefile) rename(review.old, review.save) rename(schedule.old, schedule.save) rename(stats.old, stats.save) rename(this.old, this.save)
If you feel comfortable with these changes, you can officially modify the file name by removing the -n option.
Note that the.In a rename regular expression is not a general English period, but a wildcard character that matches any character, which we can understand by referring to the following command.
$ rename -n 's/.old/.save/' * rename(logger.man-old, logger.man.save) rename(lyrics.txt-old, lyrics.txt.save) rename(review.old, review.save) rename(schedule.old, schedule.save) rename(stats.old, stats.save) rename(this.old, this.save)
In the example above, not only was.Old changed to.save, but -old was also changed to.save.
If you want. to represent a period, you need to add an \ escape symbol, even if you use \. to indicate an English period.
$ rename -n 's/\.old/\.save/' * rename(review.old, review.save) rename(schedule.old, schedule.save) rename(stats.old, stats.save) rename(this.old, this.save)
To change both uppercase and lowercase letters, we can use the following commands.
$ rename -n 'y/A-Z/a-z/' W* rename(WARNING_SIGN.pdf, warning_sign.pdf) rename(Will_Gardner_buttons.pdf, will_gardner_buttons.pdf) rename(Wingding_Invites.pdf, wingding_invites.pdf) rename(WOW-buttons.pdf, wow-buttons.pdf)
Here, use -n to preview the changes that will be made, and y to indicate case changes.
In the example above, we changed all file names starting with the capital letter W to lower case.
If you want to rename a single file, you can use the mv command.If you want to rename a set of files, it is more convenient to use the rename command. Note that it is best to use the rename command with the -n option to preview the changes that will be made, confirm that they are correct, and rename them to avoid accidental occurrences.
I am a promising Linux developer in the top 500 foreign enterprises in the world, specializing in the production of dry Linux products.Welcome to my public number "Licensed Linux", which shares a series of introductory, basic, advanced Linux tutorials, as well as technical dry goods such as Git, Vim, open source projects.Public number background reply "1024" to get the latest and most complete technical information, reply "Enroll" to enter the master such as cloud technology exchange group.