Search (find, locate), execute (exec), confirm (ok),

find

Find the file in the specified directory, the basic format:, find(选项)(参数)another way of writing:find path -option [ -print ] [ -exec -ok command ] {} \;

With multiple test parameters

parameter effect
-name Find by file name
-type Search according to file type: f (file), d (path)
-regex Match regular expression
-i not case sensitive
Means negation, negation
-maxdepth 3 Find the file depth, here is three levels
-size According to file size
-delete delete
-mtime Query according to the last modification time
#选择符合条件的数据文件,并删除
#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

Note: exec only recognizes original commands, not aliases

[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
  • Any command can be received after exec
  • {} Used in conjunction with the -exec option to match all files, and then will be replaced with the corresponding file name
  • Multiple commands cannot be used in the -exec parameter, but files can be accessed

ok

Used to confirm, execute input y, not execute inputn

[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 and exec cannot be used at the same time;

locate

Through database search, the database will be updated regularly (so sometimes new files or recent files cannot be found), of course, it can also be updated manually through commands.

Use example:

#指定路径查找文件
[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

Guess you like

Origin blog.csdn.net/jjt_zaj/article/details/113056547