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
The top solution
(if [ -e *.flac ];)
did not work for me, giving:[: too many arguments
if ls *.flac >/dev/null 2>&1;
then it will work.bash only:
I use parentheses instead of braces to ensure a subshell is used (don't want to clobber your current
nullglob
setting).You can use -f to check whether files of a specific type exist:
You need to be carful which flag you throw into your
if
statement, and how it relates to the outcome you want.If you want to check for only regular files and not other types of file system entries then you'll want to change your code skeleton to:
The use of the
-f
restricts theif
to regular files, whereas-e
is more expansive and will match all types of filesystem entries. There are of course other options like-d
for directories, etc. See http://tldp.org/LDP/abs/html/fto.html for a good listing.As pointed out by @msw,
test
(i.e.[
) will choke if you try and feed it more than one argument. This might happen in your case if the glob for*.flac
returned more than one file. In that case try wrapping yourif
test in a loop like:This way you
break
on the first instance of the file extension you want and can keep on going with the rest of the script.