Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 09 Mar 1999 20:07:50 +1000
From:      Greg Black <gjb@comkey.com.au>
To:        cjclark@home.com
Cc:        chris@tci.com, mikegoe@ibm.net, freebsd-questions@FreeBSD.ORG
Subject:   Re: /var -- Device Busy 
Message-ID:  <19990309100750.9249.qmail@alpha.comkey.com.au>
In-Reply-To: <199903082028.PAA23450@cc942873-a.ewndsr1.nj.home.com>  of Mon, 08 Mar 1999 15:28:27 EST
References:  <199903082028.PAA23450@cc942873-a.ewndsr1.nj.home.com> 

next in thread | previous in thread | raw e-mail | index | archive | help
> > > > Hmmm...I've always used rm -r to remove
> > > > directories...didn't even realize there was a rmdir
> > > > command...I guess there's many ways to do the same thing...
> > > > :)
> > > 
> > > Are you sure you weren't using rm -rf?
> > 
> > Do the following and answer your own question:
> > 
> >     mkdir testdir
> >     touch testdir/file
> >     rm -r testdir 
> > 
> > Of course, if you are not root and if some elements in the tree
> > are read-only, then you'll get asked about them without the -f
> > flag -- but the -f flag will not allow you to rm anything that
> > you could not rm without using -f (which means that -f has no
> > meaning when used by root).
> 
> Not true. The '-f' flag will prevent error messages about files that
> do not exist as well as cause 'rm' to exit on a 0 in such a situation.

This is a bizarre nit to pick.  The context is using rm -r
interactively to remove directories (and their contents).

Given that starting point, the only way that root would get a
different outcome with the -f flag is if a directory or file
listed on the command line didn't exist, in which case the
command would silently fail.  So adding -f doesn't buy you
anything useful in this unique situation and changes nothing in
the context of interest which was in the recursive operation
inside the director{y,ies}.

> This can be important in scripts and makefiles.

It's my view that scripts or makefiles that run rm -rf on a
non-existent directory are broken.  It's one thing to dash about
using something like rm -f $(OBJS) in a makefile without first
checking if the files exist (although a good case can be made
for not doing this, either) but it's insane to do a recursive rm
on stuff that's not there.

As an illustration, instead of the common form:

	clean:
		rm -f $(PROGS) $(OBJS)

we can use:

	clean:
	.for F in $(PROGS) $(OBJS)
	.if exists ($F)
		rm $F
	.endif
	.endfor

This way, we don't try to remove stuff that doesn't exist and we
get meaningful error messages if we have it wrong.  If we don't
want this to stop the make from continuing, we all know (or know
how to discover) the syntax for that.

Similarly, in a shell script, rather than:

	rm -f $FILES

we can use:

	for F in $FILES ; do [ -e $F ] && rm $F ; done

for the same benefits.

-- 
Greg Black <gjb@acm.org>



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?19990309100750.9249.qmail>