24

I'm trying to write a for loop in bash to get the files with extension of jpg, jpeg, png, this i my attempt, but does not work

for file in "${arg}"/*.{jpg,jpeg,png}; do echo ${arg}-something.jpg > z.txt ; done;

basically, i want to get the name of the file with those extension in the current folder, and do something with each file, then output the filename back with a new extension.

1

2 Answers 2

38

I would like to suggest 2 improvements to the proposed solution:

A. The for file in "$arg"/.{jpg,jpeg,png} will also produce "$arg"/.jpeg if there are no files with jpeg extention and that creates errors with scripts:

$ echo *.{jpg,jpeg,png}
myPhoto.jpg *.jpeg *.png

To avoid that, just before the for loop, set the nullglob to remove null globs from from the list:

$ shopt -s nullglob # Sets nullglob
$ echo *.{jpg,jpeg,png}
myPhoto.jpg
$ shopt -u nullglob # Unsets nullglob

B. If you also want to search *.png or *.PNG or *.PnG (i.e. ignore case), then you need to set the nocaseglob:

$ shopt -s nullglob # Sets nullglob
$ shopt -s nocaseglob # Sets nocaseglob
$ echo *.{jpg,jpeg,png}
myPhoto.jpg myPhoto.PnG
$ shopt -u nocaseglob # Unsets nocaseglob
$ shopt -u nullglob # Unsets nullglob
0
35

You are not using $file anywhere. Try

for file in "$arg"/*.{jpg,jpeg,png} ; do
    echo "$file" > z.txt
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.