SUMMARY: A negative rule in find

From: Christopher Barnard <cbarnar1_at_earthlink.net>
Date: Tue Mar 02 2010 - 22:13:22 EST
As several folks realized and pointed out, yes I had two questions with
find in the same script.  I separated them into two posts so that they
could have separate summaries.

I asked

> I want to delete all files over a certain age in a certain directory except
> for those owned by root and one other user.  Unfortunately, find does not
> appear to honor regex.  So !root (or !(root)) does not appear to work.
What
> I want to do is
>
> /usr/bin/find /export/temp -user !(root || cacheadm) -exec /bin/rm {} \;

> unfortunately find is interpreting the ! as part of the usertname.  Anyone
> have other suggestions on how else I would do this?

The answer

Find *does* honor regex, I just wasn't looking at it right.  You cannot
negate the parameter to an expression, you negate the expression itself.
So it isn't
-user !root
it is
! -user root

the code snippet is
###
### Should files owned by certain users be excluded from the list?
### Uncomment exactly one of the ADD definitions below.
### No users to exclude?  Uncomment this line.
### ADD=""
###
### Exclude any files owned by root? Uncomment this line.
### ADD=" ! -user root"
###
### Exclude any files owned by root or epicadm? Uncomment this line.
ADD="! -user root -a ! -user epicadm"
###
[...]
/usr/bin/find ${SOURCEDIR} ${ADD} -type f -mtime +${MAXAGE} -print

other suggestions I received:
* use -not -user instead.  That generated a syntax error.
* escape the !  (ie, \!).  That works on the command line, but within a
script
  you are escaping an escape, so it no longer behaves nicely.
* do an ls grepping out the user before feeding it to find.
* many many suggestions to get the negation to the -user expression, not the
  parameter to the expression (the username), which was the bulk of my
problem.
* use gnu find to get regex.  No, I did not have to go with gnu find, I just
  needed to get the regex syntax correct.  <grin>
* make sure you ask find to the regex, not the shell.  The shell just does
  plain simple globbing.
* use the -prune directive (-user root -prune)  No, that is not what -prune
  is for.
* explicitly list all of the users for which it is ok to delete their files.
  hmmm.  maybe not.
* use perl.  I'd rather not kill a mosquito with a bazooka.

This list is wonderful.  Thanks to the 28 individuals (and counting) who
gave me suggestions.


Christopher L. Barnard
-------------------
comment your code as if the maintainer is a homicidal maniac who knows where
you live.
_______________________________________________
sunmanagers mailing list
sunmanagers@sunmanagers.org
http://www.sunmanagers.org/mailman/listinfo/sunmanagers
Received on Tue Mar 2 22:13:18 2010

This archive was generated by hypermail 2.1.8 : Thu Mar 03 2016 - 06:44:15 EST