Operating System - HP-UX
1836372 Members
2672 Online
110100 Solutions
New Discussion

find files to remove but skip subdirectories

 
SOLVED
Go to solution
Frank de Vries
Respected Contributor

find files to remove but skip subdirectories

I have an hpux 11.00 box
with a /tmp that has number (about 20)
of subdirectories like .oracle and .tivoli
and a couple of others , which I do not like to search for removal.

I want to cleanup all files older then 7 days
in /tmp but not in /tmp/.oracle and /tmp/.tivoli, /tmp/xxx/, /tmp/yyy etc..

find . -name "*" -type f -print -mtime +7 -exec rm {} \;
Will remove also files in the subdirectories
of /tmp when the files are older then 7 days.

Thanks for your input.



Look before you leap
6 REPLIES 6
Peter Godron
Honored Contributor

Re: find files to remove but skip subdirectories

Jean-Yves Picard
Trusted Contributor

Re: find files to remove but skip subdirectories

hello,

what about

find /tmp \( -name .oracle -prune \) \( -name .tivoli -prune \) -type f -print -mtime +7 -exec rm {} \;

or in case you like to search .oracle (et all) file in deep subdirectory

find . -name "*" -type f -mtime +7 -print |\
awk -F/ '$2 == ".oracle" { next }
$2 == ".tivoli" { next }
{printf "rm -f %s\n",$0 ;}' | ksh

Jean-Yves Picard
Pete Randall
Outstanding Contributor

Re: find files to remove but skip subdirectories

The -prune option is generally recommended but I have to wonder:

# find /tmp |wc -l
79

# find /tmp -prune |wc -l
1

# find /tmp/* -prune |wc -l
69

# find /tmp/* |wc -l
69

So, following down through subdirectories, we come up with 79 entries. With prune, we only see /tmp itself. By changing the starting point and using prune, we come up with 69 entries. By changing the starting point and not bothering with prune, we still come up with 69 entries. It leads me to question what prune is really doing???????


Pete

Pete
James R. Ferguson
Acclaimed Contributor

Re: find files to remove but skip subdirectories

Hi Frank:

# find /tmp -type f -mtime +7 ! -path "/tmp/.oracle/*" -a ! -path "/tmp/.tivoli/*" | xargs rm

Note the use of '! -path'.

Regards!

...JRF...
Peter Nikitka
Honored Contributor
Solution

Re: find files to remove but skip subdirectories

Hi,

why use find, when you do not want to look for files recursivly?
Suggestion:

> date
Wed Oct 25 14:04:01 METDST 2006
> cd /tmp
> touch 10181405 .rmflag
> for f in *
do
[ -d $f -o $f -nt .rmflag ] && continue
rm -i $f
done

Drop the -i - flag if you are shure ...
If you need to create .rmflag dynamically, you'll have to do date arithmetics.

mfG Peter



date
touch
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Frank de Vries
Respected Contributor

Re: find files to remove but skip subdirectories

Pulling together the bits of info
from all your answers I constructed this
find command that works for me.

Please note I have over 20 subdirectories,
and I am very lazy typist so the answers
with ( !path .oracle ) are not for me.

find /tmp/* -type f -prune -mtime +3 -print -exec rm {} \;

this removes only files stricly in /tmp and
not in its subdirectories and it prints which ones were older then 3 days.
Look before you leap