tags:

views:

42

answers:

1

I'm looking for a simple script that renames 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).

+1  A: 

Try to use a loop and let, and printf for the padding:

a=1
for i in *.jpg; do
  new=$(printf "%04d.jpg" ${a}) #04 pad to length of 4
  mv ${i} ${new}
  let a=a+1
done
gauteh
You can also do `printf -v new "%04d.jpg" ${a}` to put the value into the variable. And `((a++))` works to increment the variable. Also, this doesn't do it in creation date order or minimize the padding which are things the OP specified. However, it should be noted that Linux/Unix don't store a creation date.
Dennis Williamson