Check whether a certain file type/extension exists in directory

前端 未结 15 1588
时光取名叫无心
时光取名叫无心 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 07:01
    #!/bin/bash
    files=$(ls /home/somedir/*.flac 2> /dev/null | wc -l)
    if [ "$files" != "0" ]
    then
    echo "Some files exists."
    else
    echo "No files with that extension."
    fi
    
    0 讨论(0)
  • 2021-01-30 07:01

    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.

    0 讨论(0)
  • 2021-01-30 07:02
    shopt -s nullglob
    if [[ -n $(echo *.flac) ]]    # or [ -n "$(echo *.flac)" ]
    then 
        echo true
    fi
    
    0 讨论(0)
提交回复
热议问题