Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to rename files in batch in Linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

How to rename files in batches in Linux, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this to learn, I hope you can gain something.

When you want to rename multiple files, rename is probably the easiest, safest, and most powerful command-line tool. The rename command is actually a Perl script that is preinstalled on all current Linux distributions.

The following is the basic syntax for the rename command.

The code is as follows:

rename [-v -n -f]

is a Perl-compatible regular expression that indicates which file to rename and how to do so. The regular expression is of the form's/old-name/new-name/'.

The '-v' option displays details of file name changes (e.g. XXX renamed YYY).

The '-n' option tells the rename command to display cases where the file will be renamed without actually changing the name. This option is useful if you want to simulate changing file names without changing them.

The '-f' option forces overwriting of existing files.

Let's look at a few examples of the rename command in action.

changing the file extension

Suppose you have many.jpeg image files. You want to change their names to.jpg. The following command will change the.jpeg file to *.jpg.

The code is as follows:

$ rename 's/\.jpeg$/\.jpg/' *.jpeg

Change upper case to lower case and vice versa

Sometimes you want to change the case of a file name, you can use the following command.

Change all files to lowercase:

The code is as follows:

# rename 'y/A-Z/a-z/' *

Capitalize all documents:

The code is as follows:

# rename 'y/a-z/A-Z/' *

Change file name pattern

Now let's consider a more complex regular expression with subpatterns. In PCRE, subpatterns are enclosed in parentheses, followed by a number (e.g.,$1,$2).

For example, the following command changes 'imgNNNN.jpeg' to 'danNNNN.jpg'

The code is as follows:

# rename -v 's/img_(\d{4})\.jpeg$/dan_$1\.jpg/' *.jpeg

img_5417.jpeg renamed as dan_5417.jpg

img_5418.jpeg renamed as dan_5418.jpg

img_5419.jpeg renamed as dan_5419.jpg

img_5420.jpeg renamed as dan_5420.jpg

img_5421.jpeg renamed as dan_5421.jpg

For example, the following command changes 'img_000NNNN.jpeg' to 'dan_NNNN.jpg'

The code is as follows:

# rename -v 's/img_\d{3}(\d{4})\.jpeg$/dan_$1\.jpg/' *jpeg

img_0005417.jpeg renamed as dan_5417.jpg

img_0005418.jpeg renamed as dan_5418.jpg

img_0005419.jpeg renamed as dan_5419.jpg

img_0005420.jpeg renamed as dan_5420.jpg

img_0005421.jpeg renamed as dan_5421.jpg

In the example above, the subpattern '\d{4}' captures four consecutive digits, and the four captured digits are $1, which will be used for the new filename.

Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report