I'm writing a shell script in which I need to loop over directories and then loop over files inside them. So I've written this function:
loopdirfiles() {
#loop over dirs
for dir in "${PATH}"/*
do
for file in "${dir}"/*
do
echo $file
done
done
}
The problem is that it echoes something like *path/to/dir/** on empty directories.
Is there a way to use this approach and ignore those kind of directories?
You can turn on the nullglob
option. It causes unmatching globs to expand to an empty list instead of being left unexpanded.
shopt -s nullglob
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With