I don't quite understand the example given from the 'man find', can anyone give me some examples and explanations? Can I combine regular expression in it?
the more detailed question is like this: write a shell script, changeall, which has an interface like "changeall [-r|-R] "string1" "string2". It will find all files with an suffix of .h, .C, .cc, or .cpp and change all occurrences of "string1" to "string2". -r is option for staying in current dir only or including subdir's. NOTE: 1) for non-recursive case, 'ls' is NOT allowed, we could only use 'find' and 'sed'. 2) I tried 'find -depth' but it was NOT supported. That's why I was wondering if '-prune' could help, but didn't understand the example from 'man find'.
EDIT2: I was doing assignment, I didn't ask question in great details because I would like to finish it myself. Since I already done it and hand it in, now I can state the whole question. Also, I managed to finish the assignment without using -prune, but would like to learn it anyway.
The thing I'd found confusing about about
-prune
is that it's an action (like-print
), not a test (like-name
). It alters the "to-do" list, but always returns true.The general pattern for using
-prune
is this:You pretty much always want the the
-o
immediately after-prune
, because that first part of the test (up to including-prune
) will return false for the stuff you actually want (ie: the stuff you don't want to prune out).Here's an example:
This will find the "*.foo" files that aren't under ".snapshot" directories. In this example,
-name .snapshot
is the "tests for stuff you want to prune", and-name '*.foo' -print
is the "stuff you'd normally put after the path".Important notes:
If all you want to do is print the results you might be used to leaving out the
-print
action. You generally don't want to do that when using-prune
.The default behavior of find is to "and" the entire expression with the
-print
action if there are no actions other than-prune
(ironically) at the end. That means that writing this:is equivalent to writing this:
which means that it'll also print out the name of the directory you're pruning, which usually isn't what you want. Instead it's better to explicitly specify the
-print
action if that's what you want:If your "usual condition" happens to match files that also match your prune condition, those files will not be included in the output. The way to fix this is to add a
-type d
predicate to your prune condition.For example, suppose we wanted to prune out any directory that started with
.git
(this is admittedly somewhat contrived -- normally you only need to remove thing named exactly.git
), but other than that wanted to see all files, including files like.gitignore
. You might try this:This would not include
.gitignore
in the output. Here's the fixed version:Extra tip: if you're using the GNU version of
find
, the texinfo page forfind
has a more detailed explanation than its manpage (as is true for most GNU utilities).Prune is a do not recurse at any directory switch.
From the man page
Basically it will not desend into any sub directories.
Take this example:
You have the following directories
If you run
find -name test2
:It will return both directories
If you run
find -name test2 -prune
:It will return only /home/test2 as it will not descend into /home/test2 to find /home/test2/test2
If you read all the good answers here my understanding now is that the following all return the same results:
But the last one will take a lot longer as it still searches out everything in dir1. I guess the real question is how to
-or
out unwanted results without actually searching them.So I guess prune means don't decent past matches but mark it as done...
http://www.gnu.org/software/findutils/manual/html_mono/find.html "This however is not due to the effect of the ‘-prune’ action (which only prevents further descent, it doesn't make sure we ignore that item). Instead, this effect is due to the use of ‘-o’. Since the left hand side of the “or” condition has succeeded for ./src/emacs, it is not necessary to evaluate the right-hand-side (‘-print’) at all for this particular file."
Normally the native way we do things in linux and the way we think is from left to right.
So you would go and write what you are looking for first:
Then you probably hit enter and realize you are getting too many files from directories you wish not to. Let's exclude /media to avoid searching your mounted drives.
You should now just APPEND the following to the previous command:
so the final command is:
...............|<--- Include --->|....................|<---------- Exclude --------->|
I think this structure is much easier and correlates to the right approach
Adding to the advice given in other answers (I have no rep to create replies)...
When combining
-prune
with other expressions, there is a subtle difference in behavior depending on which other expressions are used.@Laurence Gonsalves' example will find the "*.foo" files that aren't under ".snapshot" directories:-
However, this slightly different short-hand will, perhaps inadvertently, also list the
.snapshot
directory (and any nested .snapshot directories):-The reason is (according to the manpage on my system):-
That is, the second example is the equivalent of entering the following, thereby modifying the grouping of terms:-
This has at least been seen on Solaris 5.10. Having used various flavors of *nix for approx 10 years, I've only recently searched for a reason why this occurs.
Beware that -prune does not prevent descending into any directory as some have said. It prevents descending into directories that match the test it's applied to. Perhaps some examples will help (see the bottom for a regex example). Sorry for this being so lengthy.