Rename multiple Files in Ubuntu

Published: November 14, 2019

Rename multiple Files in Ubuntu

In this article, we would focus on how to rename multiple files in Ubuntu. Prior to that, its pretty straightforward if you intend to rename a file in Ubuntu. It can be done through both graphical user interface as well as command-line interface. For instance,

Lets say we have a file - 1.html. And, we want to rename it to 2.html.

Through graphical user interface (GUI), either select the file and Press F2 or right-click on the file and click Rename. Thereafter, a dialog box would appear and type 2.html.

Using command-line interface (CLI), open a terminal and issue the following -

mv 1.html 2.html

But, things get a bit different when it comes to renaming multiple files or directories, which will be discussed next.

We have divided below article in two separate sections -

  1. mv command-line utility,
  2. rename command-line utility.

Note: We advise you to take a backup before making any changes. Also, certain operations mentioned below would require you to have superuser privileges. In case you don't have one, then contact your System Administrator for assistance.

Rename multiple Files through mv command-line utility

First, we will discuss getting things done through mv command-line utility with relevant examples.

We will utilize for loop in bash & mv command.

I. Change file name -

Input files -

ABC.txt
XYZ.txt
QWE.txt
RTY.txt

So, open the directory where files are saved in terminal and issue the following -

i=1;
for f in *.txt;
do
    echo "$f --> $i.txt";
    mv $f $i.txt;
    i=$(($i+1));
done

All our files will be renamed to -

ABC.txt --> 1.txt
QWE.txt --> 2.txt
RTY.txt --> 3.txt
XYZ.txt --> 4.txt

II. Also, in case we want to make changes to file extensions. Then,

For instance, if you would want to change file extensions from html to php -

Input files -

ABC.html
XYZ.html
QWE.html
RTY.html

So, issue the following in terminal -

i=1;
for f in *.html;
do
    echo "$f --> ${f%.html}.php";
    mv "$f" "${f%.html}.php";
    i=$(($i+1));
done

This changes file extensions - html to php -

ABC.html --> ABC.php
QWE.html --> QWE.php
RTY.html --> RTY.php
XYZ.html --> XYZ.php

Word of caution - although its easy to change file extensions this way. But, this doesn't mean we have changed the format of the related files. For instance, you can't convert a spreadsheet to document merely by changing the file extension.

III. Similarly, if we want to prefix a word for each of the filenames then -

Prefix - file. Input files -

ABC.php
QWE.php
RTY.php
XYZ.php

So, open the terminal and issue the following -

i=1;
for f in *.php;
do
    echo "$f --> file$f";
    mv $f file$f;
    i=$(($i+1));
done

This would get us -

ABC.php --> fileABC.php
QWE.php --> fileQWE.php
RTY.php --> fileRTY.php
XYZ.php --> fileXYZ.php

Rename multiple Files through rename command-line utility

rename command-line utility isn't installed by default in Ubuntu distribution. Therefore, we would have install it first.

Note: Following operations would require you to have superuser privileges. In case you don't have one, then contact your System Administrator for assistance.

Then, open a terminal and issue the following -

sudo apt update

This is being done to update Ubuntu repositories to make the latest version of the package available. Then,

sudo apt install rename

It installs the relevant package and related dependencies (if any).

Now, lets say we have a file 1.html and we would want to rename it to ABC.html. Then,

rename 's/1/ABC/' 1.html

Similarly, to rename 1.html to ABC.php -

rename 's/1\.html/ABC\.php/' 1.html

To convert uppercase to lower or vice versa -

rename 'y/A-Z/a-z/' ABC.php

rename 'y/a-z/A-Z/' abc.php

But, when it comes to multiple files -

ABC.html
XYZ.html
IOP.html
TYU.html

I. To change file extension from .html to .php -

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

where,

$ - only make changes to a file which ends with .html extension.

II. To convert uppercase to lowercase or vice versa -

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

or,

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

III. To rename file names

ABC.html
XYZ.html
IOP.html
TYU.html

Issue the following -

i=1;
for f in *.html;
do
    echo "$f --> $i.html";
    rename "s/$f/$i.html/g" $f
    i=$(($i+1));
done

This would result in -

ABC.html --> 1.html
XYZ.html --> 2.html
IOP.html --> 3.html
TYU.html --> 4.html

IV. Add a prefix to multiple file names -

Input files -

ABC.html
XYZ.html
IOP.html
TYU.html

To add a prefix - file

i=1;
for f in *.html;
do
    echo "$f --> file$f";
    rename "s/$f/file$f/g" $f
    i=$(($i+1));
done

Output -

ABC.html --> fileABC.html
XYZ.html --> fileXYZ.html
IOP.html --> fileIOP.html
TYU.html --> fileTYU.html

In conclusion, we have discussed how to rename multiple files in Ubuntu through mv and rename command-line utility.