Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 335
Pandu Avatar asked Mar 07 '17 14:03

Pandu


2 Answers

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.)

like image 130
stevesliva Avatar answered Oct 28 '22 00:10

stevesliva


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"
like image 40
fedorqui 'SO stop harming' Avatar answered Oct 28 '22 00:10

fedorqui 'SO stop harming'