通常,查找到文件之后,还会有后续的操作,使用exec可以实现。
示例1、查找到后ll查看一下
find . -type f -exec ls -l {} ;
前面都懂,exec后面带的是执行的命令,{}指exec前查找到的文件,最后以;结尾,\是转义的作用,防止;出现歧义。
示例2、查找到后rm删除
find . -ctime -2 -exec rm -r {} ;
[root@localhost ppp]# ll
total 4
-rw-r--r--. 1 root root 0 Mar 30 13:45 1.doc
-rw-r--r--. 1 root root 0 Mar 30 13:45 1.sh
-rw-r--r--. 1 root root 7 Mar 30 13:46 1.txt
[root@localhost ppp]# find . -name 1.doc -exec rm -i {} \;
rm: remove regular empty file './1.doc'? y
[root@localhost ppp]# ll
total 4
-rw-r--r--. 1 root root 0 Mar 30 13:45 1.sh
-rw-r--r--. 1 root root 7 Mar 30 13:46 1.txt
-exec后可以跟任何命令,可以尝试练习,比如cp mv等等常用命令