From: Joerg Bruehe <joerg@sql.de>
Subject: Re: Check for files in a directory
Date: Thu, 26 Oct 2000 11:07:06 +0200 [thread overview]
Message-ID: <39F7F43A.DB0D8C82@sql.de> (raw)
In-Reply-To: svernidqsb8975@corp.supernews.com
Hi !
Arthur Dalessandro wrote:
>
> I've seen the man test pages, which show if a file exists, is there a quick
> and dirty way of checking whether a given directory contains any files? I
> have a cron job that I want to run, the job should only be run if these temp
> files exist in the given directory. Any help would be appreciated.
Several ways (in shell):
find $DIR -type f -print
yields results only if there are files (somewhere) _below_ that directory.
I am no expert in the '-prune' etc. options to restrict it to
files _in_ that directory, but that should be possible.
cd $DIR
echo * | cut -c1
(minus c one) yields "*" if there are no entries, the first character
of the (alphabetically) first file if there are entries.
(Fails if you have entries starting with '*' - which were bad design IMO.)
This does _not_ differ between files and subdirectories!
Shell script:
# echo_first
echo $1
called as
echo_first *
works similar to previous approach, useful in "test" command:
cd $DIR
if [ `echo_first *` != '*' ]
then ...
Both last approaches fail if the directory contains so many files
that "*" expands to too many characters,
and also if you want to detect "hidden" files (name starting with '.').
Note that testing "*" directly gives syntax errors if there are two
or more files, you need to restrict it to a single word.
Regards
and happy scripting !
Joerg Bruehe
--
Joerg Bruehe, SQL Datenbanksysteme GmbH, Berlin, Germany
(speaking only for himself)
mailto: joerg@sql.de
prev parent reply other threads:[~2000-10-26 9:07 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2000-10-25 4:00 Check for files in a directory Arthur Dalessandro
2000-10-26 6:17 ` Eckhardt Wallmeroth
2000-10-26 6:28 ` Adam Price
2000-10-26 9:07 ` Joerg Bruehe [this message]
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox