菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
158
0

三剑客

原创
05/13 14:22
阅读数 28569

文件查找-find

find 命令的基本语法如下

命令 路径 选项 表达式 动作
find [path...] [options] [expression] [action]
查找 地区 小姐姐 18 约...
视频 路径 日韩 无码
# 举个栗子
[root@localhost ~]# find /etc/ -name '*.sh'
/etc/profile.d/colorgrep.sh
/etc/profile.d/colorls.sh
/etc/profile.d/which2.sh
/etc/profile.d/less.sh
/etc/profile.d/256term.sh
/etc/profile.d/lang.sh
/etc/profile.d/vim.sh
/etc/dhcp/dhclient-exit-hooks.d/azure-cloud.sh
/etc/kernel/postinst.d/51-dracut-rescue-postinst.sh

根据文件名查找文件

//创建文件
touch /etc/sysconfig/network-scripts/{ifcfg-eth1,IFCFG-ETH1}

//查找/etc目录下包含ifcfg-eth0名称的文件
[root@zls ~]# find /etc -name "ifcfg-eth1"

//-i 忽略大小写
[root@zls ~]# find /etc -iname "ifcfg-eth1"
//查找/etc目录下包含ifcfg-eth名称所有文件
[root@zls ~]# find /etc/ -name "ifcfg-eth*"
[root@zls ~]# find /etc -iname "ifcfg-eth*"

//查找包含eth的文件
[root@localhost opt]# find /opt/ -name '*eth*'
[root@localhost opt]# find /opt/ -iname '*eth*'


find /root/dir1 ! (-name 'file5' -o -name 'file9' )

根据文件的大小查找

-size n[cwbkMG]
              `b'    block

              `c'    bytes 字节

              `w'    words 单词

              `k'    kb

              `M'    MB

              `G'    GB

# 查找大于5M的文件
[root@localhost ~]# find /etc/ -size +5M
/etc/udev/hwdb.bin

# 查找等于5M的文件
[root@localhost ~]# find /etc/ -size 5M

# 查找小于5M的文件
[root@localhost ~]# find /etc/ -size -5M

## 动作,查看
[root@localhost ~]# find /etc/ -size +5M -ls

## 动作,删除
[root@localhost ~]# find /tmp/ -size +5M -delete

## 集合name
[root@localhost ~]# find /etc/ -size -5M -name '*.sh'

## 需求:在/etc 找到 .sh  和 .conf 结尾的 小于5M的文件
[root@localhost ~]# find /etc/ -size -5M -name '*.sh' -o -name '*.db'

## 需求:在/etc/ 找到 大于3M 小于5M 名字以.sh结尾和.conf结尾
[root@localhost ~]# find /etc/ \( -size +3M -a -size -6M \) -a \( -name '*.sh' -o -name '*.conf' \)

## 需求:在/etc 目录下找到小于5M 并且 文件名不是以 .sh 结尾 或者 不是以 .db结尾
[root@localhost ~]# find /etc/ -size -5M ! \( -name '*.sh' -o -name '*.db' \)

根据文件类型查找

f:文件
d:目录
l:软连接
s:socket
p:管道文件
b:块设备
c:字符设备

find / -type f
find / -type d
find / -type b

find / -type f -ls

find / -type f|head -5|xargs ls -l

根据日期查找

-mtime

## 找七天之前的文件,(不包含今天)
[root@localhost ~]# find /opt/ -mtime +7 -name '*.txt'

## 找最近七天的文件
[root@localhost ~]# find /opt/ -mtime -7 -name '*.txt'

## 找第七天的(不包含今天)
[root@localhost ~]# find /opt/ -mtime 7 -name '*.txt'


## 企业需求:只保留N天的备份,其余的都删除
[root@localhost ~]#  find /opt/ ! -mtime -7 -name '*.txt' -delete
[root@localhost ~]#  find /opt/ ! -mtime -30 -name '*.txt' -delete

条件语句

-a:and 和,并且
-o:or 或者
!:取反

动作

-print:打印出查找的内容,find默认就会打印
-ls:查看找出的文件相信信息
[root@localhost ~]# find /opt/ ! -perm /222 -ls

-delete
[root@localhost opt]# find /opt/ -type d ! -name 'opt'|xargs rm -fr


-ok
语法: -ok \;
-exec
语法: -exec \;

## 拷贝找到的文件到/tmp下
[root@localhost opt]# find /opt/ -mtime +5 |xargs cp -t /tmp/
[root@localhost opt]# find /opt/ -mtime +5 -exec cp {} /tmp/ \;
[root@localhost opt]# find /opt/ -mtime +5 |xargs -I {} cp {} /tmp/
[root@localhost opt]# find /opt/ -name '*.txt' -ok  cp {} /tmp/ \;
< cp ... /opt/2020-04-01_file.txt > ? y
< cp ... /opt/2020-04-02_file.txt > ? y
< cp ... /opt/2020-04-03_file.txt > ? y
< cp ... /opt/2020-04-04_file.txt > ? y
< cp ... /opt/2020-04-05_file.txt > ? y
< cp ... /opt/2020-04-06_file.txt > ? y
< cp ... /opt/2020-04-07_file.txt > ? y
< cp ... /opt/2020-04-08_file.txt > ? y
< cp ... /opt/2020-04-09_file.txt > ? y
< cp ... /opt/2020-04-10_file.txt > ? y
< cp ... /opt/2020-04-11_file.txt > ? y
< cp ... /opt/2020-04-12_file.txt > ? y
< cp ... /opt/2020-04-13_file.txt > ? y
< cp ... /opt/2020-04-14_file.txt > ? y
< cp ... /opt/2020-04-15_file.txt > ? y
< cp ... /opt/2020-04-16_file.txt > ? y
< cp ... /opt/zls.txt > ? y


## find 结合xargs
#拷贝
find  / -type f |xargs cp -t /tmp
#查看
find  / -type f |xargs ls -l
#替换
find  / -type f |xargs sed -i 's###g'
#移动
find  / -type f |xargs mv -t /tmp
#删除
find  / -type f |xargs rm -fr

find的动作很少用,如find自带的删除动作,它只能实现单个表达式的结果,也就是find删除前的单个命令结果,所以当对find有动作时,需加管道接xargs来进行连接删除。

find-根据用户查找文件

-user:查找属主是X个用户
-group:查找属组是X个组
-nouser:查找没有用户
-nogroup:查找没有组

[root@localhost opt]# find ./ -user zls|xargs ls -l
-rw-r--r--. 1 zls qiandao 0 Apr 16 00:36 ./file1
-rw-r--r--. 1 zls zls     0 Apr 16 00:36 ./file3

[root@localhost opt]# find ./ -user zls -group qiandao
./file1

[root@localhost opt]# find ./ -user zls -o -group qiandao
./file1
./file3
./file4

find-根据深度查找

-maxdepth level

[root@localhost ~]# find /etc/ -maxdepth 2 -type f -name '*.conf'

find-根据文件权限查找

-perm

## 精确查找
[root@localhost opt]# find /opt/ -perm 644

## 包含指定权限的文件
root@localhost opt]# find /opt/ -perm -222

-222:and
/222:or

属主 属组 其它用户
- 1    2     3
  x    w     wx
  r-x  rwx   rwx
  
/ 1    2     3
  x    w     wx
  r-x  rwx   rwx
  rw   wx    rwx
  r-x  r     r

系统文件查看命令-grep


语法:grep [选项] 文件
作用:过滤关键字,并且把关键字所在行的内容都打印出来


|:管道符,作用,将管道符左边命令的标准输出交给管道符右边命令的标准输入来处理。

管道符后面的命令,可以处理管道符前面命令输出的结果。

# 过滤出b.txt中,含有'风'所在行的内容都打印出来
[root@localhost ~]# grep '风' b.txt
江山风景美如画,
一句卧槽风好大。

# 过滤含有卧,所在行的内容
[root@localhost ~]# grep '卧' b.txt
卧槽
一句卧槽风好大。

# 过滤以卧开头的内容
[root@localhost ~]# grep '^卧' b.txt
卧槽

^: 以....开头
$: 以....结尾

# 过滤以大结尾的内容
[root@localhost ~]# grep '大$' b.txt
一句卧槽风好大
风好大

# 正则表达式
[root@localhost ~]# grep '[a-z]' b.txt
My name is zls
18 years old
[root@localhost ~]# grep '[a-Z]' b.txt
My name is zls
18 years old
[root@localhost ~]# grep '[A-z]' b.txt
grep: Invalid range end
[root@localhost ~]# grep '[z]' b.txt
My name is zls
[root@localhost ~]# grep '[a-f]' b.txt
My name is zls
18 years old

# . 表示任意字符
[root@localhost ~]# grep '.' b.txt
卧槽
作者:曾老湿
江山风景美如画
本想吟诗赠天下
奈何自己没文化
一句卧槽风好大
曾老湿
风好大

My name is zls
18 years old


# * 匹配多次
[root@localhost ~]# grep '.*' b.txt
卧槽
作者:曾老湿
江山风景美如画
本想吟诗赠天下
奈何自己没文化
一句卧槽风好大
曾老湿
风好大
My name is zls
18 years old

# rl 匹配目录中的所有文件包含root的用户
[root@tcy day07]# grep -rl "root" /etc/

# -q 参数不输出结果,使用$?判断
[root@tcy day07]# grep -q "root" /etc/passwd
[root@tcy day07]# echo $?

# grep选项:
grep 选项 参数
	.	任意字符
	*	匹配多次
	-n	过滤出来的内容显示行号
	-o	只过滤出来关键字,不显示其他内容
	-i	不区分大小写
	-E	扩展正则,等于egrep
	-v	取反
	-w	精确匹配
	-A	查看关键字,并且会显示关键字后的N行内容
	-B	查看关键字,并且会显示关键字前N行内容
	-C	查看关键字,并且会显示关键字前后N行内容
        -c      查看匹配内容所占有的行数
	-r	递归过滤
        -l      配合-r一起使用,表示递归过滤目录中的文件内容
        -q      静默输入,也就是不会有输入结果,结合$?判断结果
#会显示所有内容
[root@aaa ~]# grep '.*' /etc/services
#显示行号
[root@aaa ~]# grep -n '.*' /etc/services
#只过滤出来关键字
[root@aaa ~]# grep -o 'root' /etc/passwd
root
root
root
root
#不区分大小写
[root@aaa ~]# grep -i 'nimbus' /etc/services
nimcontroller   48000/tcp               # Nimbus Controller
nimcontroller   48000/udp               # Nimbus Controller
nimspooler      48001/tcp               # Nimbus Spooler
nimspooler      48001/udp               # Nimbus Spooler
nimhub          48002/tcp               # Nimbus Hub
nimhub          48002/udp               # Nimbus Hub
nimgtw          48003/tcp               # Nimbus Gateway
nimgtw          48003/udp               # Nimbus Gateway
nimbusdb        48004/tcp               # NimbusDB Connector
nimbusdbctrl    48005/tcp               # NimbusDB Control
#支持扩展正则
[root@aaa ~]# grep -E 'root|bin' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
#不输出root用户
[root@aaa ~]# grep -v 'root' /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
#精确匹配bin
[root@aaa ~]# grep -w 'bin' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
#查看关键字,并显示前2行内容
[root@aaa ~]# grep -B 2 'mail' /etc/passwd
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
#查看关键字,并显示后2行内容
[root@aaa ~]# grep -A 2 'mail' /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
#查看关键字,并显示前后2行内容
[root@aaa ~]# grep -C 2 'mail' /etc/passwd
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
#递归过滤
[root@aaa opt]# grep -r 'tcy' /opt
/opt/tcy.txt:tcy
[root@aaa opt]# grep -Er 'tcy|root' /opt
/opt/tcy.txt:tcy
/opt/tcy.txt:root

发表评论

0/200
158 点赞
0 评论
收藏
为你推荐 换一批