How does one test for the existence of files in a directory using bash?
if ... ; then
echo 'Found some!'
fi
To be clear, I don’t want to test for the existence of a specific file. I would like to test if a specific directory contains any files.
I went with:
(
shopt -s dotglob nullglob
existing_files=( ./* )
if [[ ${#existing_files[@]} -gt 0 ]] ; then
some_command "${existing_files[@]}"
fi
)
Using the array avoids race conditions from reading the file list twice.
I typically just use a cheap ls -A to see if there’s a response.
Pseudo-maybe-correct-syntax-example-ahoy:
edit, this will work:
myDir=(./*) if [ ${#myDir[@]} -gt 1 ]; then echo "there's something down here"; fi