In Linux, the mv command is often used to rename files, which is very convenient when renaming individual files. However, if we want to rename a set of files, MV is a bit weak. However, today we will introduce a useful batch rename command to implement the rename command.
Let’s take a closer look at the use of the rename command.
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 look at an example.
$ rename 's/old/new/' this.old
$ ls this*
this.newCopy the code
Where s specifies that we replace the first string with the second string, thus changing this.old to this.new.
Wouldn’t it be more convenient to use mv this.old this.new in the example above? True, but such a command can only rename one file at a time. What we’re going to do today is rename a group of files at once.
How do you deal with that? It’s easy. 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.newCopy the code
With this simple command, we can rename all files ending in.old in the current directory to files ending in.new.
If you think that’s all there is to the rename command then pattern Tucson is broken. The rename command is not limited to changing a file extension; it can also change any string in the file name. For example, if we wanted to change a file named report.* to review.*, we could use the following command:
$ rename 's/report/review/' *Copy the code
Note that the rules provided in the regular expression can change any part of the file name, be it the file name or the extension.
$ rename 's/123/124/' *
$ ls *124*
status.124 report124.txtCopy the code
If you want to use rename interactively to see what changes have been made and avoid incorrect changes, use the -v option.
$ rename -v 's/123/124/' *
status.123 renamed as status.124
report123.txt renamed as report124.txtCopy the code
The -v option gives you a preview when you want to change a text and a preview when you want to change a text, which is inefficient. What if I want to preview it holistically and change it all at once when I’m sure there’s nothing wrong?
We can use the -n or –nono options to rename the command.
$ 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)Copy the code
If you are comfortable with the above changes, you can formally change the file name by removing the -n option.
Note that the. In rename’s regular expression is not a full stop. Instead, it is a wildcard that matches any character.
$ 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)Copy the code
In the above example, not only is.old changed to.save, -old is also changed to.save.
If you want. To represent a period, you need to add an ‘escape’ symbol, using. Represents the 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)Copy the code
To change all uppercase letters to lowercase letters, we can use the following command.
$ 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)Copy the code
Where, use -n to preview the changes to be made and y to indicate changing case.
In the example above, we changed all file names starting with a capital W to lowercase.
conclusion
If you want to rename a single file, use the mv command. If you want to rename a group of files, the rename command is more convenient. Note that it is better to use the -n option in the rename command to preview the changes to be made and confirm them before renaming to avoid unexpected changes. —————–
I am good xu, the world’s top 500 foreign companies Linux development engineer, specializing in the production of Linux dry goods. Welcome to pay attention to my public account “Liang Xu Linux”, which shared the Linux introduction, basic, advanced and other series of tutorials, but also Git, Vim, open source projects and other technical goods. Public number background reply “1024” to obtain the latest and most complete technical information, reply “into the group” into the master such as cloud technology exchange group.