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

青灯夜游
Release: 2019-03-13 14:05:39
Original
17384 people have browsed it

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
Copy after login

● Install the rename command on CentOS and Fedora

sudo yum install prename
Copy after login

● Install the rename command on Arch Linux

yay perl-rename ## or yaourt -S perl-rename
Copy after login

Basic syntax of the rename command:

rename [OPTIONS] perlexpr files
Copy after login

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
Copy after login

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
Copy after login

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)
Copy after login

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
Copy after login

Common examples of using the rename command

Replace spaces in file names with underscores

rename 'y/ /_/' *
Copy after login

Convert file names Convert the file name to uppercase for lowercase

rename 'y/A-Z/a-z/' *
Copy after login

rename 'y/a-z/A-Z/' *
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template