We Recommend

bash Cookbook: Solutions and Examples for bash Users bash Cookbook: Solutions and Examples for bash Users
bash Cookbook teaches shell scripting the way Unix masters practice the craft. It presents a variety of recipes and tricks for all levels of shell programmers so that anyone can become a proficient user of the most common Unix shell -- the bash shell -- and cygwin or other popular Unix emulation packages.


Posted By

tylerhall on 05/26/07


Tagged

regex file Bash rename


Versions (?)


Who likes this?

2 people have marked this snippet as a favorite

basicmagic
lukaszkorecki


Rename Multiple Files in Bash Using Regular Expressions


Published in: Bash 


This small, bash script will loop though the specified files and rename them according to a sed regular expression. In this example, I'm renaming a bunch of .avi movie files.

  1. for i in *.avi
  2. do
  3. j=`echo $i | sed 's/find/replace/g'`
  4. mv "$i" "$j"
  5. done
  6.  
  7. Can also be written on a single line as
  8.  
  9. for i in *.avi; do j=`echo $i | sed 's/find/replace/g'`; mv "$i" "$j"; done

Report this snippet 

Comments

RSS Icon Subscribe to comments
Posted By: mind on May 26, 2007

prename is much simpler, and it is in the perl standard distributio that is installed in almost all unix systems. to use it: prename 's/find/replace/ files

enjoy

You need to login to post a comment.