学习笔记整理——Find命令,附带练习题及答案。
Find
语法: find [路径] [参数]  如果不输⼊路径,查询当前⽬录
⼩技巧Tips:
1. 在使⽤ -maxdepth 参数的时候,如果有多个选项,把 maxdepth 放到路径的后⾯,其他参数
的前⾯,否则可能会出错。
2. -name 后⾯养成习惯加双引号,避免出错
3. 如果不指定⽂件的具体路径,可以写根⽬录 / ,扩⼤搜索范围。⽐如在搜索⼀个 inode 号的时
候,毫⽆头绪,就可以使⽤根⽬录
4. find 可以和正则表达式匹配⼀起使⽤ !取反,*通配符
参数
-
name  ⽂件名字
-iname 忽略⽂件名的⼤⼩写,匹配所有⼤⼩写字母
-type  f⽂件,d⽬录,l连接⽂件,b块设备,c串⾏端⼝设备
-size  通过⽂件⼤⼩查
-inum  查 inode
-user  指定属主,也可以使⽤ uid
-group 指定⽤户组,也可以使⽤ gid
-*time mtime 创建或更改时间;atime 访问时间;ctime⽂件inode号被修改,
-*min  mmin ±n,⼤于⼩于 n 分钟
-mtime +365 创建或更改时间,⼤于365天的
-mtime -10  创建或更改时间,⼩于10天
-
atime +365 访问或读取时间,⼤于365天
-atime -10  访问或读取时间,⼩于10天
-o    或者
-a    并且
-not  查不满⾜条件的⽂件,⽤在特定的条件之前
-mindepth 指定⽬录的开始深度
-mindepth 0 不限制
-mindepth 1 从当前⽬录及其内容开始
/tmp/link/a/1
/
/tmp/2
-
mindepth 2 从⼀级⼦⽬录内容开始,当前⽬录的⽂件和⽬录不再范围内
/tmp/link/3
/tmp/link/a/2
-maxdepth 指定⽬录的最⼤深度
-maxdepth 0
-maxdepth 1 只查⽬录本⾝及其内部⽂件,包括⼀级⽬录本⾝
/tmp/2
/tmp/3
-maxdepth 2 ⽬录内的⽂件,包括⽬录下的⼀级⼦⽬录及其⽂件
/tmp/link/2
/tmp/link/3
/
/tmp/harda
/tmp/2
-perm 指定⽂件权限
-perm  mode,匹配项必须严格匹配此权限
-perm -mode,匹配项必须不少于此权限。匹配⼤于此权限的⽂件
-perm /mode,匹配项中 任何⼀组包含要求权限中的任意⼀个 就可以,仅限于普通权限,相对于单独的
rwx ⾥⾯的任意⼀个权限,并⾮ user group others 654⾥⾯的 6 5 4 任意权限。如果权限使 644,转换
或可写、other 可读或可写,均可以匹配。
-perm /mode(四位),和3位的数字权限规则⼀致,区别在于特殊权限和普通权限独⽴思考
-perm +mode,功能和/⼀样,
-path "..." -prune -o [其他参数] -print
... 代表排除指定的⽂件或者⽬录,其他均为固定写法。注意,如果在排除⽬录的时候,最后⾯不能有/符号,√"/tmp/123"  ×"/tmp/123/"。
-empty  空⽂件,空⽬录,
-false  总是出错的⽂件
-fstype  指定⽂件类型查,ext3 ext4
Find练习题
1.查 /tmp ⽬录下名字为 aming开头的所有⽂件。
find /tmp -name "aming*"
注意这⾥并不能使⽤ ^aming
2.查指定 inode 的⽂件或⽬录
find / -inum 141418
3.出 tmp ⽬录下的所有⼀级⼦⽬录
find /tmp -maxdepth 1 -type d
在root⽬录下及其最⼤两层深度的⼦⽬录中,查 passwd ⽂件
find /root -maxdepth 3 -name passwd
在第⼆层和第四层⼦⽬录之间查 passwd ⽂件
find . -mindepth 3 -maxdepth 5 -name passwd
4.搜索tmp⽬录下所属组group1,所属主user1的⽂件
find /tmp -type f -user user1 -group group1
5.搜索根⽬录下的 1. 和 a ⽬录
find / - -o - -o -type d -name a
6. 搜索tmp⽬录下以 a 开头并且以 c 结尾的⽂件
find /tmp -name "a*" -a -name "*c" -type f
7. 搜索 tmp ⽬录下,不是以 a 开头,并且⼤⼩超过100M的⽂件
find /tmp -not -name "a*" -a -name "*txt" -size +100M -tyep f
8. 搜索 tmp ⽬录下,修改时间⼀年内的⽂件
find . -type f  -mtime -365
9. 搜索⽬录下,修改时间⼀年内,不是⽂件的其他类型
find . -not -type f -mtime -365
10.搜索⽬录下,修改时间超过⼀年的并且⼤于100M的⽂件
find . -type f -not -mtime -365 -a -size +100M
11.列出tmp⽬录下⼀年内都没有改变的⽂件
find /tmp -type f ! -mtime -365
12.出 tmp ⽬录下的所有的⼀级空⽬录
13.搜索tmp⽬录下名称包含“*.txt"的⽬录,或者包含"*.txt"并且权限为777的⽂件或⽬录
find /tmp -name *.txt -a \( -perm 777 -o -type d \)
-a and意思
14.查所有的隐藏⽬录
find . -type d -name ".*"
15.搜索当前⽬录下10天以前的⽂件,或者权限为644的的⽂件和⽬录
find . -type f -mtime +10 -o -perm 644
16.在 / ⽬录下的 etc⽬录及其⼦⽬录下查 passwd ⽂件
find /etc -name passwd
find / -path -path "/etc*" -name passwd
/
etc/passwd
/etc/pam.d/passwd
在根⽬录下的 etc 以及 tmp ⽬录内查 passwd ⽂件
find查命令的使用
cp /etc/passwd /tmp
find / \( -path "/etc*" -o -path "/tmp*" \) -name passwd
在所有名为 general 的⽬录下查 .stp 结尾的⽂件
find / -path "*/general/*" -name "*.stp"
到 usr ⽬录下,并且在 applications ⼦⽬录⾥⾯以 .cache 结尾的⽂件。假设并不知道 applications 的绝对路径,并且⽬录是第 10 层甚⾄第 100 层⽬录,只知道绝对路径以 /usr/local 开头
⽅法⼀:
find /usr/local -name applications -type d
在⽤上⾯到的绝对路径去执⾏ find ....... -name "*.cache"
⽅法⼆:
find /usr/local -path "*share*" -name "*.cache"
NOTE: -path ".." ,表⽰ path 路径⾥⾯包含指定的字符。
17.查除 /etc ⽬录及其⼦⽬录下的,仍在根⽬录的 passwd ⽂件
find / -path "/etc" -prune -o -name passwd -print
查/tmp/ ⽬录下所有⽂件(不包含⽬录), 并且不包含⽬录123
find /tmp/ -path "/tmp/123" -prune -o -type f -print
-print -path -prune -o 固定格式,⼀个都不能少 ,表⽰排除指定的⽬录或⽂件
查/tmp/ ⽬录下所有⽂件(不包含⽬录), 并且不包含⽬录123和⽬录234和⽬录345
find /tmp/ \( -path "/tmp/123" -o -path "/tmp/234" -o -path "/tmp/345" \) -prune -o -type f -print
注意,如果是查⽬录时,-path 后⾯的⽬录名⼀定不要带/ 如 写成 -path "/tmp/123/" 就错了,⽽查⽂件时,带/ 没有问题。
18.删除tmp⽬录下所有⽂件
find /tmp/ -type f |xargs rm
19.查类型为 ext4 的⽂件
find . -fstype ext4
20.查tmp⽬录下包含abcde字母的⽂件,不区分⼤⼩
使⽤find重命名⽂件
21.⽤inode 16187430 编号重命名为 newtest
find -inum 16187430 -exec mv {} new-test-file-name \;
22.给tmp⽬录下的所有⽂件增加.bak后缀
find /tmp/ -type f |xargs -i {} {}.bak
-i 把签名列出的⽂件⼀个个的处理
{} 表⽰⼀次⼀个对象,⽐如find出来很多⾏,每次处理⼀⾏,⽤{}代表这⼀⾏
23.到具有组读权限的⽂件和⽬录
find . -perm -g=r  -exec ls -l {} \;
-g=r 会模糊匹配,只要含有 r 权限的都会列出来
24.到具有组读权限和写权限的⽂件和⽬录
find . -perm -g=r -a -perm -g=w -type f
25.搜索具有⽤户读写可执⾏权限的⽬录
find . -perm -u=r -a -perm -u=w -a -perm -u=x -type f
26.到对组⽤户具有只读权限的⽂件,
find -perm g=r -exec ls -l {} \;
----r----- 1 root root 0 3⽉  31 21:50 ./1
find -perm 040 -exec ls -l {} \;
27.列出/etc⽬录及其⼦⽬录下的5个最⼤的⽂件。
find /etc  -exec ls -s {} \; | sort -n -r | head -5
28.列出 /etc ⽬录及其⼦⽬录下5个最⼩的⽂件
find /etc -type f -exec ls -s {} \; | sort -n  | head -5
29.列出 /etc ⽬录及其⼦⽬录下5个最⼩的⽂件,不包含 0 空⽂件
find /etc -not -empty -type f -exec ls -s {} \; | sort -n  | head -5
find /etc ! -empty -type f -exec ls -s {} \; | sort -n  | head -5
30.find 命令删除⼤于100M的 .zip ⽂件
find / -type f -name "*.zip" -size +100M -exec rm -i {} \;
31.列出在1.txt修改之后修改的⽂件
find -
32.列出在1.txt 和 test 之间修改的⽂件
find - ! -newer test      该命令列出的⽂件,会包含 test ⽂件
33.列出在 2012.12.01 到 2012.12.31 修改的⽂件
思路,⼿动创建两个⽂件,⽂件⽬录⽆所谓,修改时间
touch -m -t
touch -m -t
find / - ! -
配合 alias,实际⼯作中可⽤
alias rmc="find . -iname core -exec rm {} \;"      删除c程序产⽣的core⽂件
alias rm1g="find / -type f -name *.tar -size +1G -exec rm -i {} \;"
alias rm2g="find / -type f -name *.tar -size +2G -exec rm -i {} \;"
alias rm5g="find / -type f -name *.tar -size +5G -exec rm -i {} \;"
到具有组读权限的⽂件find ⼯作中最常⽤的命令
find /path -type[f d]-mtime [+-n] -name "*log*" -size +100M |xargs rm -rf
find /path -type[f d]-mtime [+-n] -name "*name*" -size -100M -exec rm -rf {} \;
find . -type d -o \( -type f -mtime +7 \)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。