find有用的却容易忽略的技巧

find /home/linux -name “nn*.txt” -print
find /home/linux -iname “nn*.txt” -print

1. -iname 忽略大小写

$ find . ( -name “shell” -o -name “dest” ) -print
./shell
./shell/dest

2.多个条件用-o,-name会列出文件名或文件夹名(反斜线前面要有空格)

find . -path “/shell/” -print
./shell/combined
./shell/config.property

3. -path 会列出所有匹配此路径的文件名或文件夹名

find . -regex “.*(.py|.sh)$”
./1.sh
./projects/skeleton/NAME/init.py
./projects/skeleton/setup.py

4. -regex使用正则匹配

-iregex忽略大小写

find . ! -regex “.*(.py|.sh)$”

5. !取相反的结果

6. -maxdepth最大深度

-mindepth最小深度

7. -type类型

-atime;-mtime;-ctime

8. a访问时间;m文件内容修改时间;c文件元数据改变时间(权限和所有权)单位是天

 打印出在最近7天内被访问过的所有文件:

find.typefatime7print7访 find . -type f -atime 7 -print
 打印出访问时间超过7天的所有文件:
$ find . -type f -atime +7 -print

 -amin (访问时间);

 -mmin (修改时间);

 -cmin (变化时间)。单位是分钟

9. -newer找出比file.txt修改时间更近的所有文件:

find . -type f -newer file.txt -print

-size 2k
大小,单位可以是ckMG

10. -detele 删除查询到的结果

find . -type f -perm 755 -print

11. -perm 755 根据文件权限查找

12. -user xavier 文件所有者

13. -exec command {} \;

find . -type f -name “*.c” -exec cat {} \;>all_c_files.txt

find . -type f -mtime +10 -name “*.txt” -exec cp {} OLD \;

我们无法在 -exec 参数中直接使用多个命令。它只能够接受单个命令,不过
我们可以耍一个小花招。把多个命令写到一个shell脚本中(例如 command.sh ),然
后在 -exec 中使用这个脚本:
-exec ./commands.sh {} \;

-exec 能够同 printf 结合来生成有用的输出信息。例如:
$ find . -type f -name “*.txt” -exec printf “Text file: %s\n” {} \;

( -name “.git” -prune ) 的作用是用于进行排除

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值