The Most Useful Ways To Rename Files And Directories In Linux

How To Rename Files And Directories In Linux

The ability to change files and directories names is one of the primary skills that every Linux user needs. This article continues the Bash article series and shows how to use various ways like file manager, mv, and rename utilities in combination with finding and bash looping constructs. Improve your Linux skills in just 3 minutes!

Two ways are available for you to do this:

  • File manager.
  • Command-line terminal.

Using the file manager

Renaming files using Midnight Commander
Renaming files using Midnight Commander

One of the easiest ways of renaming files and directories in Linux for new users is using Midnight Commander.

Midnight Commander – is a console-based file manager cloned from the famous Norton Commander.

Midnight Commander interface
Midnight Commander interface

To install Midnight Commander under Ubuntu:

$ sudo apt-get update

$ sudo apt-get -y install mc

For CentOS/Fedora/RHEL, use a different package manager:

$ sudo yum -y install mc

To launch Midnight Commander, execute mc command.

You need to use keyboard arrows to move the file selector. To switch between left and right screens, you need to use the Tab key. You may use your mouse too, but you’re limited to select only files, which are visible on the screen.

To rename the file or directory, move the cursor on top of it and press F6. If you forgot the exact key, use your mouse to select Move/Rename from the File menu.

Renaming files using Midnight Commander (menu)
Renaming files using Midnight Commander (menu)

Next, let’s look at how we can rename the files and directories by using mv and rename commands.

“mv” command

The mv command helps you move or rename files and directories from the source to the destination location. The syntax for the command is the following:

$ mv [OPTIONS] source destination

The source and destination can be a file or directory.

To rename file1.txt to file2.txt using mv, execute the following command:

$ mv file1.txt file2.txt

To change the name of folder1 directory to folder2, use the following command:

$ mv folder1 folder2

Renaming multiple files at once

The mv utility can rename only one file at a time, but you can use it with other commands to rename more than one file. These commands include find and Bash for and while loops.

For example, let’s imagine you need to change the file extension for a specific file type in a directory. In the following example, we rename all HTML files and change their extension from html to php.

Here’s the example folder structure:

$ tree example
├── index.html
├── page1.html
├── page2.html
└── page3.html

0 directories, 4 files

Now, let’s use the following Bash for-loop construct inside the example directory:

$ cd example

$ for f in *.html; do
    mv "$f" "${f%.html}.php"

Here we stepped into the example directory. Next, we executed the mv command in Bash for-loop (the command between for and done keywords).

Here’s what’s happening:

  • The for-loop is walking through the files ending on the .html and putting every file name to the variable f.
  • Then mv utility changes extension of every file f from .html file to .php. A part of the expression ${f%.html} is responsible for removing the .html from the file name. A complete expression “${f%.html}.php” will add .php to the file name without .html part.

Here’s the expected outcome:

$ ls -l
total 0
-rw-r--r--  1 amaksimov  wheel  0 Dec  5 17:13 index.php
-rw-r--r--  1 amaksimov  wheel  0 Dec  5 17:13 page1.php
-rw-r--r--  1 amaksimov  wheel  0 Dec  5 17:13 page2.php
-rw-r--r--  1 amaksimov  wheel  0 Dec  5 17:13 page3.php

“find” command

Using find utility is one of the most common ways to automate file and directory operations in Linux.

In the example below, we are using the find to achieve the same goal and change file extension.

The find utility finds all files ending on .html and uses the -exec argument to pass every found file name to the sh shell script written in one line.

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

In the sh script-line, we set the variable f with the value of the file name f=”{}”, then we’re executing familiar mv command. A semicolon is used to split the variable set command from the mv command.

“rename” command

In some cases, it is easier to use the rename command instead of mv. And you can use it with regular expressions without combining with other Linux commands.

Here’s the syntax for the rename command:

$ rename [OPTIONS] regexp files

For example, let’s rename all .php files back to .html:

$ ls
index.php  page1.php  page2.php  page3.php

$ rename 's/.php/.html/' *.php

$ ls
index.html  page1.html  page2.html  page3.html

If you wish to print the names of the files that you have selected for renaming, you can use the following command:

$ rename -n 's/.html/.php/' *.html
rename(index.html, index.php)
rename(page1.html, page1.php)
rename(page2.html, page2.php)
rename(page3.html, page3.php)

$ ls
index.html  page1.html  page2.html  page3.html


In this article, you’ve learned how to change files and directories names in Linux using various ways. We hope, this article was helpful. If so, please, help us to spread it to the world.