Skip to content

Instantly share code, notes, and snippets.

@devraj
Last active September 12, 2023 02:05
Show Gist options
  • Save devraj/682f45462fb7713920c543bf11fd880b to your computer and use it in GitHub Desktop.
Save devraj/682f45462fb7713920c543bf11fd880b to your computer and use it in GitHub Desktop.
Takes the contents a folder and renames them into filenames with sequential file names e.g 001.jpg, 002,jpg
let counter=1;\
for filename in *; do\
new_filename=$(printf "%03d.png" $counter);\
print "$filename -> $new_filename";\
let counter=counter+1;\
mv "${filename}" $new_filename;\
done
@devraj
Copy link
Author

devraj commented Jan 12, 2023

I was working on a site for a friend who's a photographer and provided me with a set of JPG files that were names like

-rw-r--r--@ 1 devraj  staff  4367696 28 Sep 14:54 _DSC0156.JPG
-rw-r--r--@ 1 devraj  staff  4261008 28 Sep 14:54 _DSC0163.JPG
-rw-r--r--@ 1 devraj  staff  3881646 28 Sep 14:54 _DSC0170.JPG
-rw-r--r--@ 1 devraj  staff  5858034 28 Sep 14:54 _DSC0178.JPG

I wanted to rename them into a sequence like 001.jpg, 002,jpg

This script provides the basis of the counter renaming the files with `padding applied to the numbers, resulting in:

_DSC0110.JPG -> 229.jpg
_DSC0114.JPG -> 230.jpg
_DSC0117.JPG -> 231.jpg
_DSC0120.JPG -> 232.jpg
_DSC0153.JPG -> 233.jpg
_DSC0154.JPG -> 234.jpg
_DSC0156.JPG -> 235.jpg
_DSC0163.JPG -> 236.jpg
_DSC0170.JPG -> 237.jpg
_DSC0178.JPG -> 238.jpg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment