0

I am looking to rename the 137 .jpg files from a directory with numbers from 1 to 137 keep the same order.

The files look like this:

IMG_20190512_164221.jpg  IMG_20190512_171322.jpg  IMG_20190512_180210.jpg
IMG_20190512_164246.jpg  IMG_20190512_171355.jpg  IMG_20190512_180232.jpg
IMG_20190512_164412.jpg  IMG_20190512_171424.jpg  IMG_20190512_180258.jpg
IMG_20190512_164438.jpg  IMG_20190512_171444.jpg  IMG_20190512_180259.jpg
IMG_20190512_164506.jpg  IMG_20190512_171511.jpg  IMG_20190512_180314.jpg
IMG_20190512_164528.jpg  IMG_20190512_171534.jpg  IMG_20190512_180339.jpg
IMG_20190512_164546.jpg  IMG_20190512_171549.jpg  IMG_20190512_180401.jpg
IMG_20190512_164555.jpg  IMG_20190512_171602.jpg  IMG_20190512_180432.jpg
IMG_20190512_164750.jpg  IMG_20190512_171635.jpg  IMG_20190512_180456.jpg
IMG_20190512_164759.jpg  IMG_20190512_171700.jpg  IMG_20190512_180516.jpg

On research I've found answers using the rename and mv commands but they are either looping over a constant part of a string only (i.e. the extension or a common set of chars).

This is the closest I found on another post but it is still not what I am looking for:

cnt=0
for i in *; do
  let cnt=cnt+1
  mv "$i" "$(echo "${i}_${cnt}" | awk -F_ '{print $1"_"$3}')"
done
  • why not mv "$i" IMG_$cnt? Maybe with some formatting to use 3 digits, search for bash parameters expansion... – Rmano May 14 at 6:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.