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

有些话、适合烂在心里 提交于 2019-12-10 12:17:15

问题


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' *


来源:https://stackoverflow.com/questions/8144501/grep-l-is-not-working-for-a-folder-with-sub-folders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!