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).
If your
rename
doesn't support-N
, you can do something like this:Edit To start with a given number, you can use the (somewhat ugly-looking) code below, just replace 123 with the number you want:
This lists files in order by creation time (newest first, add
-r
to ls to reverse sort), then sends this list of files to rename. Rename uses perl code in the regex to format and increment counter.However, if you're dealing with JPEG images with EXIF information, I'd recommend
exiftool
This is from the exiftool documentation, under "Renaming Examples"
You can also use ls
Let us assume we have these files in a directory, listed in order of creation, the first being the oldest:
then
ls -1cr
outputs exactly the list above. You can then userename
:which outputs
The warning ”use of uninitialized value […]” is displayed for files without an extension; you can ignore it.
Remove
-n
from therename
command to actually apply the renaming.This answer is inspired by Luke’s answer of April 2014. It ignores Gnutt’s requirement of setting the number of leading zeroes depending on the total amount of files.
To me this combination of answers worked perfectly:
ls -v
helps with ordering 1 10 9 in correct: 1 9 10 order, avoiding filename extension problems with jpg JPG jpeggawk 'BEGIN{ a=1 }{ printf "mv %s %04d.jpg\n", $0, a++ }'
renumbers with 4 characters and leading zeros. By avoiding mv I do not accidentally try to overwrite anything that is there already by accidentally having the same number.bash
executesBe aware of what @xhienne said, piping unknown content to bash is a security risk. But this was not the case for me as I was using my scanned photos.
Follow command rename all files to sequence and also lowercase extension:
To renumber 6000, files in one folder you could use the 'Rename' option of the ACDsee program.
For defining a prefix use this format:
####"*"
Then set the start number and press Rename and the program will rename all 6000 files with sequential numbers.