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

vue-electron problem solution when using serialport

The error is as follows: Uncaught TypeError: Cann...

Using CSS to implement loading animation of Android system

There are two common loading icons on the web, on...

HTML tag meta summary, HTML5 head meta attribute summary

Preface meta is an auxiliary tag in the head area...

How to quickly build an FTP file service using FileZilla

In order to facilitate the storage and access of ...

How to create a web wireframe using Photoshop

This post introduces a set of free Photoshop wire...

Analysis of uniapp entry-level nvue climbing pit record

Table of contents Preface Hello World image Set b...

Summary of horizontal scrolling website design

Horizontal scrolling isn’t appropriate in all situ...

A detailed explanation of how React Fiber works

Table of contents What is React Fiber? Why React ...

Alibaba Cloud ESC Server Docker Deployment of Single Node Mysql

1. Download the accelerated version of msyql dock...

Use js to write a simple snake game

This article shares the specific code of a simple...

Web project development VUE mixing and inheritance principle

Table of contents Mixin Mixin Note (duplicate nam...

Example code for text origami effect using CSS3

Preface This article mainly shares with you an ex...