在Linux中的最近修改的文件中查找包含字符串的文件

发布于 2025-01-24 12:53:01 字数 199 浏览 2 评论 0原文

我试图找到哪个文件包含我正在使用的服务器中的特定字符串。我尝试使用grep -rnw,但是服务器很巨大,可能需要几天的时间才能完成。 我不知道文件的位置或命名方式。我唯一知道的是几乎每天都会修改它(如果有的话,不知道确切的时间或频率)。

一种方法是列出过去2天内已修改的系统的每个文件,并将GREP搜索应用于这些文件,但我不知道该如何实现。

I am trying to find which file contains a specific string in a server I'm working on. I tried with grep -rnw but the server is immense and it would take probably days to complete.
I have no idea of where the file can be located nor how it is named. The only thing I know is that it is modified nearly every day (don't know the exact time or the frequency if there is any).

An approach would be to list every files of the system that have been modified within the past 2 days, and apply the grep search to these files, but I don't know how to achieve this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

骄兵必败 2025-01-31 12:53:01

您确实可以使用查找< / code>来过滤最近修改的文件,而仅对这些文件进行过滤:

find / -type f -mtime -2 -exec grep -h -n“您的搜索查询”'{ }'\;

  • -type f仅匹配常规文件
  • -mtime -2匹配过去2天修改的文件(也许会增加此信息,以防万一)
  • -exec 的所有匹配中执行以下命令
  • 匹配的行号

You can indeed use find to filter for recently modified files and only grep over those:

find / -type f -mtime -2 -exec grep -H -n "your search query" '{}' \;

  • -type f only matches regular files
  • -mtime -2 matches files modified in the last 2 days (maybe increase this just in case)
  • -exec executes the following command on all matches of find
  • -H tells grep to print the filenames alongside the matching line
  • -n prints the matched line number
对你的占有欲 2025-01-31 12:53:01

到目前为止,您的其他答案都建议使用查找's -exec功能,以运行已确定的每个候选文件的grep命令。这是可行的,但是启动数百或数千个单独的grep命令是昂贵的。将与XARGS组合将的组合起来更有效,将单独的grep命令的数量减少到最低:

find / -type f -mtime -2 -print0 |
  xargs -r0 grep -Fnw 'search string'

xargs将从其标准输入中读取的文件名为“ grep”命令的形式参数列表开始,从而大大减少了单独的grep命令的数量。

还请注意:

  • 示例命令使用GNU 提供的扩展名和gnu XARGS。从示例命令中删除两个0 s可以解决该问题,但请您访问涉及包含newlines的文件名的问题。
  • 如图所示,-f选项将使grep在您描述的情况下,搜索词是固定字符串的情况。它还将保护您免受搜索词的可能性,如果它包含任何正则元视频器。
  • 查找可以使用各种其他信息,以更具选择性,以将哪些文件传递给GREP,如果您可以筹集任何此类详细信息。例如,如果您可以确定哪个用户将拥有该文件或有关其模式的任何内容(权限),或者在文件大小上的下限或上限。另外,如果您可以将搜索限制在整个文件系统的范围内,那么当然也可以改善经过的时间。
  • 对于一个大的文件系统,即使在不阅读其任何内容的情况下,也只需遍历所有文件,都将花费很长时间。

Your other answers so far all suggest using find's -exec feature to run a grep command for each candidate file identified. That's viable, but launching hundreds or thousands of separate grep commands would be costly. It would be more efficient to combine find with xargs to reduce the number of separate grep commands to a minimum:

find / -type f -mtime -2 -print0 |
  xargs -r0 grep -Fnw 'search string'

xargs will group the file names read from its standard input to form argument lists for grep commands starting with the given words, yielding a huge reduction in the number of separate grep commands.

Note also that:

  • The example command uses extensions provided by GNU find and GNU xargs. Removing the two 0s from the example command would fix that, but leave you open to issues involving file names containing newlines.
  • The -F option, as shown, will make grep slightly more efficient for the case you describe, where the search term is a fixed string. It will also protect you against the possibility of the search term being misinterpreted in the event that it contains any regex metacharacters.
  • find can use all sorts of additional information to be more selective about which files are passed on to grep, if you can glean any such details. For example, if you can determine what user will own the file, or anything about its mode (permissions), or a lower or upper bound on the file size. Also, if you can limit the search to less than the whole filesystem then of course that will improve the elapsed time, too.
  • For a large filesystem, it will take a fairly long time, no matter what you do, just to traverse all the files, even without reading any of their contents.
靖瑶 2025-01-31 12:53:01
find ./ -mtime -2 -exec grep "ABC" {} /dev/null \; -ls

一些说明:

  • -mtime -2:获取最近两天已修改的文件(您可以根据需要修改值
  • grep“ abc”:字符串您正在寻找
  • -exec do_something {} \;:这是通过搜索结果执行“ do_something”的方法(称为{}
  • - ls:将其添加到您的查找命令中,可以根据您的意愿省略该文件。
find ./ -mtime -2 -exec grep "ABC" {} /dev/null \; -ls

Some explanation:

  • -mtime -2 : take the files, which have been modified the last two days (you might modify the value as you wish
  • grep "ABC": the string you're looking for
  • -exec do_something {} \; : this is the way to execute "do_something" with your search result (known as {})
  • -ls : adding this to your find command gives the complete information on the file you have found. This can be omitted as you wish
紫轩蝶泪 2025-01-31 12:53:01

尝试使用:

find / -mtime 0 -exec grep "some string" "{}" \\;

Try using:

find / -mtime 0 -exec grep "some string" "{}" \\;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文