Check whether a certain file type/extension exists in directory

LinuxBashShell

Linux Problem Overview


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 true; 
fi 

Linux Solutions


Solution 1 - Linux

#!/bin/bash

count=`ls -1 *.flac 2>/dev/null | wc -l`
if [ $count != 0 ]
then 
echo true
fi 

Solution 2 - Linux

#/bin/bash

myarray=(`find ./ -maxdepth 1 -name "*.py"`)
if [ ${#myarray[@]} -gt 0 ]; then 
    echo true 
else 
    echo false
fi

Solution 3 - Linux

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

Solution 4 - Linux

shopt -s nullglob
if [[ -n $(echo *.flac) ]]    # or [ -n "$(echo *.flac)" ]
then 
    echo true
fi

Solution 5 - Linux

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.

Solution 6 - Linux

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.

Solution 7 - Linux

#!/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

Solution 8 - Linux

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

#!/bin/bash

if [ -f *.flac ] ; then
   echo true
fi 

Solution 9 - Linux

   shopt -s nullglob
   set -- $(echo *.ext)
    if [ "${#}" -gt 0 ];then
      echo "got file"
    fi

Solution 10 - Linux

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.

Solution 11 - Linux

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).

Solution 12 - Linux

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.

Solution 13 - Linux

Here's a fairly simple solution:

if [ "$(ls -A | grep -i \\.flac\$)" ]; then echo true; fi

As you can see, this is only one line of code, but it works well enough. It should work with both bash, and a posix-compliant shell like dash. It's also case-insensitive, and doesn't care what type of files (regular, symlink, directory, etc.) are present, which could be useful if you have some symlinks, or something.

Solution 14 - Linux

I tried this:

if [ -f *.html ]; then
    echo "html files exist"
else
    echo "html files dont exist"
fi

I used this piece of code without any problem for other files, but for html files I received an error:

[: too many arguments

I then tried @JeremyWeir's count solution, which worked for me:

count=`ls -1 *.flac 2>/dev/null | wc -l`
if [ $count != 0 ]
then 
echo true
fi 

Keep in mind you'll have to reset the count if you're doing this in a loop:

count=$((0))

Solution 15 - Linux

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWurlitzerView Question on Stackoverflow
Solution 1 - LinuxJeremyWeirView Answer on Stackoverflow
Solution 2 - Linux逆さまView Answer on Stackoverflow
Solution 3 - LinuxfrayserView Answer on Stackoverflow
Solution 4 - LinuxDennis WilliamsonView Answer on Stackoverflow
Solution 5 - LinuxdtlussierView Answer on Stackoverflow
Solution 6 - LinuxErik BuitenhuisView Answer on Stackoverflow
Solution 7 - LinuxRuelView Answer on Stackoverflow
Solution 8 - LinuxUziView Answer on Stackoverflow
Solution 9 - Linuxghostdog74View Answer on Stackoverflow
Solution 10 - LinuxIsoView Answer on Stackoverflow
Solution 11 - Linuxglenn jackmanView Answer on Stackoverflow
Solution 12 - LinuxjochenView Answer on Stackoverflow
Solution 13 - LinuxTSJNachos117View Answer on Stackoverflow
Solution 14 - LinuxGChufView Answer on Stackoverflow
Solution 15 - LinuxTSJNachos117View Answer on Stackoverflow