LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-12-2012, 06:49 AM   #1
m4rtin
Member
 
Registered: Sep 2007
Posts: 261

Rep: Reputation: 16
Question find(1) and "-not -path <pattern>"


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?
 
Old 03-12-2012, 08:25 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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.
 
Old 03-12-2012, 11:39 AM   #3
m4rtin
Member
 
Registered: Sep 2007
Posts: 261

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by grail View Post
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?
 
Old 03-12-2012, 12:44 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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.
 
Old 03-12-2012, 07:36 PM   #5
m4rtin
Member
 
Registered: Sep 2007
Posts: 261

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by grail View Post
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?
 
Old 03-13-2012, 12:20 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I think if you look at the man page for find you will see that -prune is used with other options to perform its task, ie:
Code:
find -prune
Alone the above will not be of any great help (that I am aware of)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash, find : How to avoid [...] pattern matching in file names expanded from "$var"? Telengard Programming 19 04-23-2011 02:36 AM
How to find the path to the "Mount" executable file in RHEL4? elitt2 Red Hat 4 10-08-2007 10:22 AM
starting vnc couldn't find "xauth" on your PATH. verlossen Linux - Networking 6 08-21-2006 04:57 PM
configure: error: Can't find "javac" in your PATH, when install/compile a program... JungMin Programming 5 12-07-2005 05:04 PM
configure: error: Can't find "javac" in your PATH, when install/compile a program... JungMin Linux - Software 4 12-05-2005 11:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:30 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
Open Source Consulting | Domain Registration