Alex Rivera | Logout

Batch rename file extensions, including subdirectories

Asked 2009-06-22T04:37:17.230
13

I'm renaming empty file extensions with this command:

rename *. *.bla

However, I have a folder with hundreds of such subfolders, and this command requires me to manually navigate to each subfolder and run it.

Is there a command that I can run from just one upper level folder that will include all the files in the subfolders?

Edit
Report

1 Answer

0

Using find and xargs and basename would make the expression easier to read

Prepare a rename shell script like this (corrected to handle multiple files)

#!/bin/sh
if [ $# -lt 3 ] 
then
    echo "usage: rename.sh sufold sufnew file1 (file2 file3 ...) "
    exit
fi

sufold=$1
sufnew=$2
shift
shift

for f in $*
do
  echo "to mv " $f `dirname $f`"/"`basename $f $sufold`$sufnew
  mv $f `dirname $f`"/"`basename $f $sufold`$sufnew
done

Then you could invoke that to each file matched your criteria, including recursive folder search by find command

find . -name "*." | xargs rename.sh "." ".bla"

A more common sample

find . -name "*.htm" | xargs rename.sh ".htm" ".html"
answered 2011-01-20T06:55:07.153

Your Answer