Explain how to rename a group of files in batches at one time on Linux

  • 2021-07-09 09:41:41
  • OfStack

In Linux, we usually use the mv command to rename files, which is very convenient when renaming individual files. However, if we want to rename a group of files, mv is a little weak. However, it doesn't matter. Today, we will introduce a good command that can realize batch renaming-rename command.

Let's introduce the usage of rename command in 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 look at an example first.


$ rename 's/old/new/' this.old
$ ls this*
this.new

Where the role of s is to specify that we 使用第2个字符串替换第1个字符串 Thus replacing this. old with this. new.

Some people may ask, in the above example, we use mv this.old this.new Isn't this order more convenient? Yes, but this command can only rename one file at a time, and what we are going to do today is to rename one group of files at a time.

How to deal with it? Quite simply, let's look at the following 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

From the above results, we can see that through the operation of this simple command, we can rename all files ending in. old to files ending in. new in the current directory, which is simple and efficient!

If you think that's the whole rename command, then the pattern Tucson is broken. The rename command is not limited to changing file name extensions, but can also change any string in the file name. For example, if we want to change the file named report.* to review.*, we can use the following command:


$ rename 's/report/review/' *

Note that the rules provided in the regular expression can change any part of the file name, whether it is the file name or the extension.


$ rename 's/123/124/' *
$ ls *124*
status.124 report124.txt

If you want to rename using rename interactively to see what changes have been made and avoid wrong modifications, you can use the-v option.


$ rename -v 's/123/124/' *
status.123 renamed as status.124
report123.txt renamed as report124.txt

-v option is to give you a preview when changing 1 article, and then give you a preview when changing 1 article, which is inefficient. If I want to preview it as a whole, how can I modify it all at once when it is confirmed that there is no problem?

We can use the-n or-nono option to make the rename command fulfill 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 the above changes, you can remove the-n option to formally modify the file name.

Note that in the rename regular expression. is not a 1-like English period, but a wildcard character that matches any character. We can refer to the following command for understanding.


$ 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 above example, not only. old is changed to. save, but-old is also changed to. save.

If you want. to mean a period, you need to add a\ escape symbol, that is, use\. to mean 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 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)

Where-n is used to preview the changes to be made, and y indicates the change case.

In the above example, we changed all file names beginning with uppercase letters W to lowercase letters.

Summarize

If you want to rename a single file, you can use the mv command. If you want to rename a group of files, it is more convenient to use rename command. Note that it is best to add-n option to use rename command, preview the changes to be made first, and then rename them after confirmation, so as to avoid accidents.


Related articles: