Check whether a certain file type/extension exists in directory

前端 未结 15 1852
时光取名叫无心
时光取名叫无心 2021-01-30 06:12

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 t         


        
15条回答
  •  太阳男子
    2021-01-30 06:36

    For completion, with zsh:

    if [[ -n *.flac(#qN) ]]; then
      echo true
    fi
    

    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 the N flag (NULL_GLOB option) to force the generated string to be empty in case there’s no match.

提交回复
热议问题