Operating System - HP-UX
1831627 Members
3285 Online
110027 Solutions
New Discussion

Re: grep but skip executables

 
SOLVED
Go to solution
Cody Godines_1
Occasional Advisor

grep but skip executables

Hi everyone,
How can I grep a set of files in a directory but ignoring the executables, object, and archives? Also, is there a way to grep a .gz file without having to gunzip it?
Thanks,
-Cody
8 REPLIES 8
Hai Nguyen_1
Honored Contributor

Re: grep but skip executables

Cody,

Go to the target directory and run:

# grep -li *

which will return the list of files having the , ignoring the case-sensitivity.

Hai
Patrick Wallek
Honored Contributor

Re: grep but skip executables

If you are doing something like a 'grep something *' then there is no easy way to skip files.

To look at the contents of a gz file do a 'gzcat filename.gz | grep whateveryouarelookingfor'
James R. Ferguson
Acclaimed Contributor

Re: grep but skip executables

Hi Cody:

You could use the 'file' command on the file to descern whether or not the file is an ASCII text file. If yes, then 'grep' it.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: grep but skip executables

If it were me I would send each file to the file command and grep for "text". You will also exclude binary data files that way but you will include things that are commands text like Perl scripts.

e.g.

If it ain't broke, I can fix that.
Wodisch_1
Honored Contributor
Solution

Re: grep but skip executables

Hi Cody,

so the example I gave to your other thread would have to be expanded with lines for ZIPped files:

find /parent -type f -print |
while read name; do
case "$name" in
*.gz) zcat "$name" | grep 'string' && echo "$name" ;;
*.Z) pcat "$name" | grep 'string' && echo "$name" ;;
*) case "$(file $name)" in
*text*) grep 'string' $name /dev/null ;;
esac ;;
esac
done | more

HTH,
Wodisch
Rick Beldin
HPE Pro

Re: grep but skip executables

How about something like this?

for i in *
do
if [ "`file $i | grep exe`" = "" ]
then
echo "not an exe"
else
echo $i "is an exe"
fi
done

This will catch only binary executables.

You could use something like:

for i in `find . -type f`
blah...

to only search 'real' files and skip links.
Necessary questions: Why? What? How? When?
John Dvorchak
Honored Contributor

Re: grep but skip executables

How about something like this:

find /etc -type f | xargs -l grep string
If it has wheels or a skirt, you can't afford it.
Andrew F Greenwood
Occasional Advisor

Re: grep but skip executables

For the text files I would use:

file * | grep text | cut -f1 -d: | xargs grep [-h|-l] /dev/null

For the .gz file I would use:

gunzip