Here’s a suggestion for a test, in pure shell:
for i in "$input_dir"/*.tgz "$input_dir"/.*.tgz; do
test -e "$i" && break
done
This returns success or failure, depending on if .tgz
files exist. You can obviously remove the second pattern if you want to ignore hidden files.
As a function/example:
suffix_exists ()
{
for i in "${2:-.}"/*"$1" "${2:-.}"/.*"$1"; do
test -e "$i" && return 0
done
}
if suffix_exists .tgz "$input_dir"; then
echo "$input_dir contains .tgz archives"
else
echo "$input_dir does not contain .tgz archives"
fi
It uses the current directory if none is provided.
As mentioned in another answer, you might be approaching your task in the wrong way (perhaps post your objective). Instead of detecting .tgz
archives in the directory, you could run a command on any archives that do exist, using find
. For example extracting them:
find "$input_dir" -mindepth 1 -maxdepth 1 \
-type f -name '*.tgz' -exec tar xf {} \;
Or moving them somewhere:
find "$input_dir" -mindepth 1 -maxdepth 1 \
-type f -name '*.tgz' -exec mv -i {} /my/archives \;