Explain how to use the Rename command rename in Linux

Source: Internet
Author: User
Tags regular expression touch touch command

Rename is used for naming management of files, for batch naming and support of regular expressions, rename commands exist in two different versions. One is the C language version that supports wildcards, and the other is the Perl version. Support for regular expressions

Command format:

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

Parameter description:

V Show details of file renaming

n does not perform a rename, but it simulates the execution of the rename and displays what will happen, such as whether there will be a file conflict with the same name. Testing is useful before renaming.

F Force overwrite file with same name

Common wildcard Description

? Represents an arbitrary character

* denotes one or a string of any characters

Common Regular Expression Symbol description

^ match the start position of the input

$ match end of input

. Match any character except the line feed

+ Match the previous character one or more times for example, "zo+" can Match "zoo", but does not match "Z"

[A-z] represents a range of characters, for example, "[A-z]" that matches any of the lowercase alphabetic characters between "a" and "Z".

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

Example 1: Show rename details

Use the Touch command to create 3 txt files, use the Rename command to bulk all suffixes to txt rename log

The code is as follows:

Touch A.txt

Touch B.txt

Touch C.txt

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

V parameter action, display file rename 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: Test rename 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 action, does not perform renaming, but displays the same name if the rename occurs

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: Force overwrite file with 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 the V parameter is used, the rename fails because a file of 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 action, 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 renamed A.log and B.log, covering the original a.log with B.log

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.