LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   find command and excluding a directory during the search (https://www.linuxquestions.org/questions/linux-newbie-8/find-command-and-excluding-a-directory-during-the-search-4175482013/)

rgsurfs 10-24-2013 10:10 AM

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

druuna 10-24-2013 10:36 AM

Give this a try:
Code:

find / -path /netshare1 -prune -o -type b -o -type c

Ygrex 10-24-2013 10:40 AM

Code:

! -path '/netshare1/*'

rgsurfs 10-24-2013 12:21 PM

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

rknichols 10-24-2013 12:29 PM

Quote:

Originally Posted by Ygrex (Post 5051559)
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.

Lunar 12-17-2013 08:54 PM

Thanks! finally got a simple find to work...
 
Quote:

Originally Posted by druuna (Post 5051555)
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.

druuna 12-18-2013 05:30 AM

@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


rknichols 12-18-2013 03:12 PM

Quote:

Originally Posted by Lunar (Post 5082888)
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.

Lunar 12-26-2013 09:13 PM

Thank You.
 
Quote:

Originally Posted by druuna (Post 5083159)
@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.

Lunar 12-26-2013 09:25 PM

Thank You.
 
Quote:

Originally Posted by rknichols (Post 5083460)
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.

druuna 12-27-2013 02:50 AM

Quote:

Originally Posted by Lunar (Post 5087396)
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 ;)

rknichols 12-27-2013 08:57 AM

Quote:

Originally Posted by druuna (Post 5087494)
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.


All times are GMT -5. The time now is 03:14 PM.