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

2019-06-10 05:18发布

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?

4条回答
迷人小祖宗
2楼-- · 2019-06-10 05:33

You can use:

grep -rl 'keyword' *
查看更多
甜甜的少女心
3楼-- · 2019-06-10 05:41

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

查看更多
疯言疯语
4楼-- · 2019-06-10 05:58

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

find | xargs grep -l 'keyword'

Is that what you're looking for?

查看更多
再贱就再见
5楼-- · 2019-06-10 05:58
find ./folder -type f -exec grep -l 'keyword' {} \;

Where ./folder is the folder you want to search

查看更多
登录 后发表回答