查询主要使用locate和find方法

locate

#locate查询方法演示
locate 文件名(文件名包括文件的绝对路径)
#注意
locate查询方法是直接由updatedb创建的数据库创建,该数据库会周期性更新,因此当下创建的文件并不会出现在数据库中
#更新数据库的方式
sudo /usr/libexec/locate.updatedb
#应用
rails项目都是在当下创建的,locate查询方法并不适用于rails项目,这里只是做方法说明,建议使用find查询方法

find

#find查询方式
find 文件夹(从文件夹以及子文件夹这个范围中查询) 文件属性
#文件属性
#文件名称
-name demo.rb

#文件类型
-type d

#文件大小
-size +1M

#文件权限
-perms 07008进制)
#逻辑操作符
-and
-or
-not
()

#示例
find ~ \( -type f -not -perm 0600 \) -or \( -type d -not -perm 0700 \)
#find命令允许基于搜索结果来执行操作
-delete #删除当前匹配的文件
-ls 
-print #把匹配文件的全路径名输送到标准输出
-exit #一旦找到一个匹配,退出

#用户自定义行为,通过-ok, -exec, -xargs
-exec command {} ;
-exec rm '{}' ';' #一般形式,其中command就是指一个命令的名字,{}是当前路径名的符号表示,分号是要求的界定符 表明命令结束
find ~ -type f -name 'foo*' -ok ls -l '{}' ';' #使用ok方式,花括号和分号对于 shell 有特殊含义,所以它们必须被引起来或被转义
find ~ -type f -name 'foo*' -exec ls -l '{}' + #最后该为“+”,使得把搜索结果结合为一个参数列表, 然后执行一次所期望的命令
find ~ -type f -name 'foo*' -print | xargs ls -l #xargs 它从标准输入接受输入,并把输入转换为一个特定命令的参数列表
#正则匹配需要加引号,特殊符号需要进行转义
find ~ -type f -name '*.BAK' -print #正则匹配需要加引号
find ~ \( -type f -not -perm 0600 \) -or \( -type d -not -perm 0700 \) #括号需要进行转义“\”

示例

#创建100个文件夹,同时每个文件夹中有26个文件
mkdir -p playground/dir-{00{1..9},0{10..99},100} #建立100个文件夹
touch playground/dir-{00{1..9},0{10..99},100}/file-{A..Z} #每个文件夹中有26个文件

results matching ""

    No results matching ""