Searching for a string in multiple files on Linux [closed]

徘徊边缘 提交于 2019-12-21 14:34:13

问题


How to search in multiple files or folders for a string inside a plaintext file?

For example I need to find the string "foo" in all files in the folder "/home/thisuser/bar/baz/"


回答1:


You need to have read privileges on the files you will be searching in. If you do have them, then simply use

grep -r "foo" /home/thisuser/bar/baz/*

to search in a certain folder or

grep "foo" /home/thisuser/bar/baz/somefile.txt

if you need to search in a specific file, in this case "somefile.txt". Basically the syntax is

grep [options] [searched string] [path]
// -r is an option which states that it will use recursive search

Another useful options are "-n" to show on which line in which file the string is located, "-i" to ignore case, "-s" to suppress some messages like "can't read file" or "not found" and "-I" to ignore binary files.

If you use

grep -rnisI "foo" /home/thisuser/bar/baz/*

you will exactly know where to look.




回答2:


Use

grep -r "foo" /home/thisuser/bar/baz/


来源:https://stackoverflow.com/questions/17231292/searching-for-a-string-in-multiple-files-on-linux

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