My Computer Tips

Home | About | Categories | All Tips & Tutorials

Rename all images inside folder with sequential numbers bash scripts

ID: 317

Category: Bash Scripts

Added: 27th of August 2022

Updated On: Tutorial updated and rechecked on 22nd of November 2023

Views: 1,318

The following bash script renames all .jpg images inside a folder with sequential numbers, for example 1.jpg, 2.jpg, 3.jpg......

If you are using a different image format such as .jpeg, .png, .gif just change the code at the start of the for loop, for example for i in *.png if your folder contains .png images.

If this is your first time creating a bash script please visit my other tutorial Create a simple bash script

Copy and past the code below in to your text editor, save the file as rename_images.sh

#!/usr/bin/bash
number=1;

for i in *.jpg;
do
mv "$i" "$(printf $number).${i#*.}";
((number++));
done


Make the file executable by entering the following command
chmod +x rename_images.sh


Run the script by entering
./rename_images.sh


Make sure the bash script is in the same folder as your images before executing the script.

Related Tips & Tutorials

Create a simple bash script in Ubuntu and Ubuntu based distributions

Getting user input bash scripts