Never Ending Security

It starts all here

Rename Commands Cheatsheet

Rename Command

Most of the times you use “mv” command to rename or move a single file. In order to quickly rename multiple files or a group of files in Linux we would need to use a different command.

The rename command is used to rename multiple or group of files, rename files to lower-case/upper-case and overwrite files using perl expressions. 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 

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.

A Basic Rename Command Example

In this example we will use rename command to rename files with “.html” extension to “.php

# cyberpunk@n0where.net:~$ ls -l
total 22532
-rw-rw-r-- 1 cyberpunk cyberpunk 6888896 Oct 10 12:10 web_file1.html
-rw-rw-r-- 1 cyberpunk cyberpunk  588895 Oct 10 12:10 web_file2.html
-rw-rw-r-- 1 cyberpunk cyberpunk 6188895 Oct 10 12:10 web_file3.html
-rw-rw-r-- 1 cyberpunk cyberpunk 6538895 Oct 10 12:10 web_file4.html
-rw-rw-r-- 1 cyberpunk cyberpunk  938895 Oct 10 12:10 web_file5.html
-rw-rw-r-- 1 cyberpunk cyberpunk  938937 Oct 10 12:11 web_file6.html
-rw-rw-r-- 1 cyberpunk cyberpunk  978137 Oct 10 12:11 web_file7.html

rename” command with perl expression

cyberpunk@n0where.net:~$ rename 's/\.html$/\.php/' *.html

Verify…

cyberpunk@n0where.net:~$ ls -l
total 22532
-rw-rw-r-- 1 cyberpunk cyberpunk 6888896 Oct 10 12:10 web_file1.php
-rw-rw-r-- 1 cyberpunk cyberpunk  588895 Oct 10 12:10 web_file2.php
-rw-rw-r-- 1 cyberpunk cyberpunk 6188895 Oct 10 12:10 web_file3.php
-rw-rw-r-- 1 cyberpunk cyberpunk 6538895 Oct 10 12:10 web_file4.php
-rw-rw-r-- 1 cyberpunk cyberpunk  938895 Oct 10 12:10 web_file5.php
-rw-rw-r-- 1 cyberpunk cyberpunk  938937 Oct 10 12:11 web_file6.php
-rw-rw-r-- 1 cyberpunk cyberpunk  978137 Oct 10 12:11 web_file7.php

Check For Changes Before Running Rename

While doing some critical tasks, you may want to check the result of your command without really doing the ‘rename’. To do that you would need to use “-n” switch.

cyberpunk@n0where.net:~$ rename -n 's/\.php$/\.html/' *.php

web_file1.php renamed as web_file1.html
web_file2.php renamed as web_file2.html
web_file3.php renamed as web_file3.html
web_file4.php renamed as web_file4.html
web_file5.php renamed as web_file5.html
web_file6.php renamed as web_file6.html
web_file7.php renamed as web_file7.html

Print Rename Output

By default, using the rename command does not produce any output.  If you want see the progress and output of the command you need to use “-v” switch

cyberpunk@n0where.net:~$ rename -v 's/\.php$/\.html/' *.php

web_file1.php renamed as web_file1.html
web_file2.php renamed as web_file2.html
web_file3.php renamed as web_file3.html
web_file4.php renamed as web_file4.html
web_file5.php renamed as web_file5.html
web_file6.php renamed as web_file6.html
web_file7.php renamed as web_file7.html

Convert all Lower-case to Upper-case and Vise-Versa

LOWER-CASE TO UPPER-CASE

cyberpunk@n0where.net:~$ ls -l
total 22532
-rw-rw-r-- 1 cyberpunk cyberpunk 6888896 Oct 10 12:10 web_file1.html
-rw-rw-r-- 1 cyberpunk cyberpunk  588895 Oct 10 12:10 web_file2.html
-rw-rw-r-- 1 cyberpunk cyberpunk 6188895 Oct 10 12:10 web_file3.html
-rw-rw-r-- 1 cyberpunk cyberpunk 6538895 Oct 10 12:10 web_file4.html
-rw-rw-r-- 1 cyberpunk cyberpunk  938895 Oct 10 12:10 web_file5.html
-rw-rw-r-- 1 cyberpunk cyberpunk  938937 Oct 10 12:11 web_file6.html
-rw-rw-r-- 1 cyberpunk cyberpunk  978137 Oct 10 12:11 web_file7.html

Rename:

cyberpunk@n0where.net:~$ rename 'y/a-z/A-Z/' *.html

Verify…

cyberpunk@n0where.net:~$ ls -l
total 22532
-rw-rw-r-- 1 cyberpunk cyberpunk 6888896 Oct 10 12:10 WEB_FILE1.HTML
-rw-rw-r-- 1 cyberpunk cyberpunk  588895 Oct 10 12:10 WEB_FILE2.HTML
-rw-rw-r-- 1 cyberpunk cyberpunk 6188895 Oct 10 12:10 WEB_FILE3.HTML
-rw-rw-r-- 1 cyberpunk cyberpunk 6538895 Oct 10 12:10 WEB_FILE4.HTML
-rw-rw-r-- 1 cyberpunk cyberpunk  938895 Oct 10 12:10 WEB_FILE5.HTML
-rw-rw-r-- 1 cyberpunk cyberpunk  938937 Oct 10 12:11 WEB_FILE6.HTML
-rw-rw-r-- 1 cyberpunk cyberpunk  978137 Oct 10 12:11 WEB_FILE7.HTML

UPPER TO LOWER CASE

Rename:

cyberpunk@n0where.net:~$ rename 'y/A-Z/a-z/' *.HTML
cyberpunk@n0where.net:~$ ls -l
total 22532
-rw-rw-r-- 1 cyberpunk cyberpunk 6888896 Oct 10 12:10 web_file1.html
-rw-rw-r-- 1 cyberpunk cyberpunk  588895 Oct 10 12:10 web_file2.html
-rw-rw-r-- 1 cyberpunk cyberpunk 6188895 Oct 10 12:10 web_file3.html
-rw-rw-r-- 1 cyberpunk cyberpunk 6538895 Oct 10 12:10 web_file4.html
-rw-rw-r-- 1 cyberpunk cyberpunk  938895 Oct 10 12:10 web_file5.html
-rw-rw-r-- 1 cyberpunk cyberpunk  938937 Oct 10 12:11 web_file6.html
-rw-rw-r-- 1 cyberpunk cyberpunk  978137 Oct 10 12:11 web_file7.html

Overwrite Existing Files

If you want to overwrite existing files, use the “-f” switch.

cyberpunk@n0where.net:~$ rename -f 's/a/b/' *.html

If you would like to know more about rename command, check out “man rename” .

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s