Home >Operation and Maintenance >Linux Operation and Maintenance >How to use the rename command to rename files in Linux?

How to use the rename command to rename files in Linux?

青灯夜游
青灯夜游Original
2019-03-13 14:04:5217424browse

In the previous article [How to use the mv command to rename files in Linux], we introduced the method of using the mv command to rename files. In this article, we will continue to introduce another way to rename files. How to name files: Use the rename command to rename files. I hope it will be helpful to everyone.

How to use the rename command to rename files in Linux?

# The rename command is used to rename multiple files. This command is more advanced than mv as it requires some basic knowledge of regular expressions. It has two versions: C language version and perl version; different versions have different syntax.

The following will introduce how to use the perl version of the rename command. If you don't have the rename command installed for that version, you can easily install it using your distribution's package manager.

● Install the rename command on Ubuntu and Debian

sudo apt install rename

● Install the rename command on CentOS and Fedora

sudo yum install prename

● Install the rename command on Arch Linux

yay perl-rename ## or yaourt -S perl-rename

Basic syntax of the rename command:

rename [OPTIONS] perlexpr files

The rename command will rename all files according to the specified perlexpr regular expression.

Example:

Change all files with the extension .html to .php

rename 's/.html/.php/' *.html

You can use the -n parameter to print the files that need to be renamed files without having to rename them.

rename -n 's/.html/.php/' *.html

The output looks like this:

rename(file-90.html, file-90.php)
rename(file-91.html, file-91.php)
rename(file-92.html, file-92.php)
rename(file-93.html, file-93.php)
rename(file-94.html, file-94.php)

By default, the rename command does not overwrite existing files; passing the -f parameter allows overwriting of existing files.

rename -f 's/.html/.php/' *.html

Common examples of using the rename command

Replace spaces in file names with underscores

rename 'y/ /_/' *

Convert file names Convert the file name to uppercase for lowercase

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

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

Related video tutorial recommendations: "PHP Tutorial"

The above is all of this article Content, I hope it will be helpful to everyone’s study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to use the rename command to rename files in Linux?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn