Mark the "Is the directory" results when using the find command on Unix

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

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

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

 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 displayed string. can someone help?

+5
source share
2 answers

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

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

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

However, you may need to find all the files containing the string:

 grep -lR "samplestring" 
+3
source

grep -s or grep --no-messages

The GNU grep documentation should be read about portability notes if you are hoping to use this code in several places:

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

+3
source

Source: https://habr.com/ru/post/1265138/


All Articles