在Linux上搜索多个文件中的字符串 [英] Searching for a string in multiple files on Linux

查看:827
本文介绍了在Linux上搜索多个文件中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在多个文件或文件夹中搜索纯文本文件中的字符串?



例如,我需要在所有文件中查找字符串foo文件夹/ home / thisuser / bar / baz /

解决方案

您需要对要搜索的文件拥有读取权限如果你有他们,那么只需使用

  grep -rfoo/ home / thisuser / bar / baz / * 

可在特定文件夹中搜索或

  grepfoo/home/thisuser/bar/baz/somefile.txt 

如果您需要搜索特定文件,在这种情况下是somefile.txt。
基本上,语法是:

$ pre $ grep [options] [搜索字符串] [路径]
// - r是一个选项,它表示它将使用递归搜索

另一个有用的选项是-n显示字符串所在文件的哪一行,-i忽略大小写,-s可以禁止某些消息,如无法读取文件或未找到和-I忽略二进制文件



如果您使用

  grep -rnisIfoo/ home / thisuser / bar / baz / * 

您完全知道去哪里看。


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/"

解决方案

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.

这篇关于在Linux上搜索多个文件中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆