Linux之文件查找

目录

1.grep: 文件内容过滤

2.查找命令

3.查询命令和配置文件的位置

4.find

4.1按文件名查找 -name

4.2按文件大小 -size

4.3按时间查找 (atime,mtime,ctime)

4.4按文件类型 -type

4.5按文件权限 -perm

5.找到后处理动作


1.grep: 文件内容过滤
[root@qfedu.com ~]# grep 'root' /etc/passwd  #从/etc/passwd文件中过滤root字段
[root@qfedu.com ~]# grep -r 'root' /etc/  #递归查找
2.查找命令
[root@qfedu.com ~]# which ls
3.查询命令和配置文件的位置
[root@qfedu.com ~]# whereis cp
[root@qfedu.com ~]# whereis passwd
4.find

语法:
# find 路径 条件 跟条件相关的操作符   [-exec 动作]
默认不写路径时查找的是当前路径
条件:
1.指定的名称  -name
2.文件类型  -type
3.权限 -perm
4.时间 (atime,mtime,ctime) #天

4.1按文件名查找 -name
从根开始找文件
[root@qfedu.com ~]# find / -name “*file2” #从根开始找文件 
[root@qfedu.com ~]# find /etc -iname "Ifcfg-ens33" #-i忽略大小写
-----------取反  分别找出test5   和除了test5的文件 ------------
[root@qfedu.com ~]# find /home/ -name *test5*
[root@qfedu.com ~]# find /home/ ! -name "test5*" # !--取反
4.2按文件大小 -size
[root@qfedu.com ~]# find /etc -size +5M		#大于5M
[root@qfedu.com ~]# find /etc -size 5M		#等于5M
[root@qfedu.com ~]# find /etc -size -5M      #小于5M
-a:and
[root@qfedu.com ~]# find / -size +3M -a -size -5M  #查找/下面大于3M而且小于5M的文件
-o:or
[root@qfedu.com ~]# find / -size -1M -o -size +80M #查找/下面小于1M或者大于80M的文件
[root@qfedu.com ~]# find / -size -3M -a -name "*.txt" #查找/ 下面小于3M而且名字是.txt的文件
4.3按时间查找 (atime,mtime,ctime)
-atime = access访问时间
-mtime = modify改变时间  内容修改时间会改变
-ctime = change修改时间   属性修改时间会改变

-amin  #分钟
-mmin
-cmin
[root@qfedu.com ~]# find /opt -mtime +5		#修改时间5天之前
[root@qfedu.com ~]# find . -mtime -2		#修改时间2天之内
[root@qfedu.com ~]# find /opt -mmin -2      #修改时间在2分钟之内
4.4按文件类型 -type
f:普通文件 d:目录 l:软链接 b:设备
[root@qfedu.com ~]# find /dev -type f	#f普通文件
[root@qfedu.com ~]# find / -type f -size -1M -o -name "*.txt"
4.5按文件权限 -perm
[root@qfedu.com ~]# find . -perm 644            #.是当前目录    精确查找644  
[root@qfedu.com ~]# find /usr/bin  -perm -4000  #包含set uid
5.找到后处理动作
[root@qfedu.com ~]# find /etc -name "ifcfg*" -exec cp -rf {} /tmp \;    
#exec命令对之前查找出来的文件做进一步操作-----  查找带ifcfg开头的文件复制到tmp下

[root@qfedu.com ~]# find /home/ -name test* -exec rm -rf {} \;   
#{}为前面查找到的内容,\; 格式

[root@qfedu.com ~]# # find /home/ -name "test*" | xargs -i cp {} /tmp/      
#找到之后删除处理xargs 参数传递

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值