Main
Terminal Logo

greprename – perl-style batch file renaming

Script for: command line (shell)
Copyright: Matthias Steffens, use at your own risk!
Description: This command line script allows to batch rename files & directories by use of perl-style regular expressions.
Features:
  • preview new names of files & directories before renaming them
  • simulation mode: preview generated names & see which files/directories would be affected, but don't rename anything
  • apply case transformations
  • re-arrange name parts
  • string formatting
  • auto-numbering: add incrementing numbers
  • perform math operations on existing numbers
  • writes all performed actions to a log file which may be used to revert any erroneous renaming action
See the examples below to get an idea of what's possible.
Necessary: Shell with Perl execution capabilities
History: v1.0  -  initial version released on this site
Download: greprename.zip
Installation: After unpacking the zip archive, put the resulting file within an executable path and make sure that the file itself is executable.
Usage: For help with the syntax type greprename -h. This will print the usage info:

greprename [-hvefFdDs] 's/PATTERN/REPLACEMENT/[egi]' FILE ...

Options:       -h, --help       - display this help text
               -v, --version    - display version information
               -e, --examples   - display usage examples
               -f               - rename files
               -F               - do NOT rename files
               -d               - rename directories
               -D               - do NOT rename directories
               -s               - display preview only (simulation mode)

Notes:   Ommitting any of the file renaming options will cause -f and/or
         -d to be assumed as default.

         The -f and -F options (or the -d and -D options, respectively)
         override each other and the command's actions are determined by
         the last one specified.
Examples: To view some usage examples type greprename -e:

--------------------------------------------------------------------------------
GREPRENAME USAGE EXAMPLES:
--------------------------------------------------------------------------------

Case 1:    To change the file extension of every file from .jpg to .jpeg, type:

greprename 's/\.jpg$/\.jpeg/' *

Examples:  file_one.jpg   ==> file_one.jpeg
           file_two.jpg   ==> file_two.jpeg
           file_three.jpg ==> file_three.jpeg

--------------------------------------------------------------------------------

Case 2:    To apply case transformations to every file, e.g. form words (= make
           its first char uppercase and the following chars lowercase), type:

greprename 's/([[:alpha:]])([[:alpha:]]+)/\u$1\L$2/g' *

Note the 'g'-modifier (= global) at the end which causes every occurrence to
get processed. Otherwise, only the first match would be transformed!

Examples:  file_one   ==> File_One
           file_two   ==> File_Two
           file_three ==> File_Three

--------------------------------------------------------------------------------

Case 3:    To re-arrange JPEG files with date info (e.g., from "dd.mm.yyyy" to
           "yyyy-mm-dd") so that they sort correctly in the finder, type:

greprename 's/(\d{2})\.(\d{2})\.(\d{4})/$3-$2-$1/' *.jpg

The shell wildcard expression '*.jpg' will filter the current directory for
files with a .jpg file extension causing only JPEG files to be processed.

Examples:  13.01.2002.jpg ==> 2002-01-13.jpg
           19.11.2003.jpg ==> 2003-11-19.jpg
           31.12.2003.jpg ==> 2003-12-31.jpg

--------------------------------------------------------------------------------
ADVANCED EXAMPLES (that make use of perls 'e'-modifier which
allows for code execution within the replacement pattern):
--------------------------------------------------------------------------------

Case 4:    To append a dash & an incrementing number to the end of
           the file (starting at 10 and incrementing by 1), e.g. type:

greprename 's/.+/"$&-".($ct+9)/e' *

Note the use of the variable $ct. This counter variable gets defined within the
script. Starting at 1, its value will increment by 1 with each rename action.

Examples:  file_one   ==> file_one-10
           file_two   ==> file_two-11
           file_three ==> file_three-12

--------------------------------------------------------------------------------

Case 5:    To prefix each file name with an incrementing number (starting at
           80, incrementing by 10 and padded with leading zeros), e.g. type:

greprename 's/^/sprintf("%03d_",($ct*10+70))/e' *

Examples:  file_one   ==> 080_file_one
           file_two   ==> 090_file_two
           file_three ==> 100_file_three

--------------------------------------------------------------------------------

Case 6:    To take any existing number(s) from a file name & multiply
           each of the numbers found by the factor 2, type:

greprename 's/\d+/($&*2)/eg' *

Examples:  2-file-1 ==> 4-file-2
           file-20  ==> file-40
           400-file ==> 800-file

--------------------------------------------------------------------------------

Case 7:    To take (the first) existing number from a file name & reformat
           it with padding spaces (upto a depth of 4 chars), e.g. type:

greprename 's/(\D+)(\d+)/sprintf("$1%4d",$2)/e' *

Examples:  file-1   ==> file-   1
           file-20  ==> file-  20
           file-400 ==> file- 400

--------------------------------------------------------------------------------

 


Contact: Matthias Steffens  |  Main  |  Last Updated: 19-Apr-06