搜索(find,locate),执行(exec),确认(ok),

find

在指定目录下查找文件,基本格式:find(选项)(参数) ,另一种写法:find path -option [ -print ] [ -exec -ok command ] {} \;

具有多个test参数

参数作用
-name根据文件名查找
-type根据文件类型查找:f(文件),d(路径)
-regex匹配正则表达式
-i不区分大小写
表示否定,取反
-maxdepth 3查找文件深度,这里是三层
-size根据文件大小
-delete删除
-mtime根据最后修改时间查询,
#选择符合条件的数据文件,并删除
#find默认匹配的是通配符
find .  -type f -name "*.log.[0-9]*" -delete 

# + 表示超过那个时间,-表示那个时间之内
# 不填写路径表示当前路径
[dps@ccod131 bak]$ find   -mtime -10
.
./test11.txt
./TEST.TXT
./update-resin.sh
./zaj.sh

# 基于正则表达式查找
[dps@ccod131 bak]$ find -regex ".*\.sh"
./break_continue.sh
./if_file.sh
./if_number.sh
./if_string.sh
./select_1.sh
./read.sh
./until.sh
./update-resin.sh

# -i 不区分大小写
[dps@ccod131 bak]$ find -iregex ".*\.sh"
./break_continue.sh
./if_file.sh
./if_number.sh
./if_string.sh
./select_1.sh
./read.sh
./until.sh
./update-resin.sh

# i表示否定
[dps@ccod131 bak]$ find  ! -iregex ".*\.SH"
.
./test11.txt
./test
./test/a7b
./test/a2b
./test/a6b

# 查找指定时间段内的文件
 find . -name '*.dat' -newermt '2023-09-01' ! -newermt '2023-11-01'

mtime

操作含义
+1修改时间大于1天的数据,也就是修改时间在48h之内的数据
1修改时间在24-48h之内的数据
0修改时间在24小时以内的数据
-1修改时间在24小时以内的数据
在这里插入图片描述

-exec

注意:exec只识别原始命令,不识别别名

[dps@ccod131 bak]$ find  -name "*.sh" -size +2  -exec ls -l {} \;
-rwxr-xr-x 1 dps dps 2016 12月  3 17:59 ./update-resin.sh
  • exec后边可以接任何命令
  • {} 用于与-exec选项结合使用来匹配所有文件,然后会被替换为相应的文件名
  • -exec参数中无法使用多个命令,不过可以接文件

ok

用来确认,执行输入y,不执行输入n

[dps@ccod131 bak]$ find  -name "*.sh" -size +2  -ok   ls -l {} \;
< ls ... ./update-resin.sh > ? y
-rwxr-xr-x 1 dps dps 2016 12月  3 17:59 ./update-resin.sh

  • ok和exec不能同时使用;

locate

通过数据库搜索,数据库会定时更新(故有时候新建的文件或近期的文件查不到),当然可也通过命令手动更新。

使用样例:

#指定路径查找文件
[dps@ccod131 bak]$ locate ~/bak/test
/dps/bak/test
/dps/bak/test11.txt
/dps/bak/test/a1b
/dps/bak/test/a2b
/dps/bak/test/a3b
/dps/bak/test/a5b
/dps/bak/test/a6b
/dps/bak/test/a7b

# 默认查找该机器的所有路径
[dps@ccod131 bak]$ locate bash
/bin/bash
/dps/.bash_history
/dps/.bash_logout
/dps/.bash_profile
/dps/.bashrc
/dps/jdk1.7.0_10/jre/lib/zi/Africa/Lubumbashi
/etc/bash_completion.d
/etc/bashrc
/etc/bash_completion.d/gdbus-bash-completion.sh
/etc/bash_completion.d/git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值