I want to rename the files in a directory to sequential numbers. Based on creation date of the files.
For Example sadf.jpg
to 0001.jpg
, wrjr3.jpg
to 0002.jpg
and so on, the number of leading zeroes depending on the total amount of files (no need for extra zeroes if not needed).
On OSX, install the rename script from Homebrew:
Then you can do it really ridiculously easily:
Or to add a nice prefix:
This script will sort the files by creation date on Mac OS bash. I use it to mass rename videos. Just change the extension and the first part of the name.
To work in all situations, put a \" for files that have space in the name
Again using Pero's solution with little modifying, because
find
will be traversing the directory tree in the order items are stored within the directory entries. This will (mostly) be consistent from run to run, on the same machine and will essentially be "file/directory creation order" if there have been no deletes.However, in some case you need to get some logical order, say, by name, which is used in this example.
I had a similar issue and wrote a shell script for that reason. I've decided to post it regardless that many good answers were already posted because I think it can be helpful for someone. Feel free to improve it!
numerate
@Gnutt The behavior you want can be achieved by typing the following:
If the option
-r
is left out the reaming will be only simulated (Should be helpful for testing).The otion L describes the length of the target number (which will be filled with leading zeros) it is also possible to add a prefix/suffix with the options
-p <prefix>
-s <suffix>
.In case somebody wants the files to be sorted numerically before they get numbered, just remove the
-o modtime
option.rename -vn - remove n for off test mode
{$i=1;} - control start number
"%04d.jpg" - control count zero 04 and set output extension .jpg