find grep 联合使用 find rm
1. use exec
<usage>: find . -type f -exec file {} \;
Runs ‘file’ on every file in or below the current directory. Notice that the braces are enclosed in
single quote marks to protect them from interpretation as shell script punctuation. The semicolon
is similarly protected by the use of a backslash, though single quotes could have been used in that
case also.
find . -type f -print -exec grep Gravity {} \;
2. use xargs
find . -type f -print | xargs grep 'Gravity'
但是这个命令要求文件名不能有空格,或则找不到文件
3 exec选项后面跟随着所要执行的命令或脚本,然后是一对儿{ },一个空格和一个\,最后是一个分号。
在/logs目录中查找更改时间在5日以前的文件并删除它们:
$ find logs -type f -mtime +5 -exec rm { } \;
find * -type f -name *~ -exec rm {} \;
find * -type f -name *.bak -exec rm {} \;
find * -type f -name *.bak -name *~ -exec rm {} \;
在下面的例子中, find命令在当前目录中查找所有文件名以.LOG结尾、更改时间在5日以上的文件,并删除它们,只不过在删除之前先给出提示。
$ find . -name "*.conf" -mtime +5 -ok rm { } \;
< rm ... ./conf/httpd.conf > ? n
按y键删除文件,按n键不删除。
4。 find . -name *.c
error: find: paths must precede expression:
Always quote globs in find: find . -name "*.c"
To see why, try tracing the original command and looking at what is actually executed:
strace find . -name "*.c"
show line number in grep:
-n
show file name in grep:
-H
for example:
find . -type f -exec grep -n -H InputBox {} \;

浙公网安备 33010602011771号