Linux replaces filename method instances with the rename command batch
- 2020-06-03 09:03:53
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.
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, if I have the following file and I want to remove the @ symbol from all the files,
firstname.lastname@example.org email@example.com firstname.lastname@example.org email@example.com firstname.lastname@example.org
rename \@2x.png 2x.png *.png
rename 's/\@2x/2x/' *.png