Check whether a certain file type/extension exists in directory

前端 未结 15 1589
时光取名叫无心
时光取名叫无心 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:45
       shopt -s nullglob
       set -- $(echo *.ext)
        if [ "${#}" -gt 0 ];then
          echo "got file"
        fi
    
    0 讨论(0)
  • 2021-01-30 06:47

    This uses ls(1), if no flac files exist, ls reports error and the script exits; othewise the script continues and the files may be be processed

    #! /bin/sh
    ls *.flac  >/dev/null || exit
    ## Do something with flac files here
    
    0 讨论(0)
  • 2021-01-30 06:48

    Here is a solution using no external commands (i.e. no ls), but a shell function instead. Tested in bash:

    shopt -s nullglob
    function have_any() {
        [ $# -gt 0 ]
    }
    
    if have_any ./*.flac; then
        echo true
    fi
    

    The function have_any uses $# to count its arguments, and [ $# -gt 0 ] then tests whether there is at least one argument. The use of ./*.flac instead of just *.flac in the call to have_any is to avoid problems caused by files with names like --help.

    0 讨论(0)
  • 2021-01-30 06:49

    You can use -f to check whether files of a specific type exist:

    #!/bin/bash
    
    if [ -f *.flac ] ; then
       echo true
    fi 
    
    0 讨论(0)
  • 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).

    0 讨论(0)
  • 2021-01-30 06:59

    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:

    if [ -f file ]; then
    echo true;
    fi
    

    The use of the -f restricts the if 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 your if test in a loop like:

    for file in ./*.pdf
    do
        if [ -f "${file}" ]; then
        echo 'true';
        break
        fi
    done
    

    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.

    0 讨论(0)
提交回复
热议问题