Use the rename command to rename the file and directory method records

Do we need to modify the file and directory names during the operation and maintenance of the Linux server? Some friends may have used the mv command. But it should be better to use rename at this time. Let's see how to use Rename command Rename files and directories.

 Use the rename command to rename the file and directory method record - Page 1

1. Install rename

 sudo apt update Sudo apt install rename # ubuntu or debian

If Centos is used:

 sudo yum install prename #Centos

2. Command Options

 rename [OPTIONS] perlexpr files

Here we look at the parameters.

 -v, --verbose Detailed: The plot name of the file that was successfully renamed. -0, --null When reading from STDIN, use  0 as the record separator -n, --nono Do nothing: Print the file name to be renamed, but do not rename it. -f, --force Overwrite: Allow overwriting existing files --path, --fullpath Rename full path: including any directory components. default -d, --filename, --nopath, --nofullpath Do not rename directory: only rename the file name part of the path -h, --help Help: Print feeds and options. -m, --man Manual: print the manual page -V, --version Version: displays the version number -E expression: code acting on file name You can build code repeatedly (such as "perl-e"). If there is no - e, the first parameter is used as code. -E statement: code that performs operations on file names, such as - e, but terminates at ';'

3. Rename common cases

Replace spaces in file names with underscores

 rename 'y/ /_/' *

Convert file name to lowercase

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

Convert file names to uppercase

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

Remove. bak from file name

 rename 's/\.bak$//' *.bak

Set the. jpeg and Rename the JPG file name to. jpg

 rename 's/\.jpe?g$/.jpg/i' *

Source: Lao Jiang Tribe » Use the rename command to rename the file and directory method records |Welcome to share (public account: Chiang's circle of friends)

Get new information from webmaster/add QQ group from the public account "Chiang Kai shek's circle of friends"【 one billion twelve million four hundred and twenty-three thousand two hundred and seventy-nine 】Get preferential promotion from merchants