Rename multiple files in Linux (批量重命名)

本文介绍如何使用Linux下的rename命令批量重命名文件。通过Perl表达式支持正则表达式,可以方便地修改文件名,包括替换后缀等操作。文章提供了实际的例子说明rename命令的用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Rename multiple files in Linux

Important notes:

All examples of this article are tested successfully in Ubuntu/Debian environment.
And I have tested these examples also in RHEL/Non-Debian environment, but failed.
So, pls note about this.

-------------------------------------------------------------------------

Many of you may know the ways how to rename multiple files in Linux(such as by using awk , using mv together with for , etc...), But today, I want to share with you the way by using the command "rename " to change names of bulk files in Linux.

Let talk about the advantages of this "rename " command.

First of all, rename is actually a a perl script, so it support the perl REGEX , it's very helpful when we handle the multiple files.

Secondly, the scripts can be retrenched to be very simple, I mean, we can use one command line to achieve our targets.

-------------------------------------------------------------------------

rename:

Syntax:

rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]

-v
Verbose: print names of files successfully renamed.
-n
No Action: show what files would have been renamed.
-f
Force: overwrite existing files.
perlexpr
Perl Expression

 

Regular Expressions

^
matches the beginning of the line

$
matches the end of the line

.
Matches any single character

(character)*
match arbitrarily many occurences of (character)

(character)?
Match 0 or 1 instance of (character)

[abcdef]
Match any character enclosed in [] (in this instance, a b c d e or f) ranges of characters such as [a-z] are permitted. The behaviour of this deserves more description.

[^abcdef]
Match any character NOT enclosed in [] (in this instance, any character other than a b c d e or f)

(character)/{m,n/}
Match m-n repetitions of (character)

(character)/{m,/}
Match m or more repetitions of (character)

(character)/{,n/}
Match n or less (possibly 0) repetitions of (character)

(character)/{n/}
Match exactly n repetitions of (character)

/(expression/)
Group operator.

/n
Backreference - matches nth group

expression1/|expression2
Matches expression1 or expression 2. Works with GNU sed, but this feature might not work with other forms of sed.

/w
matches any single character classified as a “word” character (alphanumeric or “_”)

/W
matches any non-“word” character

/s
matches any whitespace character (space, tab, newline)

/S
matches any non-whitespace character

/d
matches any digit character, equiv. to [0-9]

/D
matches any non-digit character

-------------------------------------------------------------------------

As rename is a perl script you will need perl to run it, and here are some examples about how to use it.

Examples:

rename -v 's//.htm$//.html/' *.htm

This is going to change htm to html in every file ending with .htm in its name.

If you want to change the name of something like this:

-rw-r--r-- 1 bob bob 0 2011-05-18 19:33 1.txt
-rw-r--r-- 1 bob bob 0 2011-05-18 19:33 2.txt
-rw-r--r-- 1 bob bob 0 2011-05-18 19:34 3.txt
-rw-r--r-- 1 bob bob 0 2011-05-18 20:35 b.txt
-rw-r--r-- 1 bob bob 0 2011-05-18 20:35 c.txt
-rw-r--r-- 1 bob bob 0 2011-05-18 20:35 d.txt
-rw-r--r-- 1 bob bob 0 2011-05-18 20:35 e.txt

 

That is the output of ls -l, and are files created with touch by me for this examples.
Now lets say I want to add a more descriptive string to the name of these files like Thesis, so here we go.

rename -n 's/(/w{1})/.txt$/$1_thesis/.txt/' *.txt

Note: Using -n to make only a test and see if the result is what you want

1.txt renamed as 1_thesis.txt
2.txt renamed as 2_thesis.txt
3.txt renamed as 3_thesis.txt
b.txt renamed as b_thesis.txt
c.txt renamed as c_thesis.txt
d.txt renamed as d_thesis.txt

e.txt renamed as e_thesis.txt

 

As you see that is what I wanted, now lets suppose I only want to change the name to files with a number in the name and with a letter in it.

rename -n 's/(/d{1})/.txt$/$1_thesis/.txt/' *.txt

1.txt renamed as 1_thesis.txt
2.txt renamed as 2_thesis.txt
3.txt renamed as 3_thesis.txt

You can also match only the ones with non-digit names

rename -n 's/(/D{1})/.txt$/$1_thesis/.txt/' *.txt

And the output will be:

b.txt renamed as b_thesis.txt
c.txt renamed as c_thesis.txt
d.txt renamed as d_thesis.txt
e.txt renamed as e_thesis.txt

 

As you may see, it is just a "using the right regexp" thing.

In case you do not have rename on your system (I think non-Debian does not have ) you can use mv .

 

 

转自:http://scmbob.org/rename_multiple_files_in_linux.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值