![如何将 find 命令(使用 grep)的输出重定向到日志文件?](https://linux22.com/image/31874/%E5%A6%82%E4%BD%95%E5%B0%86%20find%20%E5%91%BD%E4%BB%A4%EF%BC%88%E4%BD%BF%E7%94%A8%20grep%EF%BC%89%E7%9A%84%E8%BE%93%E5%87%BA%E9%87%8D%E5%AE%9A%E5%90%91%E5%88%B0%E6%97%A5%E5%BF%97%E6%96%87%E4%BB%B6%EF%BC%9F.png)
考虑搜索包含模式“搜索字符串”的所有文件的代码:
bash-3.2$ # The below find works fine..
bash-3.2$ find . -type f -exec grep -il "search string" {} \;
bash-3.2$ # But I am unable to redirect output to a log file..
bash-3.2$ find . -type f -exec grep -il "search string" {} \ > log.txt
find: incomplete statement
bash-3.2$
来自 Solaris 的手册页寻找:
-exec command True if the executed command returns a zero value as exit status. The end of command must be punctuated by an escaped semicolon. A command argument {} is replaced by the current path name.
所以看来转义分号是强制性的。还有其他方法可以解决这个问题吗?
答案1
您正在删除\;
.只需这样做:
find . -type f -exec grep -il "search string" {} \; > log.txt