find搜索姿势
默认先处理文件夹,再处理文件夹
find [选项] [路径] [查找条件] [处理动作]
find后接命令
find命令强大之处在于
- 可以在查找到内容后,调用命令对查找到的内容进行进一步的处理
查看find帮助文件
- [处理动作]相关内容
actions: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print
-fprint0 FILE -fprint FILE -ls -fls FILE -prune -quit
-exec COMMAND ; -exec COMMAND {} + -ok COMMAND ;
-execdir COMMAND ; -execdir COMMAND {} + -okdir COMMAND ;
按照帮助文件执行报错
[root@C6-56 ~]# find ./ -name 'Video*' -exec rm -rf {}
find: missing argument to `-exec'
[root@C6-56 ~]# find ./ -name 'Video*' -exec rm -rf {} ;
find: missing argument to `-exec'
正确find加命令处理姿势
find -name "*.sh" -ok mv {} /opt \; 问一次移一次
find -name "*.sh" -exec mv {} /opt \;不问直接移
{}花括号相当于find内置变量,代表前边find查找到的结果
一旦有-ok 或者有 -exec 后边就必须有 ;结束,这个是语法要求
实例1:查找当前目录下所有zip文件并删除
[root@C6-56 ~]# find ./ -name '*.zip'
./Videos.tar.zip
[root@C6-56 ~]# find ./ -name '*.zip' -exec rm {}
find: missing argument to `-exec'
[root@C6-56 ~]# find ./ -name '*.zip' -exec rm{}
find: missing argument to `-exec'
[root@C6-56 ~]# find ./ -name '*.zip' -exec rm {}
find: missing argument to `-exec'
[root@C6-56 ~]# find ./ -name '*.zip' -exec rm {} \;
[root@C6-56 ~]# find ./ -name '*.zip'
实例2:查找当前目录下包含123的文件和文件夹并删除
[root@C6-56 ~]# ll 123*
-rw-r--r--. 1 root root 81 Apr 8 13:03 123.txt
-rw-r--r--. 1 root root 53 Apr 8 09:10 123.txt.bak
123:
total 8
-rw-r--r--. 1 root root 7 Apr 8 07:09 123.txt
drwxr-xr-x. 3 root root 4096 Apr 8 07:09 321
[root@C6-56 ~]# find ./ -name '123*'
./123.txt.bak
./123
./123/123.txt
./123/321/123
./123/321/123/123.txt
./123/321/123.txt
./123.txt
[root@C6-56 ~]# find ./ -name '123*' -exec rm -rf {}
find: missing argument to `-exec'
[root@C6-56 ~]# find ./ -name '123*'
./123.txt.bak
./123
./123/123.txt
./123/321/123
./123/321/123/123.txt
./123/321/123.txt
./123.txt
[root@C6-56 ~]# find ./ -name '123*' -exec rm -rf {} ;
find: missing argument to `-exec'
[root@C6-56 ~]# find ./ -name '123*'
./123.txt.bak
./123
./123/123.txt
./123/321/123
./123/321/123/123.txt
./123/321/123.txt
./123.txt
[root@C6-56 ~]# find ./ -name '123*' -exec rm -rf {} \;
find: `./123': No such file or directory
[root@C6-56 ~]# find ./ -name '123*'
[root@C6-56 ~]#