find使用介绍:

1.查找到文件并且拷贝到其他目录下:

find ./ -name "biao-frame.sh" -exec cp {} /tmp \;

find ./ -name "[1-9].txt" | cpio -pdv /opt/

2.查找到文件并且mv移动文件到/opt目录下:

[root@VM_82_178_centos ~]#  touch {1..5}.txt
[root@VM_82_178_centos ~]# ll *.txt
-rw-r--r-- 1 root root  0 Jul 31 14:32 1.txt
-rw-r--r-- 1 root root  0 Jul 31 14:32 2.txt
-rw-r--r-- 1 root root  0 Jul 31 14:32 3.txt
-rw-r--r-- 1 root root  0 Jul 31 14:32 4.txt
-rw-r--r-- 1 root root  0 Jul 31 14:32 5.txt
-rw-r--r-- 1 root root 17 Sep 27  2018 test.txt
[root@VM_82_178_centos ~]# find ./ -name "[1-9].txt" |xargs -i mv {} /opt/
[root@VM_82_178_centos ~]# ll /opt/*.txt
-rw-r--r-- 1 root root 0 Jul 31 14:32 /opt/1.txt
-rw-r--r-- 1 root root 0 Jul 31 14:32 /opt/2.txt
-rw-r--r-- 1 root root 0 Jul 31 14:32 /opt/3.txt
-rw-r--r-- 1 root root 0 Jul 31 14:32 /opt/4.txt
-rw-r--r-- 1 root root 0 Jul 31 14:32 /opt/5.txt

-p参数会提示让你确认是否执行后面的命令,y执行,n不执行

[root@VM_82_178_centos opt]# find ./ -name "[1-9].txt" |xargs -p -i mv {} /root/
mv ./4.txt /root/ ?...y
mv ./5.txt /root/ ?...y
mv ./6.txt /root/ ?...y
mv ./3.txt /root/ ?...y
mv ./2.txt /root/ ?...y
mv ./1.txt /root/ ?...y

[root@VM_82_178_centos opt]# ll /root/*.txt
-rw-r--r-- 1 root root  0 Jul 31 14:32 /root/1.txt
-rw-r--r-- 1 root root  0 Jul 31 14:32 /root/2.txt
-rw-r--r-- 1 root root  0 Jul 31 14:32 /root/3.txt
-rw-r--r-- 1 root root  0 Jul 31 14:32 /root/4.txt
-rw-r--r-- 1 root root  0 Jul 31 14:32 /root/5.txt
-rw-r--r-- 1 root root  4 Jul 31 13:54 /root/6.txt

*3.查找目录下50天前的文件,并且排除掉xx.conf nginx_status.conf 文件和 /usr/local/nginx/conf/vhost/bak/ 下面的文件,然后删除掉**

find /usr/local/nginx/conf/vhost -type f -mtime +50 -name "*" ! -path "/usr/local/nginx/conf/vhost/xx.conf" ! -path "/usr/local/nginx/conf/vhost/nginx_status.conf"  ! -path "/usr/local/nginx/conf/vhost/bak/*" -exec rm -f {} \;

4.查找到文件并批量修改文件内容:

[root@VM_82_178_centos ~]# cat 1.sh 
12345
22222
33333
00000
[root@VM_82_178_centos ~]# find ./ -name "1.sh" -exec sed -i s/0/9/g {} \;
[root@VM_82_178_centos ~]# less 1.sh 
12345
22222
33333
99999

5.查大于512k的文件

find /home -size +512k

6.查找大小为0的文件并且删除

[root@VM_82_178_centos ~]# find ./ -name "[1-9].txt" -type f  -size 0  -exec rm -f {} \;
./3.txt
./2.txt
./1.txt

7.查到txt文件,放到一行,然后删除

 find ./ -name "[1-9]".txt -type f -print0 |xargs -0 rm -f
[root@VM_82_178_centos ~]# find ./ -name "[1-9].txt" -type f
./4.txt
./5.txt
./3.txt
./2.txt
./1.txt

[root@VM_82_178_centos ~]# find ./ -name "[1-9].txt" -type f -print
./4.txt
./5.txt
./3.txt
./2.txt
./1.txt
null

把查到的txt文件放到一行,文件名之前没有空格
[root@VM_82_178_centos ~]# find ./ -name "[1-9].txt" -type f -print0
./4.txt./5.txt./3.txt./2.txt./1.txt[root@VM_82_178_centos ~]# 
把查到的txt文件放到一行,文件名之间空格隔开
[root@VM_82_178_centos ~]# find ./ -name "[1-9].txt" -type f -print0|xargs -0
./4.txt ./5.txt ./3.txt ./2.txt ./1.txt

把查到的txt文件放到一行,文件名空格隔开
[root@VM_82_178_centos ~]# find ./ -name "[1-9].txt" -type f|xargs
./4.txt ./5.txt ./3.txt ./2.txt ./1.txt

[root@VM_82_178_centos ~]#  find ./ -name "[1-9].txt" -type f |xargs -n1
./4.txt
./5.txt
./3.txt
./2.txt
./1.txt

查出txt文件,并且每行放5个文件
[root@VM_82_178_centos ~]#  find ./ -name "[1-9].txt" -type f |xargs -n5
./4.txt ./5.txt ./3.txt ./2.txt ./1.txt

查出txt文件,并且每行放2个文件
[root@VM_82_178_centos ~]#  find ./ -name "[1-9].txt" -type f|xargs -n2
./4.txt ./5.txt
./3.txt ./2.txt
./1.txt

把查到的txt文件放到一行,文件名空格隔开,然后删除文件
 find ./ -name "[1-9].txt" -type f -print0|xargs -0 rm -f

8.查看文件格式

[root@VM_82_178_centos ~]#  find ./ -type f -name "[0-9].txt" -exec file '{}' \;
./4.txt: empty
./5.txt: empty
./3.txt: empty
./2.txt: empty
./1.txt: ASCII text

9.用grep命令在当前目录下的txt结尾的文件中搜索AA这个词


[root@VM_82_178_centos ~]# echo AA >>5.txt
[root@VM_82_178_centos ~]#  find ./ -name "[1-9].txt" |xargs grep "AA"
./5.txt:AA

10.find查找出以txt结尾的文件,然后把查找出的txt结尾的文件名称追加到一个文本中

[root@VM_82_178_centos ~]# find ./ -name "[1-9].txt" -print | xargs echo "" >/tmp/core.log
[root@VM_82_178_centos ~]# less /tmp/core.log 
 ./4.txt ./5.txt ./6.txt ./3.txt ./2.txt ./1.txt

11. 参数-l1是一次处理一个,-t是处理之前打印出命令


[root@VM_82_178_centos ~]# find ./ -name "[1-9].txt"  -print0 | xargs -0 -l1 -t -i mv {} /opt/
mv ./4.txt /opt/ 
mv ./5.txt /opt/ 
mv ./6.txt /opt/ 
mv ./3.txt /opt/ 
mv ./2.txt /opt/ 
mv ./1.txt /opt/ 
[root@VM_82_178_centos ~]# ll /opt/*.txt
-rw-r--r-- 1 root root 0 Jul 31 14:32 /opt/1.txt
-rw-r--r-- 1 root root 0 Jul 31 14:32 /opt/2.txt
-rw-r--r-- 1 root root 0 Jul 31 14:32 /opt/3.txt
-rw-r--r-- 1 root root 0 Jul 31 14:32 /opt/4.txt
-rw-r--r-- 1 root root 3 Jul 31 14:40 /opt/5.txt
-rw-r--r-- 1 root root 4 Jul 31 13:54 /opt/6.txt

[root@VM_82_178_centos opt]# find ./ -name "[1-9].txt"  -print0 | xargs -0 -l1 -t rm -f
rm -f ./4.txt 
rm -f ./5.txt 
rm -f ./6.txt 
rm -f ./3.txt 
rm -f ./2.txt 
rm -f ./1.txt 
[root@VM_82_178_centos opt]# ls
 rh  test_2018-11-27.sql

12.遍历一次以txt结尾文件,将权限为664文件和目录列入/root/a.txt,将大于1M的文件列入/root/big.txt


find ./ -name "[0-9].txt" \( -perm  644 -fprintf /opt/a.txt '%#m %u %p\n' \) , \( -size +1M -fprintf /opt/big.txt '%-10s %p\n' \)

[root@VM_82_178_centos opt]# cat a.txt 
0644 root ./4.txt
0644 root ./5.txt
0644 root ./3.txt
0644 www ./2.txt
0644 root ./1.txt
[root@VM_82_178_centos opt]# cat big.txt 
1901997    ./1.txt

**参数-%m指文件的模式,%#m 此处的#号,是补0位**
you will see a difference between the actual value of the file's mode and the output of %m.   Normally you will  want  to
                     have a leading zero on this number, and to do this, you should use the # flag (as in, for example, `%#m').

**参数%u 指文件的用户名,如果用户没有名称,则为数字用户ID**
File's user name, or numeric user ID if the user has no name
**参数%p指文件名**
**参数\n为换行符**

** 参数%s 指文件大小(以字节为单位)**

13.find查找到文件然后删除

find ./ -name nn108cpv-root -delete