Linux Commands

How to Rename Files and Directories in Linux

How to Rename Files and Directories in Linux

It’s a basic requirement to rename the files and directories on a Linux system. There are two ways to rename files using command-line terminal or via GUI file manager. In this tutorial, we will cover how to rename files and directories using mv and rename commands.

Rename Files using mv Command#

The mv command is a short name of move. We can use it to rename or move files from one location to another. Below is the syntax for the mv command:

mv [OPTIONS] source_path destination_path

In source_path, there can be single or multiple files or directories. In destination_path can have only one files or directory.

Let’s see an example for better understanding. Here, we will rename the file tns.txt to tecnstuff.txt, run:

mv tns.txt tecnstuff.txt

Rename multiple files with mv Command#

Using the mv command you can rename only one file at a time. You can use mv command with other commands such as find or inside bash for or while loops to rename multiple files.

In below example we used the Bash for loop to rename all .xls files to .xlsx in the current working directory.

for i in *.xls; do
    mv -- "$i" "${I%.xls}.xlsx"

Let’s see how the above code will work:

Following is the example of mv command with combination with find to do same process as above example:

find . -depth -name "*.xls" -exec sh -c 'f="{}"; mv -- "$f" "${f%.xls}.xlsx"' \;

Here, find command is gathering and passing all files which ends with .xls to the mv command using the -exec option. The string {} indicates currently processing file name.

Rename Files using rename Command#

To rename the multiple files you can use the rename command. It’s very easy and advanced then the mv command. There are two methods to use the rename command. Here, we will use the perl version of the rename command. You can easily install this version using package manager, if your system don’t have.

Following are the different commands for different distributions:

Install rename on Ubuntu and Debian#

sudo apt install rename

Installing rename on CentOS and Fedora#

sudo yum install prename

Install rename on Arch Linux#

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

Following is the basic syntax of rename command:

rename [OPTIONS] expression files

It will rename the files as per given regular expression. Visit perl regular expressions to learn more.

In this example, we will also change the files extension .xls to .xlsx:

rename 's/.xls/.xlsx/' *.xls

Use the -n option to print the file names which to be renamed.

rename -n 's/.xls/.xlsx/' *.xls

It will show the output like below:

rename(2018.xls, 2018.xlsx)
rename(2019.xls, 2019.xlsx)
rename(2020.xls, 2020.xlsx)

To overwrite the existing files you should use the -f option with rename command. The rename command doesn’t overwrite existing files by default.

rename -f 's/.xls/.xlsx/' *.xls

Following is the list of most used examples of how to use the rename command:

Convert filenames to lowercase#

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

Convert filenames to uppercase#

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

Replace spaces in filenames with underscores#

rename 'y/ /_/' *


I hope you successfully learned how to use the mv and rename command to rename the files and directories. You can also use the mmv command to rename file in Linux system.

If you have any questions or feedback, feel free to leave a comment.


Leave a Reply

Your email address will not be published. Required fields are marked *