How To Rename Files And Directories In Linux

Andrei Maksimov
Andrei Maksimov
0
(0)

The ability to change files and directories names is one of the primary skills that all Linux users need. 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 (Midnight Commander).
  • Command-line terminal.

Any of provided recipes allow you to rename the blank file or existing file if required.

Midnight Commander (file manager)

The Midnight Commander is a free cross-platform file manager that provides an MS-DOS-style user interface to your Linux terminal. It is capable of running on Linux, FreeBSD, Mac OS X, and Windows. One of the many features offered by Midnight Commander is the ability to rename files. This can be done by selecting the file or files to be renamed and pressing F6. The user will then be prompted to enter the new name for the file or files.

Renaming files using Midnight Commander
Renaming files using Midnight Commander

Once the new name has been entered, the files will be renamed. Midnight Commander also offers a number of other features, such as the ability to copy, move, and delete files, as well as search for files by name or content.

Midnight Commander interface
Midnight Commander interface

To install Midnight Commander on Ubuntu Linux (you need to have Linux system administrator privileges):

$ 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 the 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 selecting only files, which are visible on the screen.

Rename single file

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)

Rename multiple files

The Midnight Commander is a perfect CLI program to rename files in Linux when you’re not familiar with command-line utilities. You can use it to rename multiple files in Linux too:

  1. Select required existing files using regular expression. Press + which will ask the regex to select files. For example, giving *.txt will select all the files with the TXT extension.
  2. Rename all the selected files using regex. Press F6 which will ask for the source and regex for target file names. For this example, provide *.txt in source and *.json in destination field to rename all *.txt files to *.json files.

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 and change file and directory names by moving them 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 single file or directory.

Rename one file

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 similar command:

$ mv folder1 folder2

Rename 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 multiple files. 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
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

# rename one or more files
$ for f in *.html; do
    mv "$f" "${f%.html}.php"
done

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 system.

In the example below, we are using the find to achieve the same goal and change the 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 the familiar mv command. A semicolon is used to split the variable set command from the mv command. In summary, a find command is a powerful tool you can use for renaming multiple files in Linux.

“rename” command

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

If you need to install rename utility, you can execute the following commands on the following Linux distributions:

  • Ubuntu: sudo apt-get update && sudo apt install rename
  • CentOS: rename utility is already included in your OS

Here’s the syntax for the rename command:

$ rename [OPTIONS] regexp files

For example, let’s change all file extensions from .php files back to .html:

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

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

Let’s execute the ls command to see the result of our previous command:

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

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

# change file extensions from .html to .php

$ 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)

Let’s execute the ls command to see the result of our previous command:

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

Nautilus File Manager

Nautilus is the graphical user interface for the GNOME desktop environment that makes it easy to manage your files. One of the things you can do with Nautilus is renaming a file. To do this, simply right-click on the file and select “Rename” from the drop-down menu. Enter the new name for the file and press “Enter.” The file will be renamed and you’ll see the new name in the file manager window. That’s all there is to it! With Nautilus, renaming a file is a quick and easy process.

Let’s rename the TXT file using Nautilus:

Rename TXT File using Nautilus

Summary

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.

How useful was this post?

Click on a star to rate it!

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Please, tell us what's wrong with this post, and we'll fix it ASAP!

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Want to be an author of another post?

We’re looking for skilled technical authors for our blog!

Leave a comment

If you’d like to ask a question about the code or piece of configuration, feel free to use https://codeshare.io/ or a similar tool as Facebook comments are breaking code formatting.