Deafsilver
Deafsilver

Reputation: 47

Find number of files with certain extension in directory

I am trying to create a script that will rename all the files in a directory with an extension of .jpg. I figure that I would have to find the total number of files with the .jpg extension and iterate through all the files in the directory using a for loop. My question is, how would I go about getting the total number of files with the .jpg extension within a directory?

Thank you!

Upvotes: 1

Views: 954

Answers (1)

that other guy
that other guy

Reputation: 123570

There's no reason to count the number of files in advance.

#!/bin/bash
i=1
for file in *.jpg
do
    mv -i "$file" "$i.jpg"
    (( i++ ))
done

Upvotes: 2

Related Questions