Search code examples
unixgrepdirectoryfind

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?


Solution

  • 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"