linux shell 脚本攻略学习5---find命令详解

1.find命令详解

语法:

find base_path#base_path可以是任何位置,find会从该位置向下找

实例:

amosli@amosli-pc:~$ find /home/amosli/learn/
/home/amosli/learn/
/home/amosli/learn/timing.log
/home/amosli/learn/file.py
......

查找当前目录:

find . -print,'.'代表当前目录,'..'代表父目录,-printf指明打钱出匹配文件的文件名(路径)。当使用-print时,‘\n’作为分隔文件的定界符

根据文件名或者正则表达式匹配搜索

-name 参数,搜索文件名中以.txt结尾的文件:

amosli@amosli-pc:~/learn$ find /home/amosli/learn/ -name "*.txt" -print
/home/amosli/learn/input.txt
/home/amosli/learn/stderr.txt
/home/amosli/learn/test1.txt
/home/amosli/learn/output.txt
/home/amosli/learn/out.txt
/home/amosli/learn/mutil_blank.txt
/home/amosli/learn/stdout.txt
/home/amosli/learn/text.txt

查找文件名不区分大小写-iname参数:

amosli@amosli-pc:~/learn$ find . -iname 'tex*'
./TEXT.txt
./text.txt

-o参数,用来匹配多个条件中的一个:

amosli@amosli-pc:~/learn$ find . \( -name "te*" -o -name "*.pdf" \)
./text.pdf
./test1.txt
./text.txt

-path参数,用来匹配文件路径或者文件的:

amosli@amosli-pc:~/learn$ find . -path "*test*"
./test1.txt
./test

-regex参数,使用正则表达式来匹配:

amosli@amosli-pc:~/learn/test$ ls 
new.PY  next.jsp  test.py
amosli@amosli-pc:~/learn/test$ find . -regex ".*\(\.py\|\.sh\)$"
./test.py

基于目录深度的搜索:

-maxdepth和-mindepth,最大深度,最小深度搜索,会搜索子目录

amosli@amosli-pc:~/learn/test$ find . -maxdepth 1 -type f 
./new.PY
./test.py
./next.jsp
amosli@amosli-pc:~/learn/test$ find . -mindepth 0 -type f #如果深度为>1的话就没有结果了,因为下面没有子目录了
./new.PY
./test.py
./next.jsp

基于文件类型的搜索:

find . -type d -print
f 普通文件
l 符号链接
d 目录
c 字符设备
b 块设备
s 套接字
p Fifo
amosli@amosli-pc:~/learn/test$ find . -type d
.
amosli@amosli-pc:~/learn/test$ find . -type f
./new.PY
./test.py
./next.jsp

根据文件时间搜索

计量单位 天
-atime 最近一次访问时间
-mtime 最后一次被修改时间
-ctime 文件元数据,最近一次修改时间

find . -type f -atime -7 -print #七天内
                      +7 -print #超过七天
分钟
-amin 访问时间
-mmin 修改时间
-cmin 变化时间
find . -type f -newer file.txt -print  #用于比较时间戳的参考文件,比参考文件更新的文件

 根据文件大小的搜索

find . -type f -size +2k 
+ 大于 -小于 无符号,恰好等于 b 块 c 字节 w 字(2字节) k 千字节 M 兆字节 G 吉字节

学了这么多实际应用一把:

需求:查找普通文件大于100M的以zip结尾的文件,顺便清理掉垃圾文件

amosli@amosli-pc:/$ sudo find . -mindepth 0 -type f -size +100M -name "*.zip"
./home/amosli/project/software/zip_tar/glassfish-4.0-ml.zip
./home/amosli/下载/96186_20130619.zip

现在删除glassfish的zip包

amosli@amosli-pc:/$ rm /home/amosli/project/software/zip_tar/glassfish-4.0-ml.zip
amosli@amosli-pc:/$ sudo find . -mindepth 0 -type f -size +100M -name "*.zip"
./home/amosli/下载/96186_20130619.zip

 是不是感觉有点麻烦?找到之后还要手工去删除。下面介绍-delete参数就可以解决这个问题了。

-delete 删除匹配的文件
find . -type f -name "*.swp" -delete
#注意:-delete位置一定是最后

再执行一下刚才的命令加上-delete参数

amosli@amosli-pc:/$ sudo find . -mindepth 0 -type f -size +100M -name "*.zip" -delete

即可删除96186_20130619.zip

-exec 执行命令或动作

find . -type f -user root -exec chown slynux {} \;
find . -type f -exec cp {} OLD \;
find . -iname "abc.txt" -exec md5sum {} \;
exec无法结合多个命令,可以将多个命令放入脚本调用
  
实例:
root@amosli-pc:/home/amosli/learn/test9# ll
total 8
drwxrwxr-x 2 amosli amosli 4096 12月 21 01:37 ./
drwxrwxr-x 4 amosli amosli 4096 12月 21 01:35 ../
-rw-r--r-- 1 root   root      0 12月 21 01:37 a.txt
-rw-r--r-- 1 root   root      0 12月 21 01:37 b.txt
root@amosli-pc:/home/amosli/learn/test9# find . -type f -user root -exec chown amosli {} \;
root@amosli-pc:/home/amosli/learn/test9# ll
total 8
drwxrwxr-x 2 amosli amosli 4096 12月 21 01:37 ./
drwxrwxr-x 4 amosli amosli 4096 12月 21 01:35 ../
-rw-r--r-- 1 amosli root      0 12月 21 01:37 a.txt
-rw-r--r-- 1 amosli root      0 12月 21 01:37 b.txt
find . -type f -user root -exec chown amosli {} \;
说明:-exec执行了chown 命令,将文件权限改为amoslib .{}是一个特殊的字符串,对于每一个匹配的文件来说,{}会被替换成相应的文件名

跳过指定目录
find . \( -name ".git" -prune \) -name '*.txt'
\( -name ".git" -prune \)表示应当去除.git仓库存储文件,然后查询当前目录下名称为*.txt的文件。
 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值