Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

find and delete files but exclude specific dirs

708 views
Skip to first unread message

khpa...@gmail.com

unread,
Jun 1, 2009, 3:27:23 PM6/1/09
to
Greetings All!

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

Andrew Gabriel

unread,
Jun 1, 2009, 3:37:40 PM6/1/09
to
In article <2a5bf027-6b10-451c...@o30g2000vbc.googlegroups.com>,

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]

Sven Mascheck

unread,
Jun 1, 2009, 4:01:54 PM6/1/09
to
Andrew Gabriel wrote:

> 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)

Geoff Clare

unread,
Jun 2, 2009, 8:55:55 AM6/2/09
to
Andrew Gabriel wrote:

> 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>

Darren Dunham

unread,
Jun 2, 2009, 11:57:37 AM6/2/09
to
khpa...@gmail.com wrote:
> Greetings All!
>
> 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 {} \;

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

0 new messages