LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   using find to only search specific directories (https://www.linuxquestions.org/questions/linux-software-2/using-find-to-only-search-specific-directories-781010/)

isaaclw 01-09-2010 03:41 AM

using find to only search specific directories
 
I'm trying to figure our how the "-prune" option works. I've searched quite a bit on line, and as far as I can tell, "-prune" works exactly the opposite as it says.

I'm using Apt-proxy, and I want to scan through the folders, and find files that end with "*.bz2"
The problem is that the search takes a while because of all the "*.deb" files.
Fortunately, they're stored in their own folder:

/var/cache/apt-proxy/ubuntu
/var/cache/apt-proxy/ubuntu-security
/var/cache/apt-proxy/partner
each have two folders:
"pool", "dist"

I want to go into the "dist" folders, and find "*.bz2" files, but not the "pool" folders.

so I tried:
Code:

find /var/cache/apt-proxy/ -path "*pool" -prune
and it returned only the "pool" directories without even going down.
adding the option "-mindepth 6" doesn't do anything... (running mindepth 6 by itself with -name "*.bz2" returns all the files)

I was hoping to write this out so that find will return the file as fast as possible, since there really isn't that much to search (assuming the "pool" folder is skipped.)

What am I doing wrong?

David the H. 01-09-2010 05:44 AM

As I understand it, -prune basically says to ignore the previous name match, but it doesn't automatically say to print anything else. If you want find to continue to search for other paths, use the -o option to add them to the command afterwards.

I think this should work, but I can't actually test it myself, since I don't have an apt-proxy directory. :)
Code:

find /var/cache/apt-proxy/ -path "*pool" -prune -o "*.bz2"

isaaclw 01-09-2010 05:58 AM

Quote:

Originally Posted by David the H. (Post 3819994)
Code:

find /var/cache/apt-proxy/ -path "*pool" -prune -o "*.bz2"

That throws an error:
Code:

find: paths must preceede expression: *.bz2
Usage: .....

So I tried:
Code:

find /var/cache/apt-proxy/ -path "*pool" -prune -o -name "*.bz2"
But it's listing the "pool" folders.
Its no big deal (I can cut them out if I have to) , but I'm still unsure why it's even including them...

David the H. 01-09-2010 06:04 AM

Whoops, sorry. Forgot to add the -name option there. Truth is, I'm not an expert on find. Perhaps using -wholename instead of -path would help?

You can read more about find, and the prune option, here.
http://www.grymoire.com/Unix/Find.html#uh-16

colucix 01-09-2010 07:29 AM

Quote:

Originally Posted by David the H. (Post 3820006)
Perhaps using -wholename instead of -path would help?

You caught the point, David. The following should work:
Code:

find /var/cache/apt-proxy -wholename \*pool -prune -o -name \*.bz2 -print
@isaaclw: you can also take a look at this post, here on LinuxQuestions... a little self-citation! :D

isaaclw 01-09-2010 08:01 AM

ok. this worked exactly how I needed:
Code:

find /var/cache/apt-proxy/ -mindepth 6 -path "*pool" -prune -o -name "*.bz2"
Adding the "mindepth" removed the "pool" folders from the output.

I'll check out the wholename option, but mostly just to learn how find works...


All times are GMT -5. The time now is 10:02 AM.