Pages

Wednesday, June 28, 2017

Rename – A Command Line Tool For Renaming Multiple Files in Linux

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.

Rename Multiple Files In Linux
The “rename” command is a 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.
$ which rename
/usr/bin/rename
The Basic Syntax of Rename Command
rename 's/old-name/new-name/' files
The rename command comes with few optional arguments along with mandatory perl expression that guides rename command to do actual work.
rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]
  1. -v: Print names of files successfully renamed.
  2. -n: Show what files would have been renamed.
  3. -f: Force overwrite existing files.
  4. perlexpr: Perl Expression.
For better understanding of this utility, we’ve discussed few practical examples of this command in the article.

1. A Basic Rename Command Example

Suppose you’ve bunch of files with “.html” extension and you want to rename all “.html” files to “.php” at one go. For example, first do a “ls -l” to check the list of files with “.html” extension.
# ravisaive@tecmint:~$ ls -l
total 22532
-rw-rw-r-- 1 ravisaive ravisaive 6888896 Oct 10 12:10 cricket.html
-rw-rw-r-- 1 ravisaive ravisaive  588895 Oct 10 12:10 entertainment.html
-rw-rw-r-- 1 ravisaive ravisaive 6188895 Oct 10 12:10 health.html
-rw-rw-r-- 1 ravisaive ravisaive 6538895 Oct 10 12:10 lifestyle.html
-rw-rw-r-- 1 ravisaive ravisaive  938895 Oct 10 12:10 news.html
-rw-rw-r-- 1 ravisaive ravisaive  938937 Oct 10 12:11 photos.html
-rw-rw-r-- 1 ravisaive ravisaive  978137 Oct 10 12:11 sports.html
Now, you want to change the extension of all these files from “.html” to “.php“. You can use the following “rename” command with perl expression as shown below.

      ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
                                                    ► Read more: http://adf.ly/1nBRn0
      ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

No comments:

Post a Comment