I would like to rename all files in a folder so to have consecutive numbers. For instance:
1.png
2.png
3.png
etc
I know there is the rename command and I know there are DOZENS of similar questions in here but I can't find the way.
NOTE: Suggested duplicate doesn't contain a solution specific for my case. Please stop flagging this as duplicate, because suggested duplicate does not answer my question
Assuming you want to follow the shell globbing order while sorting files, you can do:
Here looping over all the files in the current directory and renaming sequentially based on order, if you want to deal with only the
.png
files, usefor file in *.png
instead.counter
variable will keep track of the increments.This is a dry-run, remove
echo
to let the actual renaming action take place.Example:
Here's a small python script that can do what you ask
Basic usage:
It will print output to
stdout
before renaming each fileThis version pushes index until it is found that filename with such index is not taken. Although filenames may start at different index upon successive iterations of the script, the files themselves remain unchanged.
Alternative version, solves issue with duplicate filenames by appending timestamp to each filename, and then enumerating them. This solution may take longer time, as number of files increases, but for directories that range in hundreds , this won't take long time