Linux replaces filename method instances with the rename command batch

  • 2020-06-03 09:03:53
  • OfStack

preface

In Linux, you can use the mv command to modify filenames, but it can only operate on a single file. If you want to execute in batches, you have to write the shell script, which is executed iteratively with for statements. However, another command in Linux supports batch substitution of filenames, which is rename, and rename supports regular expression matching.

It is important to note that the rename command has different syntax formats in different Linux distributions.

grammar

The syntax used in Debian or Ubuntu is:


rename 's/stringx/stringy/' files

Under CentOS or RedHat:


rename stringx stringy files

The parameters of rename are divided into three parts:

stringx: Replaced string stringy: Replace string files: List of matching files

For example,

For example, if I have the following file and I want to remove the @ symbol from all the files,


uodong_pic@2x.png
eixin_pic@2x.png
inkehu_pic@2x.png
anting_pic-@2x.png
huanfa_pic@2x.png

CentOS writes:


rename \@2x.png 2x.png *.png

Ubuntu writes:


rename 's/\@2x/2x/' *.png

conclusion


Related articles: