From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fa528,8fef2fe508180e8d X-Google-Attributes: gidfa528,public X-Google-ArrivalTime: 2000-10-26 03:45:23 PST Path: supernews.google.com!sn-xit-02!sn-xit-03!supernews.com!newsfeed.direct.ca!look.ca!newsfeed.icl.net!colt.net!diablo.theplanet.net!unlisys!news.snafu.de!news.sql.de!not-for-mail From: Joerg Bruehe Newsgroups: comp.unix.tru64 Subject: Re: Check for files in a directory Date: Thu, 26 Oct 2000 11:07:06 +0200 Organization: SQL Datenbanksysteme GmbH Message-ID: <39F7F43A.DB0D8C82@sql.de> References: NNTP-Posting-Host: welcome.sql.de Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: news.sql.de 972551226 1600 194.64.230.128 (26 Oct 2000 09:07:06 GMT) X-Complaints-To: news@news.sql.de NNTP-Posting-Date: 26 Oct 2000 09:07:06 GMT X-Mailer: Mozilla 4.7 [en] (WinNT; I) X-Accept-Language: en Xref: supernews.google.com comp.unix.tru64:653 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