Linux四剑客:find(查找)、grep(过滤)、sed(替换)、awk(取列)
find语法:
find 目录 -type 文件类型 -name|-size等选项 要查找的文件或目录名 [|xargs 命令或-exec 命令 {} \;]
如果不指定查找的目录,则表示在当前目录中查找
选项
-type 指定查找的文件类型,常用类型f(文件)、d(目录)、l(软连接)
-name 指定文件名
-size 根据大小查找
-mtime 根据修改时间查找。比如查找7天前的文件(也就是这些文件的修改时间到现在已经超过7天,也就是超过7天没人改)
-exec 表示find命令找出的文件或目录交给exec运行
1)案例01:在/etc/下面找出ifcfg-eth0文件
只知晓目录和文件名,然后进行查找
[root@linux-87-01 ~]# find /etc/ -type f -name 'ifcfg-eth0'
/etc/sysconfig/network-scripts/ifcfg-eth0
[root@linux-87-01 ~]#
2)案例02:在/etc/下面找出以.conf结尾文件
[root@linux-87-01 ~]# find /etc/ -type f -name '*.conf'
*:通配符,表示所有内容,任意内容
3)案例03:在/etc/下面查找文件名包含ifcfg的文件
[root@linux-87-01 ~]# find /etc/ -type f -name '*ifcfg*'
/etc/dbus-1/system.d/nm-ifcfg-rh.conf
/etc/sysconfig/network-scripts/ifcfg-lo
/etc/sysconfig/network-scripts/ifcfg-eth0
[root@linux-87-01 ~]#
4)案例04:在系统中,找出文件大小大于100K的文件
[root@linux-87-01 ~]# find / -type f -size +100k
+100k:大于100k
-100k:小于100k
100k:等于100k
+100M:大于100M
注意:find中,size大小,除了k是小写,其他单位是大写。
5)案例05:找出/var/log下面7天之前以.log结尾的文件
7天之前的文件,指的是修改时间是7天前
[root@linux-87-01 ~]# find /var/log/ -type f -name '*.log' -mtime +7

6)案例06:找出/tmp/下以.txt结尾的文件并删除
方法1:find+rm使用反引号
#1、先运行find命令查找出对应的文件
[root@linux-87-01 ~]# find /tmp/ -type f -name '*.txt'
#2、rm删除find命令查找出对应的文件
[root@linux-87-01 ~]# rm -f `find /tmp/ -type f -name '*.txt'`
方法2:find+rm使用管道
[root@linux-87-01 ~]# find /tmp/ -type f -name '*.txt' | rm -f
此方法,会失败。
虽然,find命令找到了文件,但是,数据经过管道传输的时候,只是文字内容,文件传递给了rm,rm却没有识别。
rm、ls命令格式:rm 参数,ls 参数
而管道实际传输的是文字内容,不能成为rm、ls命令的参数。
[root@linux-87-01 ~]# find /tmp/ -type f -name '*.txt' | ls
anaconda-ks.cfg passwd
[root@linux-87-01 ~]#
如何解决?
通过xargs命令,把文字内容转换为命令可以识别的参数。
如下,所示:
[root@linux-87-01 ~]# find /tmp/ -type f -name '*.txt' | xargs ls -l
-rw-r--r--. 1 root root 0 May 5 23:18 /tmp/10.txt
-rw-r--r--. 1 root root 0 May 5 23:18 /tmp/1.txt
-rw-r--r--. 1 root root 0 May 5 23:18 /tmp/2.txt
-rw-r--r--. 1 root root 0 May 5 23:18 /tmp/3.txt
-rw-r--r--. 1 root root 0 May 5 23:18 /tmp/4.txt
-rw-r--r--. 1 root root 0 May 5 23:18 /tmp/5.txt
-rw-r--r--. 1 root root 0 May 5 23:18 /tmp/6.txt
-rw-r--r--. 1 root root 0 May 5 23:18 /tmp/7.txt
-rw-r--r--. 1 root root 0 May 5 23:18 /tmp/8.txt
-rw-r--r--. 1 root root 0 May 5 23:18 /tmp/9.txt
[root@linux-87-01 ~]#
此处,将ls换成ll,会报错。
回到题目的要求:把ls换成rm命令就可以删除了:
[root@linux-87-01 ~]# find /tmp/ -type f -name '*.txt' | xargs rm
find命令与其他命令配合使用时要使用 “| xargs”
方法3:find+rm使用-exec
[root@linux-87-01 ~]# find /tmp/ -type f -name '*.txt' -exec rm {} \;
{}:表示前面find找出的结果
\; :固定写法,表示结尾
7)案例07:查找修改时间是3天之前,后缀是*.log的文件,删除
方法1:
[root@linux-87-01 ~]# find / -type f -mtime +3 -name "*.log" | xargs rm -f
方法2:
[root@linux-87-01 ~]# find / -type f -mtime +3 -name "*.log" -exec rm -f {} \;
方法3:
[root@linux-87-01 ~]# rm -f `find / -type f -mtime +3 -name "*.log"`
方法4:
[root@linux-87-01 ~]# rm -f $( find / -type f -mtime +3 -name "*.log" )
8)案例08:在/etc中找出大于50k,修改时间大于7天,以.conf结尾的文件
[root@linux-87-01 ~]# find /etc/ -type f -mtime +7 -size +50k -name "*.conf"
9)案例09:找出/etc目录中以.conf结尾的大于10k的文件,复制到/tmp/目录中
#方法1
[root@linux-87-01 ~]# cp `find /etc -type f -size +10k -name '*.conf'` /tmp/
#方法2
[root@linux-87-01 ~]# cp $(find /etc -type f -size +10k -name '*.conf') /tmp/
#方法3
[root@linux-87-01 ~]# find /etc -type f -size +10k -name '*.conf' | xargs cp -t /tmp/
#方法4
[root@linux-87-01 ~]# find /etc -type f -size +10k -name '*.conf' -exec cp -t /tmp/ {} \;
#方法5
[root@linux-87-01 ~]# find /etc -type f -size +10k -name '*.conf' -exec cp {} /tmp/ \;
使用 xargs的时候的坑:cp 文件 目录:则会报错
必须使用-t选项:
cp -t 目录 文件:把文件复制到目录
与mv使用同样也有坑
10)案例10:找出/var/目录中以.log结尾的文件,打包压缩放到/tmp/目录,名为log.tar.gz
#方法1
[root@linux-87-01 ~]# find /var/ -type f -name "*.log" | xargs tar zcf /tmp/log.tar.gz
#方法2
[root@linux-87-01 ~]# tar zcf /tmp/log.tar.gz `find /var/ -type f -name "*.log"`
#方法3
[root@linux-87-01 ~]# tar zcf /tmp/log.tar.gz $(find /var/ -type f -name "*.log")
#方法4:
[root@linux-87-01 ~]# find /var/ -type f -name "*.log" -exec tar zcf /tmp/log.tar.gz {} +
find … exec 与tar使用的坑:

出现的原因:是因为find 使用exec,找一个处理一个,找一个处理一个。上述命令就是,找到一个文件就打包压缩,再找到一个就打包压缩,所以,不能满足题目要求。
解决方法:把\;改成+
+:表示前面find找出文件,找出所有的文件后再交给exec后面的命令执行
\; :表示前面find找出文件,每找一个就交给exec后面的命令执行,而不是全部找到之后在交给exec后面的命令执行
11)案例11:找出/etc/目录中除了sysctl.conf以外的,以.conf结尾的文件
[root@linux-87-01 ~]# find /etc/ -type f -name '*.conf' ! -name 'sysctl.conf'
!:排除