Renaming a single file is very simple task for Linux user. But renaming multiple files in group may not be a straight forward task. In this article, let us review 3 different methods to rename multiple files together.
Method 1. Use Rename Linux Command
Using rename command you can rename group of files. The syntax for renaming multiple files in group using rename command is given below.
Syntax: rename perlexpr [ files ]
To rename all the html files from html to htm extension in the current directory do the following.
rename s/.html/.htm/ *.html
Using the same concept, you an also rename all the *.txt files to *.bak files (or) all the *.bak files to *.txt files etc.,
Method 2. Use Linux Shell Script to Rename Multiple Files Together
You can write your own shell script to rename the files of specified pattern recursively as:
for filename in *.sql do echo $filename; w_o_ext=`basename $filename .sql`; echo $w_o_ext; mv $filename $w_o_ext.psql; done
Method 3. Use Midnight Commander to Rename Multiple Files
A while back we reviewed about the powerful Linux midnight commander (mc) file explorer. Using midnight command you can rename multiple files using regular expression as explained below.
- Select the required files using regular expression. Press + which will ask the regex to select files. For example, giving *.psql will select all the files with psql extension.
- Rename all the selected files using regex. Press F6 which will ask for the source and destination regex, doing so will change the file names. For this example, give *.psql in source and *.sql in destination which will rename all *.psql files to *.sql files.
Get the Linux Sysadmin Course Now!