21/09/2019

[bash] Loop over all files in folder filtering by extension

In bash, it is possible to loop over all files with a specific extension in a folder with:

for file in folder/*.extension
do
    something with $file
done

Note that if directory is empty, one iteration will performed on the nonexistent *.extension file. To avoid that set the nullglob shell option for that run only as such:

shopt -s nullglob

your_script_here

shopt -u nullglob

No comments:

Post a Comment

With great power comes great responsibility