Operating System - HP-UX
1832455 Members
3342 Online
110043 Solutions
New Discussion

Is it possible to exclude some file extentions with find?

 
SOLVED
Go to solution
Eric Antunes
Honored Contributor

Is it possible to exclude some file extentions with find?

Hi,

I'm searching for files with some words like , , ,..., using the following command:

find -exec grep -l -e '' -e '' -e '' {} \;|more

But I get lots of log and txt files that doesn't matter. Is it possible to exclude *.log and *.txt files?

Best Regards,

Eric Antunes
Each and every day is a good day to learn.
15 REPLIES 15
Pete Randall
Outstanding Contributor

Re: Is it possible to exclude some file extentions with find?

Wouldn't "grep -v *.txt |grep -v *.log" do what you want?


Pete

Pete
Dave La Mar
Honored Contributor

Re: Is it possible to exclude some file extentions with find?

Eric -
Why not simply grep -v those extensions?

Regards,

dl

"I'm not dumb. I just have a command of thoroughly useless information."
H.Merijn Brand (procura
Honored Contributor
Solution

Re: Is it possible to exclude some file extentions with find?

# find \! -name \*.log -a \! -name \*.txt -exec grep -l -e '' -e '' -e '' {} \;|more


personally, I would not use find at all.

# grep -r -l -e .... |

or perl -MFile::Find

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Devender Khatana
Honored Contributor

Re: Is it possible to exclude some file extentions with find?

Hi,

find -exec grep -l -e '' -e '' -e '' {} \;|grep -v *.log|grep -v *.txt |more


Does it work,

HTH,
Devender
Impossible itself mentions "I m possible"
Dave La Mar
Honored Contributor

Re: Is it possible to exclude some file extentions with find?

Steven types faster than me obviously.
No points on this or the "same answer" reply.

dl
"I'm not dumb. I just have a command of thoroughly useless information."
A. Clay Stephenson
Acclaimed Contributor

Re: Is it possible to exclude some file extentions with find?

Yes,

find \( ! -name '*.txt' -a ! -name '*.log' \)

However, you should really not grep anything except text files because on some UNIX flavors grep will crash on unexpected input (e.g. executables).

I would add a -type f filter to find to only allow regular files and then pass each candidate to a test using the file command piped to grep -i -q "text"

e.g.
file "${FNAME}" | grep -q -i "text"
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "okay to grep"
fi
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Is it possible to exclude some file extentions with find?

You should also note that unlike some OS's, file suffixes (.txt, .log, .dat, .sh) mean absolutely nothing other than a convention which might be respected to UNIX.
If it ain't broke, I can fix that.
Eric Antunes
Honored Contributor

Re: Is it possible to exclude some file extentions with find?

Procura's solution worked fine and also Clay's but with spaces separating the begining and end of all the expression:

$find \( ! -name '*.log' -a ! -name '*.txt' \) -exec grep -l -e ' ...

Dave, I just gave you 5 more points but I assigned 5 first because you didn't post the command, exemplifying...

Many Thanks,

Eric Antunes
Each and every day is a good day to learn.
Eric Antunes
Honored Contributor

Re: Is it possible to exclude some file extentions with find?

I've just verified that Clay's solution had already the spaces but I didn't noticed that before posting my comment. Shame on me... :(

Thanks again,

Eric Antunes
Each and every day is a good day to learn.
Eric Antunes
Honored Contributor

Re: Is it possible to exclude some file extentions with find?

With:

$find \( ! -name '*.log' -a ! -name '*.txt' \) -exec grep -l -e 'VIS' ...

Is it possible to exclude files that contains this:

...
a VISION z
...

And return just those containing this kind of text:

...
a VIS z
...

??





Each and every day is a good day to learn.
A. Clay Stephenson
Acclaimed Contributor

Re: Is it possible to exclude some file extentions with find?

grep -E -l -e 'VIS '
should do the trick.
If it ain't broke, I can fix that.
H.Merijn Brand (procura
Honored Contributor

Re: Is it possible to exclude some file extentions with find?

Time to revert to perl?

# perl -MFile::Find -le'$/=undef;find(sub{/\.(txt|log)$/ and return;@ARGV=($_);<>=~/VIS\b/ and print$File::Find::name},"DIR")'

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Eric Antunes
Honored Contributor

Re: Is it possible to exclude some file extentions with find?

Clay,

I found an easy (I think) way to avoid executables:

... -a ! -path '*/bin/*' -a ...

Eric Antunes
Each and every day is a good day to learn.
James R. Ferguson
Acclaimed Contributor

Re: Is it possible to exclude some file extentions with find?

Hi Eric:

Merely because files are found in a path named 'bin' doesn't guarantee that they are executable binaries. The nomenclature is a suggested standard only, just like dot-extensions are informational only in UNIX.

If you want to test a file for its "magic" you need to use 'file' to determine its type. 'file' attempts to classify files.

See the man pages for 'file(1)' and for 'magic(4) for more information.

Regards!

...JRF...
Eric Antunes
Honored Contributor

Re: Is it possible to exclude some file extentions with find?

Thanks for the info James.

Nevertheless, in my specific situation I'm pretty sure that files in directories like this:

-a ! -path '*/bin/*' -a ! -path '*/doc/*' -a ! -path '*/forms/*' -a ! -path '*/reports/*' -a ! -path '*/log/*' -a ! -path '*/out/*' -a ! -path '*/patch/*'

don't have what I'm looking for: variable definitions, configurations, etc...

Best Regards,

Eric Antunes

Each and every day is a good day to learn.