Check whether a certain file type/extension exists in directory

前端 未结 15 1583
时光取名叫无心
时光取名叫无心 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:43

    This should work in any borne-like shell out there:

    if [ "$(find . -maxdepth 1 -type f | grep -i '.*\.flac$')" ]; then
        echo true
    fi
    

    This also works with the GNU find, but IDK if this is compatible with other implementations of find:

    if [ "$(find . -maxdepth 1 -type f -iname \*.flac)" ]; then
        echo true
    fi
    

提交回复
热议问题