LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-21-2014, 06:41 AM   #1
postcd
Member
 
Registered: Oct 2013
Posts: 532

Rep: Reputation: Disabled
Linux FIND command, exclude several path, where most files?


Hello,

im doing regular search in files contents in around 25 linux file systems (mostly redhat, debian).,

i want to lower load on server by excluding folders which contains only system files and alot of files

Which linux folders contains most files?

I found i can exclude folders like this:

Quote:
find . -type f -name "*searched phrasse*" ! -path "./tmp/*" ! -path "./var/log/*"
the linux will entirelly skip above excluded directories, or it will go into them and search, but only mute listing? thx

i mainly want to relieve disk I/O operations, thats also why i added "/bin/nice -n 19 " before the find command
 
Old 08-21-2014, 08:03 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,939
Blog Entries: 13

Rep: Reputation: 4981Reputation: 4981Reputation: 4981Reputation: 4981Reputation: 4981Reputation: 4981Reputation: 4981Reputation: 4981Reputation: 4981Reputation: 4981Reputation: 4981
This is intriquing and sorry I don't yet have a solution. I'm beginning to wonder if you need to use a script. My first thinking was that find would have an exclude path option; it does not. Actually the first thought was to just try what you did, with a suspicion that it would not work. It does not, and suggest you also try it.

From some top level, I have ./1.txt and then made sub-directories 'a' and 'b' and placed 2.txt and 3.txt in each, and then did a find for all "*.txt" from that top level. A normal find, of course then finds all three of those txt files.

This is what I see in the manpage for find under the section about the -path option:

Code:
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
So as a result, the find for me might be something like the following to exclude the 'a' sub-directory:
Code:
find . -name "*.txt" -path ./a -prune -o -print
The problem is, that doesn't work in fact it so anti-works it's almost manic. What that command does is also finds other files not matching "*.txt" and it also finds files in the 'a' sub-directory.

Seeing as the man page's example did not use a pattern I therefore chose to use their supplied example almost verbatim:
Code:
find . -path ./a -prune -o -print
THAT actually does exclude the sub-directory as specified. The problem I see there is that it won't accept a file pattern match, because as soon as I put in the -name directive, it goes back to finding all files.

Here's the full run:

Code:
~/testcode$ ls -R
.:
2.txt  a  b  myfile.txt  test.c  signed  signed.c  whle.sh

./a:
a.txt

./b:
b.txt
## So that's everything in my current directory, and below

~/testcode$ find . -name "*.txt"
./a/a.txt
./myfile.txt
./b/b.txt
./2.txt
## That's all "*.txt" in my current directory, and below

~/testcode$ find . -path ./a -prune -o -print
.
./of_test.c
./signed
./signed.c
./myfile.txt
./b
./b/b.txt
./whle.sh
./2.txt
## This is their default -prune method, as you can see it excludes ./a

~/testcode$ find . -path ./a -prune -o -print -name "*.txt"
.
./of_test.c
./signed
./signed.c
./myfile.txt
./b
./b/b.txt
./whle.sh
./2.txt
## This still excludes ./a however it doesn't perform file pattern match for "*.txt"

~/testcode$ find . -name "*.txt" -path ./a -prune -o -print
.
./of_test.c
./signed
./a
./a/a.txt
./signed.c
./myfile.txt
./b
./b/b.txt
./whle.sh
./2.txt
## This is actually WORSE, it somehow disables the -prune action and also doesn't limit to "*.txt"
Sorry, this is "halfway" and I'm wondering if there is a real one-line "find" solution that does work.
 
Old 08-21-2014, 09:41 AM   #3
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,525

Rep: Reputation: 2812Reputation: 2812Reputation: 2812Reputation: 2812Reputation: 2812Reputation: 2812Reputation: 2812Reputation: 2812Reputation: 2812Reputation: 2812Reputation: 2812
The syntax of the -prune option to find is somewhat subtle
From 'info find'
Quote:
find . -name '.snapshot' -prune -o \( \! -name '*~' -print0 \)
The first part of the `find' command here identifies files or
directoires named `.snapshot' and tells `find' not to recurse into them
(since they do not need to be copied). The combination `-name
'.snapshot' -prune' yields false for anything that didn't get pruned,
but it is exactly those files we want to copy. Therefore we need to
use an OR (`-o') condition to introduce the rest of our expression.
The remainder of the expression simply arranges for the name of any
file not ending in `~' to be printed.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Exclude dir from find command anaskoara Linux - Newbie 3 02-05-2011 07:07 AM
[SOLVED] 'find' command regex to exclude hidden files? arashi256 Linux - Newbie 3 06-28-2010 09:53 AM
Can we use exclude option in"rm" command to exclude some files/folders? yadav_rk727 Linux - Newbie 1 02-03-2010 11:14 AM
CVS Exclude : Exclude sub directories from check out On Linux from command line shajay12 Linux - Newbie 1 08-03-2009 01:36 AM
How to exclude certain path from find command. UltraSoul Linux - Software 8 05-16-2009 07:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:32 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