There are two approaches when you rename files in Linux: via the command-line interface or the command-line. The command-line is also known as terminal. Terminal is an essential tool for administrating Linux servers, which provides Linux users some of the best productivity tools with full power.
Rename a file in Linux terminal using “mv” command
To rename a file in the Linux terminal, enter the following command at the command line.
[email protected]:~$ mv oldFileName.php newFileName.php
You can change the name of the file located in a different folder in the current folder.
[email protected]:~$ mv /path/to/file/oldFileName.php /path/to/file/newFileName.php
Note: Actually the mv command is used to move a file.
Renaming multiple files
You can use the rename tool and the for loop to rename multiple files on Linux terminal.
Rename multiple files using the “rename” tool
First we need to install the rename tool. Most linux distributions do not come with a rename tool. Run the following command in terminal to install the tool:
Install rename on Ubuntu / Debian
[email protected]:~$ sudo apt install rename
Install rename on CentOS / Fedora / AlmaLinux / Rocky Linux
[email protected]:~$ sudo yum install prename
Install rename on Arch Linux
[email protected]:~$ yay perl-rename ## or yaourt -S perl-rename
If you want to rename txt files to html, you can use the command below.
[email protected]:~$ rename 's/\.html$/\.php/' *.html
lowercase to UPPERCASE
You can use the following command to convert lowercase filenames to uppercase.
[email protected]:~$ rename 'y/A-Z/a-z/' *.jpeg
You can use the -n parameter to output the command you used in verbose.
[email protected]:~$ rename -n 's/\.html$/\.php/' *.html index.php renamed as index.html services.php renamed as services.html about.php renamed as about.html
Rename multiple files using the for loop
For example, we want to add dates to pdf file names.
[email protected]:~$ ls file1.pdf file2.pdf ... file3.pdf
The bash script below will add the date to all pdf files in the current folder.
for i in $(ls *.pdf); do mv $i $(basename $i .pdf)_$(date +%Y%m%d).pdf done
After running the code, we see that the filenames have changed.
[email protected]:~$ ls file1_20210210.pdf file2_20210210.pdf ... file3_20210210.pdf
Note: If you need help renaming multiple files, you can post a comment on this page.
Rename a file using right click
If you have the GUI, right-click the file you want to rename and click “Rename”.
Rename a file by pressing F2 key
If you have the GUI, click once on the file you want to rename and press F2.