I want to rename multiple jpg files in a directory so they have 9 digit sequence number. I also want the files to be sorted by date from oldest to newest. I came up with this:
ls -tr | nl -v 100000000 | while read n f; do mv "$f" "$n.jpg"; done
ls -tr | cat -n .....
If any of your filename contains a whitespace, you can use the following:
i=100000000 find -type f -printf '%T@ %p\0' | \ sort -zk1nr | \ sed -z 's/^[^ ]* //' | \ xargs -0 -I % echo % | \ while read f; do mv "$f" "$(printf "%09d" $i).jpg" let i++ done
Note that this doesn't use
ls for parsing, but uses the null byte as field separator in the different commands, respectively set as
find command prints the file time together with the name.
Then the file are
sed removes the timestamp.
xargs is giving the filenames to the
mv command through