Renaming files using Perl command rename

I have been using this command for a while now and it helped very much.
The command rename is available in perl package. This is a core package brought by the Debian installer.
To check the availability of the command :

fool@localhost:~$which rename
/usr/bin/rename

Having a closer look at the command, you will notice that it is a symbolic link to /etc/alternatives/rename :

fool@localhost:~$ls -l /usr/bin/rename
lrwxrwxrwx 1 root root 24 août  23  2008 /usr/bin/rename -> /etc/alternatives/rename

To display the current configuration :

root@localhost:~#update-alternatives –display rename
rename – auto mode
 link currently points to /usr/bin/prename
/usr/bin/prename – priority 60
 slave rename.1.gz: /usr/share/man/man1/prename.1.gz
Current `best’ version is /usr/bin/prename.

How to use this command?
Quite simple if you know how to use regular expressions.
Thanks to the option -n, you can test your regular expression without renaming your files effectively.
Let us have an example :

fool@localhost:~$touch fool_1.t fool_2.t

If you want to rename fool_1.t and fool_2.t into fool1.t and fool2.t : 
fool@localhost:~$rename -n ‘s/_//g’ *.t
fool_1.t renamed as fool1.t
fool_2.t renamed as fool2.t

To rename the files permanently, remove the option ‘-n‘ :
fool@localhost:~$rename ‘s/_//g’ *.t

More information :

man rename

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

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