shell编程:linux命令——find详解

当系统子目录或文件越来越多时,查找文件比较困难,这时可以借助find工具查找满足条件的文件。

一、语法格式:
    find [path] [expression] [action]

path参数为指定find所要查找的目录的起点。
    可以同时指定多个目录作为搜索的起点,甚至可以指定根目录/作为起点搜索整个文件系统。
    若没有指定path参数,默认为当前工作目录。

expression表示若干个搜索条件,如文件名-name,修改时间-mtime等等

action表示指定对搜索到的文件执行操作,如-print打印到标准输出,-exec执行某种命令等等

[root@mrhcatxq01 shell]# find -name \*.unl       #默认在当前工作目录及其子目录下查找
./f.unl
./e.unl
./d.unl
./a.unl
./a/aa.unl
./b.unl
./c.unl
[root@mrhcatxq01 shell]# find . -name \*.unl      #指定在当前工作目录及其子目录下查找
./f.unl
./e.unl
./d.unl
./a.unl
./a/aa.unl
./b.unl
./c.unl
[root@mrhcatxq01 shell]#
[root@mrhcatxq01 shell]# find . ./a -name \*.c      #指定在当前工作目录和当前工作子目录a及其它们的子目录下查找
./a/ss.c
./a/ss.c
[root@mrhcatxq01 shell]#
rm -rf ./a/ss.c;rm -rf ./a/s2.c;
[root@mrhcatxq01 shell]# find ./a -name \*.c -print -exec rm -rf {} \;
./a/ss.c
[root@mrhcatxq01 shell]# ls -l ./a/ss.c
ls: cannot access ./a/ss.c: No such file or directory
[root@mrhcatxq01 shell]# find . ./a -name \*.c
[root@mrhcatxq01 shell]#

find ./a -name \*.c -print -exec rm -rf {} \;
以上find命令,在当前目录的子目录a及子目录a的子目录中查找,文件名以.c结尾的文件,然后将找到的文件输出到屏幕,再删除文件。
-exec可以指定对找到的文件执行的命令,此处是rm -rf {},{}表示每一个搜索到的符合条件的文件。
-exec执行命令时需要命令行结束的标识符,而shell通常使用;来标识命令行结束。
由于;对shell来说是特殊字符,需要转义,所以在;前加\转义。也可以使用如下命令实现转义(双引号或单引号):
[root@mrhcatxq01 shell]# touch ./a/ss.c
[root@mrhcatxq01 shell]# find ./a -name \*.c -print -exec rm -rf {} ";"
./a/ss.c
[root@mrhcatxq01 shell]# touch ./a/ss.c
[root@mrhcatxq01 shell]# find ./a -name \*.c -print -exec rm -rf {} ';'
./a/ss.c
[root@mrhcatxq01 shell]#

示例:
1.find . -iname "*.h" -print -exec mv {} ./b \;
[root@mrhcatxq01 shell]# find . -iname "*.h" -print -exec mv {} ./b \;
./a/test22.H
./b/ss.H
mv: `./b/ss.H' and `./b/ss.H' are the same file
./b/ss.h
mv: `./b/ss.h' and `./b/ss.h' are the same file
./b/test22.H
mv: `./b/test22.H' and `./b/test22.H' are the same file
./test11.h

-iname在匹配文件时不区分大小写
-print搜索到的文件打印到屏幕
-exec执行命令,此处执行 mv {} ./b  ,{}标识搜索到的文件,;标识命令行结束,如mv ./a/test22.H ./b;

2.find . -type f -mtime +5 -print
.表示在当前工作目录及其子目录下查找
-type表示指定文件类型,f表示普通文件,若要查找目录使用d
-mtime表示指定修改时间,+5表示5天前,-5表示5天内,5表示修改时间等于5天
-print表示将搜索到的文件名输出到屏幕

二、常用选项:
-name 按照文件名查找文件。
-perm 按照文件权限来查找文件。
-user 按照文件属主来查找文件。
-group 按照文件所属的组来查找文件。
-mtime -n +n 按照文件的更改时间来查找文件,- n表示文件更改时间距现在n天以内,+ n表示文件更改时间距现在n天以前
-type 查找某一类型的文件,如:
b - 块设备文件。
d - 目录。
c - 字符设备文件。
p - 管道文件。
l - 符号链接文件。
f - 普通文件。
-size nM 按nM大小查找文件
-amin n  查找系统中n分钟访问的文件
-atime n 查找系统中n*24小时(n天)访问的文件
-ctime n 查找系统中n*24小时(n天)创建的文件
-mmin n  查找系统中n分钟被改变数据的文件

基于目录的深度搜索
find命令在用时会遍历所有的子目录,我们可以采用一些参数来限制其遍历的深度
-maxdepth:最大深度限制,1表示只在当前目录,2表示两级目录(当前及仅当前的子目录,不包括子目录的子目录)...
find . -maxdepth 1 -type f -print     #只列出当前目录下的所有普通文件

三、逻辑运算符
-a 且;-o 或;!非
[root@mrhcatxq01 shell]# find . -maxdepth 1 -name "*.sh" -o -iname "*.h"
./teste.sh
./testa.sh
./8.sh
[root@mrhcatxq01 shell]#

转载于:https://my.oschina.net/shw23/blog/691274

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值