bash linux + 在目录下的文件中查找单词但很快

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2965050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:09:29  来源:igfitidea点击:

linux + find word in file under directory but quickly

linuxbash

提问by yael

I have the following command

我有以下命令

find /var  -type f -exec grep "param1" {} \; -print

With this command I can find the param1 string in any file under /var

使用此命令,我可以在 /var 下的任何文件中找到 param1 字符串

but the time that it take for this is very long.

但这需要很长时间。

I need other possibility to find string in file but much more faster then my example

我需要在文件中找到字符串的其他可能性,但比我的例子要快得多

THX

谢谢

yael

耶尔

回答by Bella

grep -r "string"

The find is not neccesary.

发现不是必需的。

This is a good link, though outdated.

这是一个很好的链接,虽然已经过时。

link text

链接文字

Also i think this belongs in superuser.com

我也认为这属于 superuser.com

回答by yabt

Take a look at the -l option to the grep command for a speed boost. To speed up the find command use:

查看 grep 命令的 -l 选项以提高速度。要加速 find 命令,请使用:

find ... -exec sh -c '...' arg0 '{}' +


# grep ... -l: print files with matches, but stop scanning the file on the first match
grep -lsr "param1" /var

find /var -type f -exec sh -c 'grep -ls "param1" "$@"' arg0 '{}' +
find /var -type f -exec sh -c 'grep -ls "
grep "param1" $(locate -r '^/var')
" "$@"' "param1" '{}' +

回答by krico

You can use locate's index (if you don't depend on files that are added/removed)

您可以使用 locate 的索引(如果您不依赖于添加/删除的文件)

find /var -type f  | xargs grep "param1" 

回答by Noufal Ibrahim

grep -r "param1" /var 

would be slightly faster (no process spawning for each file)

会稍微快一点(每个文件都不会产生进程)

##代码##

would be slightly more so I think.

所以我想会稍微多一点。

回答by UncleZeiv

Try also using ack, which is "better than grep" in most cases. Among its features the ability to ignore typical garbage files by default (such as .svn or .git directories, core dumps, backup files), the ability to use a large set of predefined file classes, nice output formatting.

也尝试使用 ack,它在大多数情况下“比 grep 好”。它的特点之一是能够默认忽略典型的垃圾文件(例如 .svn 或 .git 目录、核心转储、备份文件)、使用大量预定义文件类的能力、良好的输出格式。

回答by the wanderer

some of these command optimizations are helpful, but the biggest jump in speed I got from grepping 2 million files was to use a SSD Hard drive. Same queries took 1/5 of the time.

其中一些命令优化很有帮助,但我从 grep 200 万个文件中获得的最大速度飞跃是使用 SSD 硬盘。相同的查询花费了 1/5 的时间。