Skip to main content
corrected loop
Source Link
Olivier Dulac
  • 6.6k
  • 1
  • 25
  • 42

another solution in case you do not have "rename:

for file in *.jpg; do
   [ -f "$file" ] || continue  #skips if no jpg file present in current dir
   echo "${file}" | awk -F'[-.]' '
      {   new=sprintf("%03d",$2); 
          print "echo TESTING mv ", $0," ",$1"-"new"."$3 
      }'
done | bash
done

(edit: moved the | bash so it's only invoked once, and not once per file. And this quick loop is not "weird filename" friendly, but could be sanitized further (to handle files with spaces, etc))

and remove the 2 words echo TESTING to really do the mv ..., once you are sure it's doing what you want it to do.

another solution in case you do not have "rename:

for file in *.jpg; do
   [ -f "$file" ] || continue  #skips if no jpg file present in current dir
   echo "${file}" | awk -F'[-.]' '
      {   new=sprintf("%03d",$2); 
          print "echo TESTING mv ", $0," ",$1"-"new"."$3 
      }' | bash
done

and remove the 2 words echo TESTING to really do the mv ..., once you are sure it's doing what you want it to do.

another solution in case you do not have "rename:

for file in *.jpg; do
   [ -f "$file" ] || continue  #skips if no jpg file present in current dir
   echo "${file}" | awk -F'[-.]' '
      {   new=sprintf("%03d",$2); 
          print "echo TESTING mv ", $0," ",$1"-"new"."$3 
      }'
done | bash

(edit: moved the | bash so it's only invoked once, and not once per file. And this quick loop is not "weird filename" friendly, but could be sanitized further (to handle files with spaces, etc))

and remove the 2 words echo TESTING to really do the mv ..., once you are sure it's doing what you want it to do.

Source Link
Olivier Dulac
  • 6.6k
  • 1
  • 25
  • 42

another solution in case you do not have "rename:

for file in *.jpg; do
   [ -f "$file" ] || continue  #skips if no jpg file present in current dir
   echo "${file}" | awk -F'[-.]' '
      {   new=sprintf("%03d",$2); 
          print "echo TESTING mv ", $0," ",$1"-"new"."$3 
      }' | bash
done

and remove the 2 words echo TESTING to really do the mv ..., once you are sure it's doing what you want it to do.