Renaming files and directories is one of the most basic tasks you often need to perform on a Linux system.
Renaming a single file is easy, but renaming multiple files at once can be a challenge, especially for users who are new to Linux. You can use GUI file manager or command line terminal to rename files.
Simple Application Server
USD1.00 New User Coupon
* Only 3,000 coupons available.
* Each new user can only get one coupon(except users from distributors).
* The coupon is valid for 30 days from the date of receipt.
In this tutorial, we will show you how to use mv and rename commands to rename files and directories.
Use the mv command to rename the file
The mv command (short moving time) is used to rename or move files from one location to another. The syntax of the mv command is as follows:
mv [OPTIONS] source destination
Copy
The source can be one or more files or directories, and the destination can be a single file or directory.
If multiple file sources are specified, the destination must be a directory. In this case, the source file will be moved to the destination directory.
If you specify a single file as the source and the destination is an existing directory, the file will be moved to the specified directory.
To rename a file, you need to specify a single file as the source and a single file as the destination.
For example, to rename file1.txt to file2.txt:
mv file1.txt file2.txt
Copy
Use mv command to rename multiple files
The mv command can only rename one file at a time, but it can be used with other commands, such as find to rename multiple files in a bash for or while loop.
The following example shows how to use the Bash for loop to change the extension of all .html files in the renamed current directory to .php.
for f in *.html; do
mv - "$f" "${f%.html}.php"
done
Copy
Let's analyze the code line by line:
The first line creates a for loop and iterates through a list of all .html files.
The second line moves each item and file in the list to a new target and replaces .html with .php. The ${file%.html} part uses shell parameter expansion. Html is deleted from the file name.
done indicates the end of the loop segment.
We can also combine mv with the find command to achieve the same function as above.
find. -depth -name "*.html" -exec sh -c'f="{}"; mv - "$f" "${f%.html}.php"' \;
Copy
The find command passes all the files ending with .html in the current directory to the command -exec one by one. The string {} is the name of the file currently being processed.
As you can see from the above example, using the mv command to rename multiple files is not an easy task, because it requires you to have a good understanding of Bash scripts.
Use the rename command to rename the file
The rename command is used to rename multiple files. This command requires some more advanced basic knowledge of regular expressions than mv.
There are two versions of the rename command with different syntax. In this tutorial, we will use the perl version of the rename command. If this version is not installed on your system, you can easily install it using the distribution's package manager.
Install rename on Ubuntu and Debian
sudo apt install rename
Copy
Install and rename on CentOS and Fedora
sudo yum install prename
Copy
Install Rename on Arch Linux
yay perl-rename ## or yaourt -S perl-rename
Copy
The syntax of the rename command is as follows:
rename [OPTIONS] perlexpr files
Copy
The rename command renames all files from multiple files according to the specified perlexpr regular expression. You can read more about Perl's regular expressions here.
For example, the following command will change the file with the extension from .html to .php:
rename's/.html/.php/' *.html
Copy
You can use the -n parameter to print the name of the file to be renamed, instead of renaming the file, it can be used for simple tests.
rename -n's/.html/.php/' *.html
Copy
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
By default, the rename command will not overwrite existing files. Pass the -f parameter to allow overwriting of existing files.
rename -f's/.html/.php/' *.html
Copy
Here are some common examples of how to use the rename command:
Replace spaces in file names with underscores
rename'y/ /_/' *
Copy
Convert file name to lowercase
rename'y/A-Z/a-z/' *
Copy
Convert file name to uppercase
rename'y/a-z/A-Z/' *
Copy
At this point you should have a good understanding of how to rename files using the mv and rename commands. Of course, there are other commands to rename files in Linux, such as mmv. For new Linux users, you can use GUI batch renaming tools, such as Métamorphose.