搜索(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

-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

猜你喜欢

转载自blog.csdn.net/jjt_zaj/article/details/113056547