How to "mass" renaming files

Joseph Sinclair plug-discuss at stcaz.net
Fri Aug 19 12:31:36 MST 2005


Below is a script I use to rename image files.  It does a lot, including renumbering, but it should be easy enough to modify for your needs.

--------------------------------------------------------------------------------
#!/bin/bash

function print_help
{
   echo Syntax is "$0 <source folder> <new file prefix> [<start number>]"
}

function rename_one_file
{
   local file_suffix=${1##*.}
   local file_suffix=${file_suffix/%AVI/avi}
   local file_suffix=${file_suffix/%MPG/mpeg}
   local file_suffix=${file_suffix/%MPEG/mpeg}
   local file_suffix=${file_suffix/%JPG/jpg}
   local file_suffix=${file_suffix/%MOV/mov}
   local file_suffix=${file_suffix/%JPEG/jpeg}
   local file_suffix=${file_suffix/%PNG/png}
   local file_suffix=${file_suffix/%GIF/gif}
   local file_suffix=${file_suffix/%jpg/jpeg}
   local file_suffix=${file_suffix/%mpg/mpeg}
   mv "${1}" "${2}${3}.${file_suffix}"
   echo ${1} renamed to ${2}${3}.${file_suffix};
}

function fixup_file
{
   local source_file="$1"
   local file_prefix="$2"
   fixup_temp_sequence=$((${fixup_temp_sequence} + 1))
   if [[ ${fixup_temp_sequence} -lt 10 ]]; then local file_prefix="${file_prefix}0"; fi
   if [[ ${fixup_temp_sequence} -lt 100 ]]; then local file_prefix="${file_prefix}0"; fi

   rename_one_file "${source_file}" "${file_prefix}" ${fixup_temp_sequence};
}

function rename_images
{
   pushd "$1" >/dev/null

   for CurrentFile in *.jpeg; do fixup_file "${CurrentFile}" "${2}-"; done
   for CurrentFile in *.jpg; do fixup_file "${CurrentFile}" "${2}-"; done
   for CurrentFile in *.png; do fixup_file "${CurrentFile}" "${2}-"; done
   for CurrentFile in *.gif; do fixup_file "${CurrentFile}" "${2}-"; done
   for CurrentFile in *.JPEG; do fixup_file "${CurrentFile}" "${2}-"; done
   for CurrentFile in *.JPG; do fixup_file "${CurrentFile}" "${2}-"; done
   for CurrentFile in *.PNG; do fixup_file "${CurrentFile}" "${2}-"; done
   for CurrentFile in *.GIF; do fixup_file "${CurrentFile}" "${2}-"; done
   find . -type f -exec chmod ugo-x {} \;
   find . -type f -exec chmod ugo+r {} \;

   popd >/dev/null
}

function rename_videos
{
   pushd "$1" >/dev/null

   for CurrentFile in *.avi; do fixup_file "${CurrentFile}" "${2}-"; done
   for CurrentFile in *.mpeg; do fixup_file "${CurrentFile}" "${2}-"; done
   for CurrentFile in *.mpg; do fixup_file "${CurrentFile}" "${2}-"; done
   for CurrentFile in *.mov; do fixup_file "${CurrentFile}" "${2}-"; done
   for CurrentFile in *.AVI; do fixup_file "${CurrentFile}" "${2}-"; done
   for CurrentFile in *.MPEG; do fixup_file "${CurrentFile}" "${2}-"; done
   for CurrentFile in *.MPG; do fixup_file "${CurrentFile}" "${2}-"; done
   for CurrentFile in *.MOV; do fixup_file "${CurrentFile}" "${2}-"; done
   find . -type f -exec chmod ugo-x {} \;
   find . -type f -exec chmod ugo+r {} \;

   popd >/dev/null
}

function fixup_folder
{
   shopt -s nullglob
   source_dir="$1"
   local new_prefix="$2"
   if [[ -n $3 ]]; then fixup_temp_sequence=$3; else fixup_temp_sequence=1; fi

   fixup_temp_sequence=$((${fixup_temp_sequence} - 1))

   if [[ -d ${source_dir} ]]; then
     if [[ -n ${new_prefix} ]]; then
       rename_images "${source_dir}" "${new_prefix}";
       if [[ -n $3 ]]; then fixup_temp_sequence=$3; else fixup_temp_sequence=1; fi
       fixup_temp_sequence=$((${fixup_temp_sequence} - 1))
       rename_videos "${source_dir}" "${new_prefix}";
     else
       print_help;
     fi
   else
     print_help;
   fi
}

fixup_folder "$1" "$2" "$3"
--------------------------------------------------------------------------------


Josef Lowder wrote:
> Can anyone provide a simple shell script command to 
> change the file names of all the files in a given directory? 
> 	from pict0001.jpg to just 01.jpg 
> 	from pict0002.jpg to just 02.jpg etc. etc. 
> with the numbers running to a few hundred pix, all with 
> the same "pict00..." first part of the filename. 
> 
> I looked on the 'net to find a shell script to "mass" rename the files 
> in any given directory and found the example below to rename the 
> suffix from php3 to just php, but I couldn't figure out how to modify 
> that to change the filename itself. 
> 
>  ls -d *.php3 | sed 's/\(.*\).php3$/mv "&" "\1.php"/' | sh 
> 
> I also found a *nix utility on my system named 'rename' but I 
> couldn't get it to work.  Below is an excerpt from the 'man' page 
> for 'rename' ... but what in the world is 'foo'?? 
> 
> MAN PAGE excerpt: 
> rename will rename the specified files by replacing 
> the first occurrence of from in their name by to. 
> For example, given the files foo1, ..., foo9, foo10, ..., foo278, the 
> commands
> 
> rename foo foo0 foo? 
>  rename foo foo0 foo?? 
> 
> will turn them into foo001, ..., foo009, foo010, ..., foo278.
> And
> rename .htm .html *.htm		will fix the extension of your html files. 
> 
> ---------------------------------------------------
> PLUG-discuss mailing list - PLUG-discuss at lists.plug.phoenix.az.us
> To subscribe, unsubscribe, or to change  you mail settings:
> http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-discuss
> 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: OpenPGP digital signature
Url : http://lists.plug.phoenix.az.us/pipermail/plug-discuss/attachments/20050819/a7853180/signature.pgp


More information about the PLUG-discuss mailing list