TrumanWong

rename

Change file names in batches using string replacement

Supplementary instructions

There are two versions of the rename command with differences in usage.

C language version, supports wildcard characters
[Explanation of common wildcard characters]
? represents an arbitrary character
* represents one or a string of arbitrary characters

Perl version, supports regular expressions
[Explanation of commonly used regular expression symbols]
^ matches the start of the input
$ matches the end of input
. Matches any character except newline characters
+ 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 alphabetic character between "a" and "z".
[^m-z] Negated character range. Matches characters not within the specified range.

Distinguishing method: rename --version

If the returned result contains util-linux, it means the C language version, otherwise it is the Perl version.

# Perl version | Ubuntu (18), Mint (20) default to the Perl version
$ rename --version
/usr/bin/rename using File::Rename version 1.10

# C language version | Centos(7) defaults to the C language version
$ rename --version
rename, from util-linux 2.23.2

grammar

# Perl version
rename [ -h|-m|-V ] [ -v ] [ -0 ] [ -n ] [ -f ] [ -d ] [ -e|-E perlexpr]*|perlexpr [ files ]

# C language version
rename [options] expression character to replace file...

Parameters

# Perl version
-v, --verbose
         Verbose: The printed name of the successfully renamed file.

-0, --null
         When reading from STDIN, use \0 as the record separator

-n, --nono
         Do nothing: Print the name of the file to be renamed, but do not rename it.

-f, --force
         Overwrite: allows overwriting existing files

--path, --fullpath
         Rename full path: include any directory components. default

-d, --filename, --nopath, --nofullpath
         Do not rename directory: only the filename portion of the path is renamed

-h, --help
         Help: Print feed and options.

-m, --man
         Manual: Prints the manual page.

-V, --version
         Version: Displays the version number.

-e expression: code to act on filenames.

         Code can be built repeatedly (like "perl -e"). Without -e, the first argument is used as the code.

-E statement: Code that performs an operation on a file name, like -e, but terminated by ';'.


# C language version
-v, --verbose
         Provides visual feedback on which files, if any, were renamed

-V, --version
         Display version information and exit.

-s, --symlink
         Perform a rename on a symbolic link target

-h, --help
         Show help text and exit

Example


Perl version

Rename 1.txt 2.txt to 1.log 2.log

$ rename -v "s/txt/log/g" 1.txt 2.txt
1.txt renamed as 1.log
2.txt renamed as 2.log

Modify file suffix

rename "s//.html//.php/" * # Change the .html suffix to .php suffix

Add file suffixes in batches

rename "s/$//.txt/" * # End all file names with txt

Delete file names in batches

rename "s//.txt//" * # Delete the .txt of all file names ending with .txt

C language version

Rename 1.txt 2.txt to 1.log 2.log

$ rename -v txt log 1.txt 2.txt
`1.txt' -> `1.log'
`2.txt' -> `2.log'

There are these files foo1, ..., foo9, foo10, ..., foo278 in the folder

# Rename the files from foo1 to foo9 to foo01 to foo09. The renamed files are only files with a 4-character name, and foo in the file name is replaced with foo0.
rename foo foo0 foo?

# All files from foo01 to foo99 are renamed to foo001 to foo099, only files with 5-character names are renamed, and foo in the file name is replaced with foo0.
rename foo foo0 foo??

# All files from foo001 to foo278 are renamed to foo0001 to foo0278, and all files starting with foo are renamed.
rename foo foo0 foo*

# All files from foo0200 to foo0278 are renamed to foo200 to foo278, and foo0 in the file name is replaced with foo.
rename foo0 foo foo0[2]*