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