LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-24-2013, 11:10 AM   #1
rgsurfs
LQ Newbie
 
Registered: May 2013
Posts: 17

Rep: Reputation: Disabled
find command and excluding a directory during the search


I have a network share drive mounted as /netshare1

I would like to perform this search and exclude the /netshare1 directory from the search:

find / -type b -o -type c > /temp/device-file-list

any ideas???

Robert
 
Old 10-24-2013, 11:36 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Give this a try:
Code:
find / -path /netshare1 -prune -o -type b -o -type c
 
2 members found this post helpful.
Old 10-24-2013, 11:40 AM   #3
Ygrex
Member
 
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666

Rep: Reputation: 68
Code:
! -path '/netshare1/*'
 
1 members found this post helpful.
Old 10-24-2013, 01:21 PM   #4
rgsurfs
LQ Newbie
 
Registered: May 2013
Posts: 17

Original Poster
Rep: Reputation: Disabled
worked perfect. tks.

I was messing around with the prune command and just didn't have it in the right order.

find / -path '/netshare1/*' -prune -o -type b -o -type c > /temp/test

Robert
 
Old 10-24-2013, 01:29 PM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,130

Rep: Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347
Quote:
Originally Posted by Ygrex View Post
Code:
! -path '/netshare1/*'
The disadvantage of doing it that way is that find will still recurse into the /netshare1 hierarchy even though nothing it finds there will satisfy the expression. If /netshare1 is a huge directory tree accessed over a network link, that could take a while.
 
Old 12-17-2013, 09:54 PM   #6
Lunar
Member
 
Registered: Feb 2005
Location: Texas, USA
Distribution: opensuse
Posts: 96

Rep: Reputation: 3
Thanks! finally got a simple find to work...

Quote:
Originally Posted by druuna View Post
Give this a try:
Code:
find / -path /netshare1 -prune -o -type b -o -type c
I've been trying, for years to get a simple 'search' to work from the root, where i can search for 'system' files without searching the huge $HOME directories..

Your code was the first that was not so complicated, I was able to hack and adapt it to do what I want.

Code:
# find / -path /home -prune -o -name "cron*"
*note: replacing -name "cron*" with what ever I'm looking for; "*.txt"; "*.log"; "*theme*" ... what ever... again, searching the 'system' and not user directories...

still don't know why i need -o (or) and not 'and' if i want it to do both things, exclude directory /home AND 'find' "cron*"... but it works.

Thanks !
Landis.

Last edited by Lunar; 12-17-2013 at 10:36 PM. Reason: distor icon change...11
 
Old 12-18-2013, 06:30 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
@Lunar: I'm not sure if the find command used is what you want or does what you expect it to do....

The above command will print /home (but not any content):
Code:
# find / -path /home -prune -o -name ".bash*"
/root/.bashrc
/root/.bash_history
/home
/etc/skel/.bash_logout
/etc/skel/.bashrc
In essence the command does 2 things (and here is where the -o (OR) comes into play):
1 - Look for /home but don't print any content (-path /home -prune) -> this prints /home
OR
2 - find files/dirs/etc that start with .bash


If you want to completely exclude directories from the search you could use something like this:
Code:
# find / ! \( -name home -prune \) -name ".bash*"
/root/.bashrc
/root/.bash_history
/etc/skel/.bash_logout
/etc/skel/.bashrc
 
2 members found this post helpful.
Old 12-18-2013, 04:12 PM   #8
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,130

Rep: Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347
Quote:
Originally Posted by Lunar View Post
still don't know why i need -o (or) and not 'and' if i want it to do both things, exclude directory /home AND 'find' "cron*"... but it works.
The arguments to find are a logical expression, with each test or action returning either TRUE or FALSE. Evaluation of the expression stops as soon as the final TRUE/FALSE result can be determined. When the implied "-and" operators and "-print" action are included, and redundant parentheses added to show the operator precedence, your expression becomes
Code:
 ( -path /home -and -prune ) -or ( -name '*.bash*' -and -print )
If the match for "/home" fails, then the value of the first term is immediately known to be FALSE and the "-prune" is never evaluated. If you replaced that "-or" operator with "-and", then the value of the whole expression would also be known and none of the rest would be evaluated. In fact, nothing would ever be printed since both "-path /home" and "-name '*.bash*'" would have to succeed in order to reach the "-print" action.
 
1 members found this post helpful.
Old 12-26-2013, 10:13 PM   #9
Lunar
Member
 
Registered: Feb 2005
Location: Texas, USA
Distribution: opensuse
Posts: 96

Rep: Reputation: 3
Thumbs up Thank You.

Quote:
Originally Posted by druuna View Post
@Lunar: I'm not sure if the find command used is what you want or does what you expect it to do....

1 - Look for /home but don't print any content (-path /home -prune) -> this prints /home
...
Thank You... I thought it was taking too long to 'search' the 'system'... So, if i understand you, 'my' find command is still 'searching' /home. It just doesn't display the results of the find.. yes.

Thank you.. I've never gotten the find / ! \ ( ..... stuff to work before, but then i've always attributed the 'file not found' type errors to my using openSuSE... but bash is bash, more or less, right..

So, I very carefully duplicated your example with my search, INCLUDING spaces (which i mostly thought i could ignore, like blah blah | more or ..blah|more, same difference ) and valah, it works and it's much quicker... I thought it printed /home to let me know it was 'empty' (skipped).. lol

Thanks !!!
Landis.
 
Old 12-26-2013, 10:25 PM   #10
Lunar
Member
 
Registered: Feb 2005
Location: Texas, USA
Distribution: opensuse
Posts: 96

Rep: Reputation: 3
Smile Thank You.

Quote:
Originally Posted by rknichols View Post
The arguments to find are a logical expression, with each test or action returning either TRUE or FALSE. ....
In fact, nothing would ever be printed since both "-path /home" and "-name '*.bash*'" would have to succeed in order to reach the "-print" action.
Ah, both can't be 'true'.. It's like a 'double negative', eh..

Thank you, very much for taking the time to explain the 'why'...
Landis.
 
Old 12-27-2013, 03:50 AM   #11
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Quote:
Originally Posted by Lunar View Post
Thank You... I thought it was taking too long to 'search' the 'system'...
Using the find command is rather resource unfriendly, especially if the starting point is the root directory (/). Depending on the search options used it checks all the files and directories when trying to locate the search criteria, which takes time (the bigger the available disk space the longer it will take). The find command does have some intelligence, if you run the same find command twice in a row the second time will be faster. However, find is smart enough to detect any changes that might have occurred in between the runs (don't ask me how that works ).

Quote:
So, if i understand you, 'my' find command is still 'searching' /home. It just doesn't display the results of the find.. yes.
Nope, that is not correct. This -path /home -prune will not descend into any subdirectory in /home

Quote:
Thank you.. I've never gotten the find / ! \ ( ..... stuff to work before, but then i've always attributed the 'file not found' type errors to my using openSuSE... but bash is bash, more or less, right..
Bash _is_ bash. Bash is independent from any distro. The only influence a specific distro can have are the content of the start-up files (/etc/bashrc, /etc/profile, ~/.bashrc etc). These start-up files set the environment (PATH, umask, aliases, colour, history etc, etc) and they do not influence bash's core behaviour.

Quote:
Thanks !!!
Landis.
You're welcome
 
Old 12-27-2013, 09:57 AM   #12
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,130

Rep: Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347
Quote:
Originally Posted by druuna View Post
The find command does have some intelligence, if you run the same find command twice in a row the second time will be faster. However, find is smart enough to detect any changes that might have occurred in between the runs (don't ask me how that works ).
That's independent of the find command itself. On a system with plenty of memory, the kernel will still have a lot of inodes and directory blocks in it's cache, so that second invocation of find won't result in as much physical I/O.
 
1 members found this post helpful.
  


Reply

Tags
directory, exclude, find


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
What is the command to search file and directory at the same time ? techie_san778 Linux - Software 4 03-26-2013 02:08 PM
[SOLVED] search a directory and add file contents when find string j-me Linux - General 3 01-11-2013 09:54 AM
Making find do search in a directory that has whitespace in its pathname xode Linux - Software 2 04-18-2006 11:42 AM
Find command using 2 search conditions nifflerX Linux - General 8 07-15-2005 02:37 PM
pattern search through find command. abhi Linux - Newbie 2 04-11-2005 11:31 AM


All times are GMT -5. The time now is 03:02 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration