In this article, we will explain how to rename files on Linux using the Rename Command.
The rename command provides greater file modification flexibility and it is commonly included in default Linux configurations. If your system lacks it, install it using Terminal.
- Use the following command for Debian, Ubuntu, Linux Mint, and their derivatives.
sudo apt install rename
- Use this command if you are using CentOS 7 or RHEL-
sudo yum install rename
- Run this command, in Arch Linux.
yay perl-rename ## or yaourt -S perl-rename
- Once installation is complete, you can use the rename command. Here’s its fundamental syntax:
rename 's/old-name/new-name/' files
- As an illustration, we’ll generate a new “filetorename” folder using the mkdir command. Subsequently, we will navigate to the directory and employ the touch command to craft five files inside it. Below are the commands:
mkdir filetorename cd filetorename touch file{1..5}.txt ls
- The ls command will display the contents of the current directory, which includes the five new files. To rename a single file named file1.txt, the command would appear as follows:
rename ‘s/file1/newfile1/’ file1.txt
- To alter the extensions of all files, for instance, to PHP, employ the following commands:
rename ‘s/.txt/.php/’ *.txt ls
- If the file you wish to rename is located in a different directory, include its path in your command. The command might appear as follows:
rename ‘s/.txt/.php/’ FILE/PATH
By following these steps, you can rename files on Linux using the rename command.