Rename multiple files with rename command in Ubuntu

Through Graphical User Interface (GUI), we can easily rename a file. Just right-click on the file and Rename. It opens a dialog box to enter a new name. From here even the extension of the file can also be modified. But, how changing the extension impacts the file is something we are not covering here.

Generally, we use a loop with mv command-line utility to rename multiple files. In this article, we cover how to rename multiple files with rename command-line utility in Ubuntu. But, before that we need to install relevant packages.

Note: Following operations require superuser privileges. In case you don’t have one then, we advise you to contact your System Administrator for assistance. Besides, have a backup before you make any changes.

Install rename in Ubuntu

The package is available through standard Ubuntu repository. Therefore, update the repository first. Hence, open a terminal and issue the following –

sudo apt update

Next, to install rename package

sudo apt install rename

Rename multiple files in Ubuntu

This is important, before making changes we can also check how things will pan out for us. This won’t make any changes to the files itself but, it would just show us what will be the outcome. To get that, use -n option,

rename -n 's/\.odt$/\.txt/' *.odt

The above command would show us how things would look like but won’t make any material changes to the .odt files.

Besides, we also have -v option (Verbose) to view the modifications made –

rename -v 's/\.odt$/\.txt/' *.odt

Case I. We start with the easiest first, rename the extension of files. So, let’s say we have multiple document files which end with extension .odt. To modify the extension to text file i.e. .txt, issue the following –

rename -v 's/\.odt$/\.txt/' *.odt

Case II. If we want our files to not have an extension then,

rename -v 's/\.txt$//' *.txt

Case III. To convert upper case letters to lower case and vice versa in filenames-

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

or,

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

As we have used a *, this would make changes to the all the files. To make changes to only text files, use .txt in place of *

Case IV. Rename part of filenames with another string. For instance, we have two text files – techpiezo-1.txt and techpiezo-2.txt and we would like these two as tp-1.txt and tp-2.txt. Then,

rename -v 's/techpiezo/tp/' *.txt

Case V. In Case IV, we got files as tp-1.txt and tp-2.txt. What if we want to have underscore(_) in place of hyphen(-).

rename -v 's/-/_/' *.txt

In conclusion, we have covered how to rename multiple files in Ubuntu through few basic examples. It is not possible for us to cover each and every combination possible. But, at least you get an idea on how to move forward.

Similar Posts

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.

Similar Posts