LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Blogs > drask
User Name
Password

Notices

Rate this Entry

Find and Prune

Posted 07-06-2012 at 01:05 PM by drask
Updated 07-06-2012 at 04:13 PM by drask

Okay, I've been frustrated with the prune option of the find command for a long time. I have a folder in my home directory called "mount" where I have directories onto which I mount the foreign filesystems I work with on a regular basis. I often know that there is a file containing a word or phrase that I need to find, and it is somewhere in my home directory. I've been trying to use prune to skip searching that particular folder, especially when grepping for something inside a file. For instance, say I have a 10 TByte hard drive mapped to my mount/webserver directory. "find . -exec grep -Hi 'Drask' {} \;" would take forever to complete, and may return a lot of results I'm not interested in since I only want to search my local machine since it has to copy each file from the server into memory in order to search it for the string 'drask'.

I kept trying things like

Code:
find . -path ./mount -prune -exec grep -Hi 'Drask' {} \;
but this would never find anything, because what is happening here is find is searching for a folder with the path of ./mount (-path ./mount). Once it finds it, it does not descend into it (-prune). It then checks any results to see if they contain the string 'Drask'. Since the only result will be the mount folder itself, it will not contain 'Drask' and so nothing gets sent to print, although every file and folder in the current directory and subdirectories (excepting './mount' itself) is still searched to see if it matches ./mount, which takes time (find isn't going to make any assumptions for you).

The secret is to put an '-o' (or) directly after the prune, so anything that was not pruned will be grepped.

Take the following directory structure:

./Test
/Test2
/Test3
/Test4
If you sit in the Test directory and enter

Code:
find . -path ./Test2 -prune -o print
you get the following output:
Code:
.
./Test4
so to search all the files in the current directory that are not in the "mount" directory, I have to do something like this (the parenthesis aren't strictly necessary, but they help me keep track of what's going on):

Code:
find . \( -path ./mount -prune \) -o \( -exec grep -Hi 'Drask' {} \; \) -print
To do the same thing in the root directory, I would use

Code:
find / \( -path /home/drask/mount -prune \) -o \( -exec grep -Hi 'Drask' {} \; \) -print 2>/dev/null
The 2>/dev/null at the end is just so I don't have to search through pages and pages of "permission denied" errors to find my actual search results.

I don't know if that is any clearer than any of the other explanations out there, but the main point is that "-o" means "or" and it is very important when using the prune directive.
Posted in Uncategorized
Views 879 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 11:20 PM.

Main Menu
Advertisement

My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration