Brad Fitzpatrick (brad) wrote,
Brad Fitzpatrick
brad

numeric file rename

What started as me accidentally forgetting an sprintf turned into a fixer utility. If this doesn't already exist, somebody out there might find it useful...

http://bradfitz.com/programming/hacks/nrename
$ ~/bin/nrename
nrename [opts] <directory>

OPTIONS:
     --dry-run -n      Dry-run, just show what it would've done
     --verbose -v      Shows renames as they happen

DESCRIPTION:

Renames all necessary files in a directory with respect to numbers in
their filenames such that when they sort alphabetically later, their
alphabetic order is the same as their numeric order.  Essentially it
just zero-pads numbers within filenames where needed, but it can deal
with any number of digits anywhere in the filename.  For instance, these
files:

   20_foo_5.dat
   20_foo_20.dat
   5_foo_6.dat

Would normally sort:

   20_foo_20.dat
   20_foo_5.dat
   5_foo_6.dat

Which is entirely wrong.  What nrename will do is:

   05_foo_06.dat
   20_foo_05.dat
   20_foo_20.dat

Which is what you presumably wanted.

AUTHOR:
 Brad Fitzpatrick <brad@fitzpat.com>
Subscribe
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 11 comments