by  Marirs

One liner to rename files extensions

We often use mv command to rename a single file in Linux. However, renaming multiple or group of files quickly makes it very difficult task in a terminal.

Linux comes with a very powerful built-in tool called rename. The rename command is used to rename multiple or group of files, rename files to lowercase, rename files to uppercase and overwrite files using perl expressions.

This command is part of Perl script and it resides under /usr/bin/ on many Linux distributions. You can run which command to find out the location of rename command.

[[email protected]]$ which rename
/usr/bin/rename

Usage:


[[email protected]]$ rename "s/txt/text/" *.txt

The above command will rename all files with extension "txt" to new extension "text" in all files matching "*.txt"

With OSx and Homebrew installed, you can install the rename command using the following:

[[email protected]]$ brew install rename
==> Downloading https://homebrew.bintray.com/bottles/rename-1.600.el_capitan.bottle.tar.gz
……………………………………………………………… 100.0%
==> Pouring rename-1.600.el_capitan.bottle.tar.gz
? /usr/local/Cellar/rename/1.600: 3 files, 40.9K                              
                            

Then issue the following command, to rename all files with extension "txt" to new extension "text" in all files matching "*.txt"

[[email protected]]$ rename "s/txt/text/" *.txt