ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I would like to use find(1) "-path" or "-ipath" option in one of my shell scripts. As I understand, if I specify for example '-not -path "*empt*"', then all those directories, which have "*empt*" string in their path, are not searched:
Code:
[martin@martin ~]$ ls doc
[martin@martin ~]$ find ./a ./an -type f
./an/empty/one/testfile
./an/file
[martin@martin ~]$ find ./a ./an -type f -not -path "*empt*" -exec cp {} doc/ \;
[martin@martin ~]$ ls doc
file
[martint@martin ~]$
In other words "-path" or "-not -path" is like grepping the search results before handing those over to action(-print by default). Am I correct?
You might want to check out the -prune option, from the man page:
Code:
-path pattern
File name matches shell pattern pattern. The metacharacters do not treat `/' or `.' specially; so, for example,
find . -path "./sr*sc"
will print an entry for a directory called `./src/misc' (if one exists). To ignore a whole directory tree, use -prune rather than checking every file in the tree. For example, to
skip the directory `src/emacs' and all files and directories under it, and print the names of the other files found, do something like this:
find . -path ./src/emacs -prune -o -print
Note that the pattern match test applies to the whole file name, starting from one of the start points named on the command line. It would only make sense to use an absolute path
name here if the relevant start point is also an absolute path. This means that this command will never match anything:
find bar -path /foo/bar/myfile -print
The predicate -path is also supported by HP-UX find and will be in a forthcoming version of the POSIX standard.
You might want to check out the -prune option, from the man page:
Code:
-path pattern
File name matches shell pattern pattern. The metacharacters do not treat `/' or `.' specially; so, for example,
find . -path "./sr*sc"
will print an entry for a directory called `./src/misc' (if one exists). To ignore a whole directory tree, use -prune rather than checking every file in the tree. For example, to
skip the directory `src/emacs' and all files and directories under it, and print the names of the other files found, do something like this:
find . -path ./src/emacs -prune -o -print
Note that the pattern match test applies to the whole file name, starting from one of the start points named on the command line. It would only make sense to use an absolute path
name here if the relevant start point is also an absolute path. This means that this command will never match anything:
find bar -path /foo/bar/myfile -print
The predicate -path is also supported by HP-UX find and will be in a forthcoming version of the POSIX standard.
ok, so all the directories in the directory list(./a and ./an in this case) are still searched through, but before handing results to action(-print by default) the results are "grepped" using the string specified with the "-path" or "-not -path"? If yes, one might use "-not -path <pattern>" as well in order to avoid handing results over to action(for example -print or -exec)? I mean it will take a lot longer than using "-prune" because all the directories are still searched through, but the end results should be the same?
Well I guess the proof is in the pudding ... that is to say, if you run both options of your find command and return the same data then I guess it is up to you which to use, however,
if one is not returning the desired results then there is only one choice in that occasion.
Well I guess the proof is in the pudding ... that is to say, if you run both options of your find command and return the same data then I guess it is up to you which to use, however,
if one is not returning the desired results then there is only one choice in that occasion.
ok..but in which case one should use -path? And in which case one should use -prune?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.