Operating System - HP-UX
1831061 Members
2370 Online
110019 Solutions
New Discussion

Re: Using prune to exclude directories when running find command.

 
SOLVED
Go to solution
Kurt Karl
New Member

Using prune to exclude directories when running find command.

Hi,

I'm having a hard time figuring out the right "find" syntax with "prune". I have these directories and files in it:

/tmp/dir1
/tmp/dir1/one
/tmp/dir1/two
/tmp/dir2
/tmp/dir2/one
/tmp/dir2/two
/tmp/dir3
/tmp/dir3/one
/tmp/dir3/two

Now I want to find and delete all files having the name "one" but exclude those belongs to /tmp/dir/two directory.

Any help would be greatly appreciated.
Thanks in advance
Kurt
4 REPLIES 4
Stefan Farrelly
Honored Contributor

Re: Using prune to exclude directories when running find command.

hope this helps - you need to use the ! -name options for what you want;

To delete all files in a dir but DONT include subdirs;

find . \( -type d ! -name . -prune \) -o \( -type f -name "*" ! -name "." -print \) -exec rm {} \;

To delete all files older than 7 days in current dir only (no subdirs);

find . \( -type d ! -name . -prune \) -o \( -type f -mtime +7 ! -name "." -print \) -exec rm {} \;

To delete all subdirs in a dir;

find . \( -type f ! -name . -prune \) -o \( -type d -name "*" ! -name "lost+found" ! -name "." -print \) -exec rmdir {} \;

Im from Palmerston North, New Zealand, but somehow ended up in London...
Peter Nikitka
Honored Contributor
Solution

Re: Using prune to exclude directories when running find command.

Hi,

in the thread

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=241011

I suggested this solution to a similar question:
...
find . \( -name ign1 -prune -o -name ign2 -prune \) -o -print

For a (variable) list of dirs I simply generate the option-arglist for find:
excl='one
two
three'

findopt=$(print "$excl" |
awk 'NR==1 {printf("( -name %s -prune ",$1);next}
{printf("-o -name %s -prune ",$1)}
END {print(")")}')

find . $findopt -o -print
...

mfG Peter
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"
Rodney Hills
Honored Contributor

Re: Using prune to exclude directories when running find command.

Using -path can do it instead of -prune. You would use -prune to prevent descending to a certain level in the tree structure.

find /tmp -type f -name one ! -path "/tmp/dir/two*" -exec rm {} \;

HTH

-- Rod Hills
There be dragons...
Ray Carlson
Frequent Advisor

Re: Using prune to exclude directories when running find command.

This message reminded me of a problem with the HP version of the "find" command that has existed for years. I haven't tested it lately, but will test again when I have time. The problem is that the -prune does not stop the find from going down the directory tree, it only stops find from reporting results from having gone down the directory tree. Normally, this would only be a slight waste of cpu cycles and time while it follows the directories and then exludes the results from the -prune directory. However, when the HP is running the AFS cache manager, the find tries to follow the /afs directory and circle the world. The find only gives up a couple of days later after filling /tmp or /var/tmp with it's working file.