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 use the rename command rename in Linux

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article introduces the knowledge about "how to use rename command in Linux". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

rename is used for file naming management, batch naming and regular expression support. There are two versions of rename command. One is C language version, which supports wildcard characters, and the other is Perl version. support regular expressions

Command Format:

Usage: rename [-v] [-n] [-f] perlexpr [filenames]

Parameter Description:

v Shows details of file renaming

nDoes not perform a rename, but simulates a rename and shows what happens, such as whether there will be a file conflict with the same name. Testing before renaming is useful.

f Force overwrite file with same name

Common Wildcard Description

? Represents an arbitrary character

* Represents an arbitrary character or string of characters

Regular expression symbols

^Match the start position of the input

$matches the end of the input

. Matches any character except newline

+ Matches the previous character one or more times For example,"zo+" matches "zoo" but not "z"

[a-z]Represents a range of characters, for example,"[a-z]" matches any lowercase letter character between "a" and "z."

[^m-z] Negative character interval. Matches characters that are not in the specified interval.

Example 1: Display rename details

Use the touch command to create 3 txt files, and use the rename command to rename all suffixes with txt to log in batches

The code is as follows:

touch a.txt

touch b.txt

touch c.txt

rename -v 's/.txt/.log/' *.txt

v parameter function, showing file renaming details

The code is as follows:

fdipzone@ubuntu:~$ rename -v 's/.txt/.log/' *.txt

a.txt renamed as a.log

b.txt renamed as b.log

c.txt renamed as c.log

Example 2: Testing if renaming encounters the same name

The code is as follows:

touch a.txt

touch b.txt

touch c.txt

touch a.log

touch b.log

rename -n 's/.txt/.log/' *.txt

n parameter function, does not perform renaming, but displays the same name as would occur if renaming were performed

The code is as follows:

fdipzone@ubuntu:~$ rename -n 's/.txt/.log/' *.txt

a.txt not renamed: a.log already exists

b.txt not renamed: b.log already exists

c.txt renamed as c.log

Example 3: Forced overwrite of files with the same name

The code is as follows:

touch a.txt

touch b.txt

touch a.log

touch b.log

rename -f 's/.txt/.log/' *.txt

If you use the v parameter, renaming fails because a file with the same name exists

The code is as follows:

fdipzone@ubuntu:~$ rename -v 's/.txt/.log/' *.txt

a.txt not renamed: a.log already exists

b.txt not renamed: b.log already exists

f parameter function, force overwrite file with same name

The code is as follows:

fdipzone@ubuntu:~$ rename -fv 's/.txt/.log/' *.txt

a.txt renamed as a.log

b.txt renamed as b.log

After execution, a.txt and b.txt are renamed a.log and b.log, overwriting the original a.log and b.log.

"Linux rename command rename use method" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

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