* Check for files in a directory
@ 2000-10-25 4:00 Arthur Dalessandro
2000-10-26 6:17 ` Eckhardt Wallmeroth
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Arthur Dalessandro @ 2000-10-25 4:00 UTC (permalink / raw)
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.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Check for files in a directory
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
2 siblings, 0 replies; 4+ messages in thread
From: Eckhardt Wallmeroth @ 2000-10-26 6:17 UTC (permalink / raw)
[ -n "`ls /your/directory`" ] && {
/run/your/job
}
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Check for files in a directory
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
2 siblings, 0 replies; 4+ messages in thread
From: Adam Price @ 2000-10-26 6:28 UTC (permalink / raw)
"Arthur Dalessandro" <adalessandro@odione.com> wrote in message news:svernidqsb8975@corp.supernews.com...
> 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.
>
>
You can do something like
cd $dir
count=$(ls -la | wc -l)
and check for a count greater than 2, or you can make use of the fact that
rmdir will fail if there are files in the directory. If it succeeds then recreate the
dir.
Hope this helps
Adam
PS Try searching comp.unix.shell on deja.com this question has come up recently
and I think there were some better answers than the 2 I gave above but I'm not
sure.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Check for files in a directory
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
2 siblings, 0 replies; 4+ messages in thread
From: Joerg Bruehe @ 2000-10-26 9:07 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2000-10-26 9:07 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox