寻找在命令行上搜索文件中文本的工具

发布于 2024-11-03 07:01:03 字数 171 浏览 2 评论 0原文

你好 我正在寻找一些在文件 ex 中使用关键字或模式搜索的脚本或程序。 php、html 等并显示该文件在哪里

我使用命令 cat /home/* | grep“关键字” 但我有太多文件夹和文件,这个命令会导致正常运行时间过长:/

我需要这个脚本来查找假网站(paypal、ebay 等)

Hello
I'm looking some script or program that use keywords or pattern search in files ex. php, html, etc and show where is this file

I use command cat /home/* | grep "keyword"
but i have too many folders and files and this command causes big uptime :/

I need this script to find fake websites (paypal, ebay, etc)

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

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

发布评论

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

评论(2

一梦等七年七年为一梦 2024-11-10 07:01:03
find /home -exec grep -s "keyword" {} \; -print
find /home -exec grep -s "keyword" {} \; -print
疯狂的代价 2024-11-10 07:01:03

你并没有真正说出你正在使用什么操作系统(和外壳)。您可能需要重新标记您的问题以帮助我们解决问题。

因为你提到了cat | ... ,我假设您使用的是 Unix/Linux 变体,因此这里有一些查看文件的指示。 (bmargulies 解决方案也很好)。

我正在寻找一些在文件中使用关键字或模式搜索的脚本或程序

grep 是在文件中搜索文本字符串的基本程序。它的用法是

grep [-options] 'search target' file1 file2 .... filen

(请注意,“搜索目标”包含空格,如果您没有用双引号或单引号将 searchTarget 中的空格括起来,则调试时将出现一个小错误。)

(另请注意,“搜索目标”可以使用各种通配符,例如 .,?,+,,. 等等,这超出了您的问题范围)。 ...无论如何...

正如我猜您已经发现的那样,即使使用通配符文件名扩展,您一次也只能将这么多文件塞入命令行中。 Unix/linux 几乎总是有一个实用程序可以帮助解决这一问题,

startDir=/home
find ${startDir} -print | xargs grep -l 'Search Target'

有人会很乐意提醒您,如果您的文件名包含空格字符或换行符,则需要进一步增强。


根据您使用的操作系统的不同,grep 的可用选项可能会有很大差异。如果幸运的话,您可以输入以下内容来获取本地 grep 的手册页。

man grep

如果您没有设置大尺寸的页面缓冲区,则可能需要这样做

man grep | page

才能看到“文档”的顶部。按任意键可前进到下一页,当您到达文档末尾时,按最后一个键将返回到命令提示符。

大多数 grep 具有的一些可能对您有用的选项

-i (ignore case)
-l (list filenames only (where txt is found)

还有 fgrep,通常被解释为“文件”grep
因为你可以给它一个要扫描的搜索目标文件,并且使用起来就像

fgrep [-other_options] -f srchTargetsFile file1 file2 ... filen

我需要这个脚本来查找虚假网站(paypal、ebay 等)

最终解决方案

你可以制作一个 srchFile,如

paypal.fake.com
eBay.fake.com
etc.fake.com

然后结合上面的内容,运行以下命令

startDir=/home
find ${startDir} -print | xargs fgrep -il -f srchFile 

一些 grep 要求 -fsrchFile 一起运行。

现在您正在查找以 /home 开头的所有文件,并使用 fgrep 在所有文件中搜索 paypay、ebay 等。 -l 表示它只会打印找到匹配项的文件名。您可以删除 -l,然后您将看到找到的内容的输出,前面带有文件名。

IHTH。

You don't really say what OS (and shell) you are using. You might want to retag your question to help us out.

Because you mention cat | ... , I am assuming you are using a Unix/Linux variant, so here are some pointers for looking at files. (bmargulies solution is good too).

I'm looking some script or program that use keywords or pattern search in files

grep is the basic program for searching files for text strings. Its usage is

grep [-options] 'search target' file1 file2 .... filen

(Note that 'search target' contains a space, if you don't surround spaces in your searchTarget with double or single quotes, you will have a minor error to debug.)

(Also note that 'search target' can use a wide range of wild-card characters, like .,?,+,,., and many more, that is beyond the scope of your question). ... anyway ...

As I guess you have discovered, you can only cram so many files at a time into the comand-line, even when using wild-card filename expansion. Unix/linux almost always have a utiltiyt that can help with that,

startDir=/home
find ${startDir} -print | xargs grep -l 'Search Target'

This, as one person will be happy to remind you, will require further enhancements if your filenames contain whitespace characters or newlines.


The options available for grep can vary wildly based on which OS you are using. If you're lucky, you type the following to get the man page for your local grep.

man grep

If you don't have your page buffer setup for a large size, you might need to do

man grep | page

so you can see the top of the 'document'. Press any key to advance to the next page and when you are at the end of the document, the last key press returns you to the command prompt.

Some options that most greps have that might be useful to you are

-i (ignore case)
-l (list filenames only (where txt is found)

There is also fgrep, which is usually interpretted to mean 'file' grep
becuase you can give it a file of search targets to scan for, and is used like

fgrep [-other_options] -f srchTargetsFile file1 file2 ... filen

I need this script to find fake websites (paypal, ebay, etc)

Final solution

you can make a srchFile like

paypal.fake.com
ebay.fake.com
etc.fake.com

and then combined with above, run the following

startDir=/home
find ${startDir} -print | xargs fgrep -il -f srchFile 

Some greps require that the -fsrchFile be run together.

Now you are finding all files starting /home, searching with fgrep for paypay, ebay, etc in all files. The -l says it will ONLY print the filename where a match is found. You can remove the -l and then you will see the output of what is found, prepended with the filename.

IHTH.

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