[CentOS] rm user:group

Brian Mathis brian.mathis at gmail.com
Tue Mar 10 16:03:38 UTC 2009


On Tue, Mar 10, 2009 at 10:39 AM, Matt <lm7812 at gmail.com> wrote:
> I want to remove files but only if they are owned by a certain user and group.
>
> Basically I have this:
>
> find /var/spool/greylist -mmin +363 -exec rm -f {} \;
>
> I want to make sure it only deletes files owned by mail.  Basically no
> matter what weird characters are in the file names I want to make sure
> it does not delete anything outside of /var/spool/greylist.  I can add
> 'sudo -u' to it but then my secure log gets filled with entries but
> perhaps thats the only way to do it.
>
> Matt


I don't see anything in that find statement that selects the user and
group combination you say you are looking for.  I think you are
looking for the -user and -group options.  Also, if you want to see
what it's going to do beforehand, don't go into it blind and use the
-print option instead of the -exec.  Once you know that -print only
prints the files you want to delete, then you can switch -print to
-exec.
    find /var/spool/greylist -user joe -group devs -print

However, if you have files with spaces or other special characters in
the name, you will need to quote the {}, or the better thing to do is
to use xargs with null delimiters, like so:
    find /var/spool/greylist -user joe -group devs -print0 | xargs -0 rm -f

If you need to use sudo, you can put the sudo in the xargs, and it
will only get called every so often, instead of once per file:
    find /var/spool/greylist -user joe -group devs -print0 | sudo xargs -0 rm -f


More information about the CentOS mailing list