Linux Rename Regular Expressions

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Linux rename Regular Expressions

The rename command is mostly used to search a string and replace it with another string


  1. rename 's/string/other string/'  


rename 's/text/txt/' * 

Linux Rename Regular Expression1

Look at the above snapshot, all the 'text' are converted into 'txt'.

You can also replace a string with the following syntax.


  1. rename 's/string/other string/' * string  


  1. rename 's/txt/TXT/' *.txt  

Linux Rename Regular Expression2

Look at the above snapshot, all '.txt' are converted into '.TXT'.

In above two examples the strings used were present only at the end of the file name. But this example is different.


rename 's/txt/bbb/' atxt.txt 

Linux Rename Regular Expression3

Look at the above snapshot, only the first occurence of sarched string is replaced.

A Global Replacement

In the above example only first 'txt' was replaced in 'atxt.txt'. To replace both the 'txt' we can use a global replacement 'g'.


  1. rename 's/string/other string/g'  


  1. rename 's/txt/TXT/g' atxt.txt  

Linux Rename Regular Expression4

Look at the above snapshot, both the 'txt' are replaced with 'TXT'.

Case Insensitive Replacement

In case insensitive replacement, a string can be replaced with a case insensitive string.


  1. rename 's/string/other string/i'  


rename 's/.text/.txt/i' * 

Linux Rename Regular Expression5

Look at the above snapshot, all '.text' are replaced with '.txt'.