linux - 在子目录中递归搜索特定字符串的简单 Bash 脚本

标签 linux bash

我最近开始学习 linux,因为接下来几个月就要举办 ctf 比赛了。我遇到的问题是我试图制作一个从目录开始的 bash 脚本,检查内容是目录还是其他类型的文件。如果它是一个文件,图像等应用 strings $f | grep -i 'abcdef',如果它是目录 cd 到该目录并重新开始。我有 C++ 经验,我理解其中的逻辑,但我无法真正让它发挥作用。我无法成功实现遍历所有子目录的循环。我们将不胜感激!

最佳答案

这个实现不需要循环。 find命令可以做你正在寻找的事情。 例如:

 find /home -type f -exec sh -c " strings {} | grep abcd " \;

解释:

/home 是你的基本目录,可以是任何东西

-type f:表示普通文件

-exec 来自手册页:

"Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of ;' is encountered. The string {}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find. Both of these constructions might need to be escaped (with a `') or quoted to protect them from expansion by the shell. See the EXAMPLES section for examples of the use of the -exec option. The specified command is run once for each matched file. The command is executed in the starting directory. There are unavoidable security problems surrounding use of the -exec action; you should use the -execdir option instead."

关于linux - 在子目录中递归搜索特定字符串的简单 Bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72067653/

相关文章:

linux - 为什么我不能使用 'file://' 协议(protocol)将参数传递给闪存?

检查IPC消息队列是否已经存在而不创建它

bash - 在 bash 中转义斜杠 (awk)

linux - 从文件中提取文本并将内容附加到 unix 中每行的末尾

linux - BASH 脚本如何在不使用 sudoers nopasswd 选项的情况下自动提升到远程服务器上的根目录?

linux - 在 bash 脚本中执行 cut 命令

Linux Bash 'read' 和使用 -n nchars 处理换行符

c - tee 总是返回 EINVAL

bash - Sed/Awk 删除第二次出现的字符串 - 平台无关

node.js - 仅当从 node.js 运行时,Shell 脚本应在读取输入时挂起