//View Tip #231
Similar Tips
» Checksum directory recursively
» Deleting difficult filenames
» Find files by modified time
» Random mass-rename
» Moved Trac and Subversion repositories to another machine/direct

 

Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow
Rename a lot of files at once:
find . | perl -ne'chomp; next unless -e; $oldname = $_; s/aaa/bbb/; next if -e; rename $oldname, $_'

Change 'aaa' and 'bbb' to what you want to find and replace in the filename


View Comments »




Comments 

Add your comment

Comments are currently disabled
glennj
Don't necessarily need Perl for this.

Suppose you want to rename .JPEG files to .jpg -- I'd do something like this (in bash):

<pre>
find . -name \*.JPEG | while IFS= read -r f; do
  echo mv "$f" "${f%.JPEG}.jpg"
done
</pre>

Remove the "echo" when you're ready to actually perform the commands.
Posted 2008-11-13 17:47:57
zundog
@glennj:

or pipe it to bash !

find | while read f; do echo mv "$f" "${f//aaa/bbb}"; done | bash
Posted 2009-06-02 22:41:11

Home Latest Browse Top 25 Random Hall Of Fame Contact Submit