Bash command to search for any occurrence of phrase and return list of files and paths

蓝咒 提交于 2019-12-06 22:12:39
ezod
{ find . -name '*my key phrase*' ;
  grep -rl 'my key phrase' .     ;
} | sort -u > mylist.txt

You're almost there, just specify you want to match file names and add wildcards to your pattern:

find -name '*my key phrase*' > mylist.txt

To search within the contents of files, use the grep command (with -r recursive option, or rgrep):

rgrep -l 'my key phrase' >> mylist.txt

one command to do it all (using xargs and bash)

find | xargs -I {} bash -c '(([[ -f "{}" ]] && grep -l "my key phrase" "{}") || ([[ "{}" =~ "my key phrase" ]] && echo {}))'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!