Linux文件查找、三剑客、正则表达式
Linux文件查找
1.find查找概述
为什么要有文件查找,因为很多时候我们可能会忘了某个文件所在的位置,此时就需要通过find来查找。
find命令可以根据不同的条件来进行查找文件,例如:文件名称、文件大小、文件修改时间、属主属组、权限、等等方式。同时find命令是Linux下必须掌握的。
*find 命令的基本语法如下*
| 命令 |
路径 |
选项 |
表达式 |
动作 |
| find |
[path…] |
[options] |
[expression] |
[action] |
| 查找 |
地区 |
妹纸 |
18-25岁 |
约? |
是linux里面的一个实时查找工具,通过制定路径完成文件查找
find [options] ….. [查找路径] [查找条件] [处理动作]
查找路径:查找的位置,默认是当前文件夹
查找条件:制定查找的标准,文件名、大小、类型、日期等等
处理动作:对符合条件的文件做什么操作,默认是输出到屏幕上
2.find查找示例
*以下列出所有find常用的选项*
1.find名称查找
1 2 3 4
| #1.创建文件touch /etc/sysconfig/network-scripts/{ifcfg-eth1,IFCFG-ETH1} #2.查找/etc目录下包含ifcfg-eth0名称的文件[root@lqz ~]# find /etc -name "ifcfg-eth1" #3.-i 忽略大小写[root@lqz ~]# find /etc -iname "ifcfg-eth1"#查找/etc目录下包含ifcfg-eth名称所有文件[root@lqz ~]# find /etc/ -name "ifcfg-eth*"[root@lqz ~]# find /etc -iname "ifcfg-eth*"
|
2.find大小查找
1 2 3 4
| #1.查找大于5M的文件[root@lqz ~]# find /etc -size +5M #2.查找等于5M的文件[root@lqz ~]# find /etc -size 5M #3.查找小于5M的文件[root@lqz ~]# find /etc -size -5M
|
3.find类型查找
1 2
| # f 文件[root@lqz ~]# find /dev -type f# d 目录[root@lqz ~]# find /dev -type d# l 链接[root@lqz ~]# find /dev -type l# b 块设备[root@lqz ~]# find /dev -type b# c 字符设备[root@lqz ~]# find /dev -type c# s 套接字[root@lqz ~]# find /dev -type s# p 管道文件[root@lqz ~]# find /dev -type p
|
4.find时间查找
1 2 3 4 5
| #1.创建测试文件(后期shell会讲)[root@lqz ~]# for i in {01..28};do date -s 201904$i && touch file-$i;done #2.查找7天以前的文件(不会打印当天的文件)[root@lqz ~]# find ./ -iname "file-*" -mtime +7 #3.查找最近7天的文件,不建议使用(会打印当天的文件)[root@lqz ~]# find ./ -iname "file-*" -mtime -7 #4.查找第7天文件(不会打印当天的文件)[root@lqz ~]# find ./ -iname "file-*" -mtime 7 #5.本地文件保留最近7天的备份文件, 备份服务器保留3个月的备份文件(实际使用方案)find /backup/ -iname "*.bak" -mtime +7 -deletefind /backup/ -iname "*.bak" -mtime +90 -delete
|
5.find用户查找
1 2
| #查找属主是jack[root@lqz ~]# find /home -user jack#查找属组是admin[root@lqz ~]# find /home -group admin#查找属主是jack, 属组是admin[root@lqz ~]# find /home -user jack -group admin#查找属主是jack, 并且属组是admin[root@lqz ~]# find /home -user jack -a -group admin#查找属主是jack, 或者属组是admin[root@lqz ~]# find /home -user jack -o -group admin#查找没有属主[root@lqz ~]# find /home -nouser#查找没有属组[root@lqz ~]# find /home -nogroup#查找没有属主或属组[root@lqz ~]# find /home -nouser -o -nogroup
|
6.find权限查找
1 2 3
| #精切匹配644权限[root@lqz ~]# find . -perm 644 -ls #包含444权限即可[root@lqz ~]# find . -perm -444 -ls#查找全局可写(每位权限必须包含w)[root@lqz ~]# find . -perm -222 -ls#包含set uid[root@lqz ~]# find /usr/sbin -perm -4000 -ls#包含set gid[root@lqz ~]# find /usr/sbin -perm -2000 -ls#包含sticky[root@lqz ~]# find /usr/sbin -perm -1000 -ls
|
查找条件
- 根据文件名查找
- -name 指定名称,可以使用正则
- -iname 忽略大小写
- -links n 引用次数为n的文件
- -regex 后面跟完整路径,而不是文件名, 必须整个路径完全匹配
- 制定搜索的层级
- -maxdepth level 最大的搜索深度,指定的目录为第1层
- -mindepth level 最小的搜索深度,包括level层
- 根据属主、属组来查找
- -user username 查找属主为username的文件
- -group groupname 查找属组为groupname的文件
- -uid id 查找属主为id的文件
- -gid id 查找属组为id的文件
- -nouser 查找没有属主的文件
- -nogroup 查找没有属组的文件
1 2 3 4
| m[root@192 test]#chown qiao bm[root@192 test]#lltotal 0drwxr-xr-x. 4 root root 24 Dec 4 22:50 a-rw-r--r--. 1 qiao root 0 Dec 6 17:53 bm[root@192 test]#chown :llx bm[root@192 test]#lltotal 0drwxr-xr-x. 4 root root 24 Dec 4 22:50 a-rw-r--r--. 1 qiao llx 0 Dec 6 17:53 bm[root@192 test]#find -group llx./bm[root@192 test]#id rootuid=0(root) gid=0(root) groups=0(root)m[root@192 test]#id qiaouid=1000(qiao) gid=1000(qiao) groups=1000(qiao)m[root@192 test]#find -uid 1000./b m[root@192 test]#useradd xiaobaom[root@192 test]#chown xiaobao bm[root@192 test]#lltotal 0drwxr-xr-x. 4 root root 24 Dec 4 22:50 a-rw-r--r--. 1 xiaobao llx 0 Dec 6 17:53 bm[root@192 test]#userdel xiaobaom[root@192 test]#lltotal 0drwxr-xr-x. 4 root root 24 Dec 4 22:50 a-rw-r--r--. 1 1002 llx 0 Dec 6 17:53 bm[root@192 test]#find -nouser./b # 全盘找m[root@192 test]#find / -nouser
|
- 根据文件类型 -type
- d 目录
- f 文件
- l 符号链接
- s 套接字
- b 块设备
- c 字符设备
- p 管道文件
1
| m[root@192 test]#find -type f./b
|
1
| m[root@192 test]#find -empty
|
1 2 3
| m[root@192 test]#find -empty -o -type d m[root@192 test]#find -empty -not -type d ./b
|
- 摩根定律
- 非(A或者B) 非A 且非B
- 非(A且B)非A或非B
1
| m[root@192 ~]#find !(-empty -a -tpye d)
|
1 2
| [root@localhost test]#find /etc -name *_config/etc/ssh/ssh_config/etc/ssh/sshd_config[root@localhost test]#find /etc -path /etc/ssh -name *_config
|
- 按照大小来查找
- -size # (#-1,#] 不包括#-1,包括#
- -size -# [0,#-1] 包括#-1
- -size +# (#,……)
- 按照时间来查找
- -amin
- -mmin
- -cmin
- -atime # [#,#+1)
- -atime -# (0,#)
- -atime +# [#+1,….]
- 查找7天以后的文件 find -atime +7
- -mtime
- -ctime
- 以分钟为单位
3 处理动作
find动作处理,比如查找到一个文件后,需要对文件进行如何处理, find的默认动作是 -print
1.find查找后的动作命令示例
| 动作 |
含义 |
| -print |
打印查找到的内容(默认) |
| -ls |
以长格式显示的方式打印查找到的内容 |
| -delete |
删除查找到的文件(仅能删除空目录) |
| -ok |
后面跟自定义 shell 命令(会提示是否操作) |
| -exec |
后面跟自定义 shell 命令(标准写法 -exec 😉 |
- -print 默认的处理动作,显示在屏幕上
- -ls 类似于ls -l 显示长格式
- -delete 删除查找到的文件
- -fls file 将查找的结果以长格式保存到文件中
- -ok command {} ; 对每一个查找到的文件执行command命令,在执行命令之前要先提示用户是否要执行
1 2
| find -size 2M -ok rm -rf {} \; 找到2M的文件,删除,提示删除
|
- -exec command {} ; 对查到的每一个文件执行command命令,不需要确认,一次性交给后面命令处理
1 2
| find -size 2M -exec rm -rf {} \; m[root@192 test]#find -size 2M -delete
|
1 2 3 4 5 6
| #1.使用-print打印查找到的文件[root@lqz ~]# find /etc -name "ifcfg*"[root@lqz ~]# find /etc -name "ifcfg*" -print #2.使用-ls打印查找到的文件,以长格式显示[root@lqz ~]# find /etc -name "ifcfg*" -ls #3.使用-delete删除文件,但仅能删除空目录[root@lqz ~]# find /etc -name "ifcfg*" -delete #4.使用-ok实现文件拷贝,但会提示是否拷贝[root@lqz ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp \; #5.使用-exec实现文件拷贝和文件删除。[root@lqz ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp \;[root@lqz ~]# find /etc -name "ifcfg*" -exec rm -f {} \;
|
2.使用find命令结合xargs
- 有的命令不支持管道
- 命令参数过长
- xargs 将管道前面的内容一条一条的交给后面命令处理
1
| echo file{1..50000}|xargs touch
|
1 2
| #xargs将前者命令查找到的文件作为一个整体传递后者命令的输入[root@lqz ~]# touch file.txt[root@lqz ~]# find . -name "file.txt" |xargs rm -f[root@lqz ~]# find . -name "file.txt" |xargs -I {} cp -rvf {} /var/tmp
|
3.find逻辑运算符
1 2 3 4
| #1.查找当前目录下,属主不是hdfs的所有文件[root@lqz ~]# find . -not -user hdfs [root@lqz ~]# find . ! -user hdfs #2.查找当前目录下,属主属于hdfs,且大小大于300字节的文件[root@lqz ~]# find . -type f -a -user hdfs -a -size +300c #3.查找当前目录下的属主为hdfs或者以xml结尾的普通文件[root@lqz ~]# find . -type f -a \( -user hdfs -o -name '*.xml' \)
|
4.find相关练习题
1
| 1.查找/tmp目录下,属主不是root,且文件名不以f开头的文件2.查找/var目录下属主为root,且属组为mail的所有文件3.查找/var目录下不属于root、lp、gdm的所有文件4.查找/var目录下最近一周内其内容修改过,同时属主不为root,也不是postfix的文件5.查找/etc目录下大于1M且类型为普通文件的所有文件6.将/etc/中的所有目录(仅目录)复制到/tmp下,目录结构不变7.将/etc目录复制到/var/tmp/,/var/tmp/etc的所有目录权限777/var/tmp/etc目录中所有文件权限6668.保留/var/log/下最近7天的日志文件,其他全部删除9.创建touch file{1..10}10个文件, 保留file9,其他一次全部删除10.解释如下每条命令含义mkdir /root/dir1touch /root/dir1/file{1..10}find /root/dir1 -type f -name "file5"find /root/dir1 ! -name "file5"find /root/dir1 -name "file5" -o -name "file9"find /root/dir1 -name "file5" -o -name "file9" -lsfind /root/dir1 \( -name "file5" -o -name "file9" \) -lsfind /root/dir1 \( -name "file5" -o -name "file9" \) -exec rm -rvf {} \;find /root/dir1 ! \( -name "file4" -o -name "file8" \) -exec rm -vf {} \;
|
1.查找/tmp目录下,属主不是root,且文件名不以f开头的文件
1
| find /tmp ! -user root ! -name "f*"
|
2.查找/var目录下属主为root,且属组为mail的所有文件
1
| find /var -user root -group mail -type f
|
3.查找/var目录下不属于root、lp、gdm的所有文件
1
| find /var ! -user root ! -user lp ! -user gdm -type f
|
4.查找/var目录下最近一周内其内容修改过,同时属主不为root,也不是postfix的文件
1
| find /var -mtime -7 ! -user root ! -user postfix -type f
|
5.查找/etc目录下大于1M且类型为普通文件的所有文件
1
| find /etc -type f -size +1M
|
6.将/etc/中的所有目录(仅目录)复制到/tmp下,目录结构不变
1 2 3
| rsync -av -f"+ */" -f"- *" /etc/ /tmp/ 或 find /etc -type d -exec mkdir -p /tmp/{} \;
|
7.将/etc目录复制到/var/tmp/,/var/tmp/etc的所有目录权限777/var/tmp/etc目录中所有文件权限666
1 2 3
| cp -r /etc /var/tmp/ find /var/tmp/etc -type d -exec chmod 777 {} \; find /var/tmp/etc -type f -exec chmod 666 {} \;
|
8.保留/var/log/下最近7天的日志文件,其他全部删除
1
| find /var/log -name "*.log" -mtime +7 -delete
|
9.创建touch file{1..10}10个文件, 保留file9,其他一次全部删除
1 2
| touch file{1..10} find . -maxdepth 1 -type f -name "file[1-8]" -o -name "file10" -delete
|
10.解释如下每条命令含义
mkdir /root/dir1
touch /root/dir1/file{1..10}
find /root/dir1 -type f -name “file5”
find /root/dir1 ! -name “file5”
find /root/dir1 -name “file5” -o -name “file9”
find /root/dir1 -name “file5” -o -name “file9” -ls
find /root/dir1 ( -name “file5” -o -name “file9” ) -ls
find /root/dir1 ( -name “file5” -o -name “file9” ) -exec rm -rvf {} ;
find /root/dir1 ! ( -name “file4” -o -name “file8” ) -exec rm -vf {} ;
1 2 3 4 5 6 7 8 9
| mkdir /root/dir1 # 在/root目录下创建名为dir1的目录 touch /root/dir1/file{1..10} # 在/root/dir1目录下创建file1到file10共10个空文件 find /root/dir1 -type f -name "file5" # 查找/root/dir1目录下名为file5的普通文件 find /root/dir1 ! -name "file5" # 查找/root/dir1目录下所有不叫file5的文件和目录 find /root/dir1 -name "file5" -o -name "file9" # 查找名为file5或file9的文件 find /root/dir1 -name "file5" -o -name "file9" -ls # 查找file5或file9并显示详细信息 find /root/dir1 \( -name "file5" -o -name "file9" \) -ls # 同上,加了括号更明确优先级 find /root/dir1 \( -name "file5" -o -name "file9" \) -exec rm -rvf {} \; # 删除file5或file9 find /root/dir1 ! \( -name "file4" -o -name "file8" \) -exec rm -vf {} \; # 删除除了file4和file8外的所有文件
|
linux三剑客
三剑客详解
grep
awk
sed
grep
grep [option] “模式” file
option
1
| --color=auto 对匹配到的行添加颜色-v 取反-i 不区分大小写-n 查找的内容增加行号-c 打印匹配到的行数-o 只显示匹配到的文字-q 静默模式-A # after 向下显示#行-B # before 向上显示#行—C # context 上下分别显示#行-e 或者 grep -e 'user' -e 'root' passwd 或的意思-E 扩展正则表达式-F 不使用正则表达式-r 递归-w 匹配整个单词
|
正则表达式元字符
- 字符匹配
- . 任意单个字符
- [] 匹配指定范围内的任意单个字符 [0-9] [a-z] [A-Z]
- [^] 取反
- [:upper:] 大写字母
- [:lower:] 小写字母
- [:alnum:] 字母和数字
- [:alpha:] 大小写字母
- [:digit:] 数字
1 2 3 4
| m[root@192 test]#grep '[[:digit:]]\+' c# 匹配数字
[:black:] 空白[:punct:] 标点符号匹配次数* 表示任意次数.* 任意字符任意次数\? 表示0或者1次\+ 至少一次\{n\} 表示n次 \转义字符\{m,n\} 最少m次,最多n次\{n,\} 至少n次\{,n\} 至多n次位置锚定^ 开头$结尾^$ 空行grep -v "^#" /etc/ssh/sshd_config |grep -v "^$" 显示不以#开头并且不是空行
|
- 向后引用
- \1 前面第一个括号出现的内容匹配完成之后再后面在出现一次
- \2 前面第二个括号出现的内容匹配完成之后再后面在出现一次
1 2
| grep"\(l..e\).*\1" clove djfdjfd;d love
|
1 2
| grep -E "(c|C)at" a 不需要加\进行转义
|
**链接:https://www.cnblogs.com/coderxueshan/p/17933844.html\*\*