How would you go about telling whether files of a specific extension are present in a directory, with bash?
Something like
if [ -e *.flac ]; then
echo true;
fi
How would you go about telling whether files of a specific extension are present in a directory, with bash?
Something like
if [ -e *.flac ]; then
echo true;
fi
For completion, with zsh:
This is listed at the end of the Conditional Expressions section in the zsh manual. Since
[[
disables filename globbing, we need to force filename generation using(#q)
at the end of the globbing string, then theN
flag (NULL_GLOB
option) to force the generated string to be empty in case there’s no match.This uses ls(1), if no flac files exist, ls reports error and the script exits; othewise the script continues and the files may be be processed
Here's a fairly simple solution:
if [ "$(ls -A | grep -i \\.flac\$)" ]; then echo true; fi
As you can see, this is only one line of code, but it works well enough. It should work with both bash, and a posix-compliant shell like dash. It's also case-insensitive, and doesn't care what type of files (regular, symlink, directory, etc.) are present, which could be useful if you have some symlinks, or something.