How to Rename a Group of Files at Once on Linux

How to Rename a Group of Files at Once on Linux

In Linux, we usually use the mv command to rename files, which is very convenient when renaming a single file. However, if we want to rename a group of files, mv is a bit powerless. But it doesn’t matter. Today we will introduce a useful command that can achieve batch renaming - the rename command.

Let's introduce the usage of the 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

Among them, s is used to specify that we使用第二個字符串替換第一個字符串, thereby changing this.old to this.new.

Someone may ask, isn’t it more convenient to use the command mv this.old this.new in the above example? That's right, but such a command can only rename one file at a time, and what we are going to do today is to rename a group of files at once.

How to deal with it? It's very simple, 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 this simple command operation, we can rename all files ending with .old in the current directory to files ending with .new. It is simple and efficient!

If you think that's all there is to the rename command then your code is broken. The rename command is not limited to changing file extensions, it can also change any string in a 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 use rename interactively to see what changes have been made to avoid making mistakes, you can use the -v option.

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

The -v option is to preview the text when you change a line, and preview it again when you change another line, but this is inefficient. What if I want to preview the whole thing and modify it all at once after confirming that there are no problems?

We can use the -n or --nono option to let the rename command achieve the above requirements.

$ rename -n 's/old/save/' *
rename(logger.man-old, logger.man-save)
rename(lyrics.txt-old, lyrics.txt-save)
rename(olderfile-, saverfile-)
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 are OK with the above changes, you can remove the -n option to formally change the file name.

Note that the . in the rename regular expression is not a regular period, but a wildcard that matches any character. We can refer to the following command to understand it.

$ 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 represent a period, you need to add a \ escape symbol, that is, use \. to represent 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/AZ/az/' 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)

Use -n to preview the changes that will be made, and y to change the case.

In the above example, we changed all file names that began with an uppercase 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 the rename command. Note that it is best to add the -n option when using the rename command to preview the changes to be made first, and then rename after confirming that they are correct to avoid accidents.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to Rename Multiple Files at Once in Linux

<<:  Pycharm2017 realizes the connection between python3.6 and mysql

>>:  MySQL group by method for single word grouping sequence and multi-field grouping

Recommend

Mysql join query principle knowledge points

Mysql join query 1. Basic concepts Connect each r...

VUE implements timeline playback component

This article example shares the specific code of ...

MySQL partitioning practice through Navicat

MySQL partitioning is helpful for managing very l...

MySQL uses the Partition function to implement horizontal partitioning strategy

Table of contents 1 Review 2 Five strategies for ...

Binary installation of mysql 5.7.23 under CentOS7

The installation information on the Internet is u...

Vue3 encapsulates the side navigation text skeleton effect component

Vue3 project encapsulation side navigation text s...

Implementation of MySQL joint index (composite index)

Joint Index The definition of the joint index in ...

How to implement a single file component in JS

Table of contents Overview Single file components...

What does the "a" in rgba mean? CSS RGBA Color Guide

RGBA is a CSS color that can set color value and ...

js+canvas realizes code rain effect

This article shares the specific code of js+canva...

CSS overflow-wrap new property value anywhere usage

1. First, understand the overflow-wrap attribute ...

Html Select uses the selected attribute to set the default selection

Adding the attribute selected = "selected&quo...

Detailed tutorial for downloading and installing mysql8.0.21

Official website address: https://www.mysql.com/ ...