- Professional Skills
- Articles
- Bookmarklets
- Javascript Console For IE
- Neural Net Extension for Ruby
- Writing a PDF Generation Framework In Ruby
- Developing Mambo Components
- PayPal Website Payments Pro
- Bulk File Renaming
- Displaying a Maintenance Page
- Ruby: escape, unescape
- Clojure Tutorial For the Non-Lisp Programmer
- Complex mocking with PHPUnit
- PHP Coding Tips
- Simple CRUD Application
- External Links
- Recent posts
Bulk Rename files with Bash
Submitted by moxley on Wed, 2006-09-06 18:51
Explore how to bulk-rename files quickly with Bash
Update: Mike Wilcox provided this much more simple way of bulk-renaming files:
ls *old|sed 's/\(.*\)\.old/mv \1.old \1.new/'|sh
Now that's compact!
Here's the original article:
Let's say you have 100 files with a ".php' extention, and you want to rename them so they have a ".html" extension. Here's how you do it:
- Create a file (e.g.,
rename.sh) with this code:#!/bin/bash oldext=php newext=html for oldfile in `ls *.$oldext`; do [[ "$oldfile" =~ "(.*)\.$oldext" ]] newfile="${BASH_REMATCH[1]}.$newext" mv $oldfile $newfile done - Give it execute permissions:
chmod 755 rename.sh - Run it:
./rename.sh
Variations
- Recursive Renaming To rename files recursively through sub-directories, use the
findcommand instead ofls - Specify old and new extensions from command line Assign
oldextto$0andnewtextto$1.


Cape Cod Cottage for Rent