Linux 中的查找与搜索相结合以返回特定行
我试图从该搜索中找到的文件中返回特定行:
find . -name "database.php"
每个文件都包含一个数据库名称,旁边是一个 php 变量,例如 $dname=
我一直在尝试使用 -exec对此文件执行 grep 搜索但没有成功
-exec "grep {\}\ dbname"
任何人都可以让我了解如何完成此任务吗?
我运行的是 CentOS 5,我的服务器上的子目录中存储了大约 100 个 database.php 文件。
谢谢杰森
I'm trying to return a particular line from files found from this search:
find . -name "database.php"
Each of these files contains a database name, next to a php variable like $dname=
I've been trying to use -exec to execute a grep search on this file with no success
-exec "grep {\}\ dbname"
Can anyone provide me with some understanding of how to accomplish this task?
I'm running CentOS 5, and there are about 100 database.php files stored in subdirectories on my server.
Thanks
Jason
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您将
grep
的参数反转,并且需要将它们作为单独的参数:/dev/null
的存在确保匹配的文件名被列为以及匹配的行。You have the arguments to
grep
inverted, and you need them as separate arguments:The presence of
/dev/null
ensures that the file name(s) that match are listed as well as the lines that match.我想这样就可以了。不确定您是否需要对 CentOS 进行任何调整。
I think this will do it. Not sure if you need to make any adjustments for CentOS.
我使用 xargs 解决了这个问题,
如果您找到一个更好的方法,请随时发布更好的方法(或者一些代表以获得一个好的答案)
I worked it out using xargs
Feel free to post a better way if you find one (or what some rep for a good answer)
我倾向于习惯性地避免查找,因为我从未学会如何正确使用它,所以我完成任务的方式是:
编辑:此命令并非在所有情况下都可行,因为它可能会生成很长的内容参数列表,而 find 则像 xargs 一样对找到的文件一一执行其命令。而且,正如我在评论中指出的那样,它可能不太便携。但它太短了;)
I tend to habitually avoid find because I've never learned how to use it properly, so the way I'd accomplish your task would be:
Edit: This command won't be viable in all cases because it can potentially generate a very long argument list, whereas find executes its command on found files one by one like xargs. And, as I noted in my comment, it's possibly not very portable. But it's damn short ;)