Pandu
Pandu

Reputation: 199

Omit "Is a directory" results while using find command in Unix

I use the following command to find a string recursively within a directory structure.

find . -exec grep -l samplestring {} \;

But when I run the command within a large directory structure, there will be a long list of

grep: ./xxxx/xxxxx_yy/eee: Is a directory
grep: ./xxxx/xxxxx_yy/eee/local: Is a directory
grep: ./xxxx/xxxxx_yy/eee/lib: Is a directory

I want to omit those above results. And just get the file name with the string displayed. can someone help?

Upvotes: 19

Views: 12501

Answers (3)

HoldOffHunger
HoldOffHunger

Reputation: 20881

Exclude directory warnings in grep with the --exclude-dir option:

grep --exclude-dir=* 'search-term' *

Just look at the grep --help page:

--exclude-dir=PATTERN directories that match PATTERN will be skipped.

Upvotes: 1

stevesliva
stevesliva

Reputation: 5655

grep -s or grep --no-messages

It is worth reading the portability notes in the GNU grep documentation if you are hoping to use this code multiple places, though:

-s --no-messages Suppress error messages about nonexistent or unreadable files. Portability note: unlike GNU grep, 7th Edition Unix grep did not conform to POSIX, because it lacked -q and its -s option behaved like GNU grep’s -q option.1 USG-style grep also lacked -q but its -s option behaved like GNU grep’s. Portable shell scripts should avoid both -q and -s and should redirect standard and error output to /dev/null instead. (-s is specified by POSIX.)

Upvotes: 21

fedorqui
fedorqui

Reputation: 289625

Whenever you are saying find ., the utility is going to return all the elements within your current directory structure: files, directories, links...

If you just want to find files, just say so!

find . -type f -exec grep -l samplestring {} \;
#      ^^^^^^^

However, you may want to find all files containing a string saying:

grep -lR "samplestring"

Upvotes: 13

Related Questions