help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] The best way to test whether a directory has *.txt files


From: John Kearney
Subject: Re: [Help-bash] The best way to test whether a directory has *.txt files?
Date: Thu, 28 Jun 2012 07:49:31 +0200
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20120614 Thunderbird/13.0.1

Am 28.06.2012 05:27, schrieb Peng Yu:
Hi,

There can be more than one way to test whether a directory contains
*.txt files (to be used with 'find -exec'). But I'm wondering what is
the best way (in terms of convenience to type) to do it. Does anybody
have any tricks on this?

Depends on what you want to do and how portable it needs to be.
Without putting too much thought into it. the following will return the number of files in a directory matching *.txt .

FileCnt=$( find /tmp -name "*.txt" | wc -l )

or

function CntTxtFilesInDir {
    local Files=( "${1}"/*.txt )
    if [ address@hidden -eq 1 -a "${Files[0]}" = "*.txt" ]; then
        return 0
    else
        return address@hidden
    fi
}
CntTxtFilesInDir  /tmp
if [ $? -gt 0 ] ; then
    echo "yah txt files exist in /tmp"
fi


--
View John
          Kearney's profile on LinkedIn John Kearney

reply via email to

[Prev in Thread] Current Thread [Next in Thread]