您的位置:首页 > 运维架构 > Shell

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

2018-01-30 10:30 127 查看
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.−typef−atime−7−print打印出恰好在7天前被访问过的所有文件: 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 ) 的作用是用于进行排除

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell