'grep -l' is not working for a folder with

2019-06-10 05:40发布

问题:

I am trying to search for file contents. The file is in a directory with sub folders.

The command

grep -l 'keyword'

is working and is finding all files which are under a folder. If I am searching in a folder with subfolders, I am not getting any results.

Is there any way to search file contents using a directory level search?

回答1:

You need to do something like this:

find . -type f -exec grep -l 'keyword' {} \;

You can also limit the find with the -name option and give it a shell regular expression, for example, '*.txt'.

Where '.' is the current folder you're in -- you can also give any directory path instead of '.' to search a given folder.

See: man find



回答2:

This usage grep each file under your current directory (including subdirectories).

find | xargs grep -l 'keyword'

Is that what you're looking for?



回答3:

find ./folder -type f -exec grep -l 'keyword' {} \;

Where ./folder is the folder you want to search



回答4:

You can use:

grep -rl 'keyword' *