Check whether a certain file type/extension exists in directory

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

    bash only:

    any_with_ext () ( 
        ext="$1"
        any=false
        shopt -s nullglob
        for f in *."$ext"; do
            any=true
            break
        done
        echo $any 
    )
    
    if $( any_with_ext flac ); then
        echo "have some flac"
    else 
        echo "dir is flac-free"
    fi
    

    I use parentheses instead of braces to ensure a subshell is used (don't want to clobber your current nullglob setting).

提交回复
热议问题