How to batch modify file names in linux rename

06-08-2023

This article mainly explains how to batch modify file names in linux rename. The content of the explanation in the article is simple and clear, easy to learn and understand. Next, please follow the editor's ideas and slowly deepen, let's study and learn how to batch linux rename Modify the file name!


1. rename rename

Syntax
rename 's/src/dst/' * * can match any file, you can add restrictions (*.jpg)

2. Basic usage

This way meets the first strong>When matching strings, replace, and do not replace
Only match the first .txt

rename 's/.txt/.jpg/ ' *

1.jpg

**Adding $** will match the suffix

rename 's/.txt$/.jpg/' *


3. Add suffix

Add str after the file name of all files

rename 's/$/str/' *

Example rename 's/$/txt/' * Add txt after the file name


4. Batch delete suffix

Delete str

rename 's/str$//' *

Examplerename 's/txt$//' *Delete txt


5. Add prefix

Add str before the file names of all files

rename 's/^/str/' *

Examplerename 's/^/ hhh' * Add hhh before the file name


6. Delete the prefix

Delete the str< in front of the file name of all files /code>

rename 's/^str//' *

Example rename 's/^hhh//' * delete all files The string starting with hhh


after the nameThe string at the end of ^str
str$

Expansion: the usage of rename in linux shell

rename is divided into two Version, respectively C language version and Perl version

Format of C language version:

rename original string and new string file name

Example:

When the file to be modified is stored in the file folder under the subdirectory of the current directory, prepare to replace all the files containing linux with unix ones

(1) currently in the file file folder, the statement is rename linux unix *

(2) Currently in the parent directory of the file file, the statement is rename linux unix file/*

The style of the Perl version:

rename 's/original string/new string/' file name

Example:

The title is the same as the title of the C language version

(1) Currently in the file folder, the statement is rename 's/linux/unix/' *

(2) Currently in the parent directory of the file file, the statement is rename 's/linux /unix/' file/*

Note: The Perl version is basically used now


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us