I would like to delete all files under the directory (and
subdirectories) as part of cron job but would like to exclude few
dirs. I tried -prune and ! options, but they are not quiet working.
Any suggestions ?
this works - delete all files older than 30 days
find /var/tmp -name '*' -mtime +30 -exec rm {} \;
I would like to exclude dir call "unix" under /var/tmp (as I do not
want to delete files under unix subdir)
does not work
-------------------
find /var/tmp (-name '*' && ! -name unix\) -mtime +30 -exec rm {} \;
does not work
--------------------
find /var/tmp -name '*' unix -prune -mtime +30 -exec rm {} \;
thank you in advance.
KP
Try something like:
find /var/tmp -name unix -prune -o -mtime +30 -exec rm {} \;
Test it with -print rather than -exec ...
Also, if there are lots of files to delete, using xargs is
more efficient:
find /var/tmp -name unix -prune -o -mtime +30 -print | xargs rm
--
Andrew Gabriel
[email address is not usable -- followup in the newsgroup]
> find [...] -exec rm {} \;
>
> Also, if there are lots of files to delete, using xargs
just turn the ; into a +
(using xargs on Solaris would be odd,
because -exec + was introduced with SVR4)
> Try something like:
>
> find /var/tmp -name unix -prune -o -mtime +30 -exec rm {} \;
>
> Test it with -print rather than -exec ...
>
> Also, if there are lots of files to delete, using xargs is more
> efficient:
>
> find /var/tmp -name unix -prune -o -mtime +30 -print | xargs rm
Using find and xargs like that on a publicly-writable directory
is just asking for trouble.
Using find with "-exec rm {} +" as Sven suggested is just as
efficient (in fact more so) and is also safe.
--
Geoff Clare <net...@gclare.org.uk>
So you don't want to delete dotfiles? I'm not sure why you're
specifying "-name '*'" at all here. It would find all files if you left
it off.
Do you want to delete directories as well? Does the age of the
directory matter, or do you just want it gone if it's empty? You can't
remove a directory with a bare 'rm', you need 'rmdir' or 'rm -r'.
You need to use -prune to keep from descending into the directory. The
directory itself will match -name "unix", but none of the files within
will (so they'll get zapped).
--
Darren