OSETC TECH

How To Rename Multiple Files or Directories at Once in Linux

This post will guide you how to rename multiple files or directories using rename command under your Linux operating system. How do I rename multiple directoreis using a bash shell script in your Linux system.

Rename Command


Rename command will rename the specified files by replacing the first occurrence of expression in their name by replacement.

The syntax of the Rename command is as follows:

rename [options] expression replacement file...

Options:

-s, --symlink Do not rename a symlink but its target.

-v, --verbose Show which files where renamed, if any.

-n, --no-act Do not make any changes.

-o, --no-overwrite Do not overwrite existing files.

-V, --version Display version information and exit.

-h, --help Display help text and exit.

Rename a Single File Using mv command


If you want to rename a single file on your Linux system, and you can use mv command at the shell prompt, type:

$ sudo mv <sourceName> <DestName>

$ sudo mv mytest1 mytest2

Rename multiple Files Using Rename Command


If you want to rename multiple files at once, you can use another command called rename. And if you are using Ubuntu or Debian Linux, and you may have to install it by running the following command:

$ sudo apt install rename

Outputs:

devops@devops-ubuntu:~$ sudo apt install rename
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
rename
0 upgraded, 1 newly installed, 0 to remove and 55 not upgraded.
Need to get 12.3 kB of archives.
After this operation, 37.9 kB of additional disk space will be used.
Get:1 http://mirrors.aliyun.com/ubuntu bionic/universe amd64 rename all 0.20-7 [12.3 kB]
Fetched 12.3 kB in 0s (40.0 kB/s)
Selecting previously unselected package rename.
(Reading database ... 186876 files and directories currently installed.)
Preparing to unpack .../archives/rename_0.20-7_all.deb ...
Unpacking rename (0.20-7) ...
Setting up rename (0.20-7) ...
update-alternatives: using /usr/bin/file-rename to provide /usr/bin/rename (rename) in auto mode
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...

For example, you want to rename all “.txt” files to “.txt.bak” at once. you can use the following rename command, type:

$ sudo rename 's/\.txt$/\.txt.bak/' *.txt

Outputs:

devops@devops-ubuntu:~$ sudo touch test{1..5}.txt
[sudo] password for devops:

devops@devops-ubuntu:~$ ls test*.txt
test1.txt test2.txt test3.txt test4.txt test5.txt
devops@devops-ubuntu:~$ sudo rename 's/\.txt$/\.txt.bak/' *.txt
devops@devops-ubuntu:~$

devops@devops-ubuntu:~$ ls *.txt.bak
test1.txt.bak test2.txt.bak test3.txt.bak test4.txt.bak test5.txt.bak

Check Changes before Renaming Files Using Rename Command


If you do not make any changes when using rename command to rename mutliple files or directoreis, and you can pass the -n option to the rename command, type:

$ sudo rename -n 's/\.txt.bak$/\.txt/' *.txt.bak

Outputs:

devops@devops-ubuntu:~$ sudo rename -n 's/\.txt.bak$/\.txt/' *.txt.bak
rename(eula.txt.bak, eula.txt)
rename(test1.txt.bak, test1.txt)
rename(test2.txt.bak, test2.txt)
rename(test3.txt.bak, test3.txt)
rename(test4.txt.bak, test4.txt)
rename(test5.txt.bak, test5.txt)

Rename Multiple Files or Directories Using Bash shell Script


you can also write an shell bash script to rename multiple files or directoreis like as below:

renamefiles.sh:

#!/bin/bash

for i in *.txt.bak
do
mv -v "$i" "${i/txt.bak/txt}"
done

Let’s execute this bash shell script with the following command:

$ ./renamefiles.sh

Outputs:

devops@devops-ubuntu:~$ ./renamefiles.sh
renamed 'eula.txt.bak' -> 'eula.txt'
renamed 'test1.txt.bak' -> 'test1.txt'
renamed 'test2.txt.bak' -> 'test2.txt'
renamed 'test3.txt.bak' -> 'test3.txt'
renamed 'test4.txt.bak' -> 'test4.txt'
renamed 'test5.txt.bak' -> 'test5.txt'

If you want to get more help about rename command, you can type the following commands:

$ man renmae

or

$ rename --help

Conclusion


You should know that how to rename mutliple files or directoreis using rename command or a bash shell script in your Linux system.