Find 用法整理笔记,附带练习题

回复 收藏

原址:http://blog.csdn.net/cityzenoldwang/article/details/68951990

整合了笔记,以及部分扩展资料。

如有错误请指正,隔壁老王登门道谢,O(∩_∩)O。

Find

语法: find [路径] [参数]  如果不输入路径,查询当前目录

小技巧Tips:

  • 在使用 -maxdepth 参数的时候,如果有多个选项,把 maxdepth 放到路径的后面,其他参数的前面,否则可能会出错。

  • -name 后面养成习惯加双引号,避免出错

  • 如果不指定文件的具体路径,可以写根目录 / ,扩大搜索范围。比如在搜索一个 inode 号的时候,毫无头绪,就可以使用根目录

  • 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.txt

                        /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/2.txt

                        /tmp/harda

                        /tmp/2

  • -perm 指定文件权限 

            -perm  mode,匹配项必须严格匹配此权限

            -perm -mode,匹配项必须不少于此权限。匹配大于此权限的文件

            -perm /mode,匹配项中 任何一组包含要求权限中的任意一个 就可以,仅限于普通权限,相对于单独的 rwx 里面的任意一个权限,并非 user group others 654里面的 6 5 4 任意权限。如果权限使 644,转换为二进制 110 100 100, 那么找到的文件只需匹配到任意一个 1 即可,即 user 可读或可写、group 可读或可写、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.txt 2.txt 和 a 目录

  find / -name 1.txt -o -name 2.txt -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 目录下的所有的一级空目录

find . -type d -empty

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 文件

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 /tmp -iname 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 -newer 1.txt

32.列出在1.txt 和 test 之间修改的文件

find -newer 1.txt ! -newer test       该命令列出的文件,会包含 test 文件

33.列出在 2012.12.01 到 2012.12.31 修改的文件

思路,手动创建两个文件,并指定时间,创建的文件目录无所谓,在find的时候指定目录即可,

touch -m -t 1212010000 1.txt

touch -m -t 1212312359 2.txt

find / -newer 1.txt ! -newer 2.txt

 find 名配合 alias,实际工作中可用

alias rmc="find . -iname core -exec rm {} \;"      删除c程序产生的core文件

alias rmao="find . -iname a.out -exec rm {} \;"

alias rm100m="find / -type f -name *.tar -size +100M -exec rm -i {} \;"

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 {} \;"

2017-04-02 08:52 举报
已邀请:
0

王斌

赞同来自:

另外大湿兄的笔记里面摘抄了一些,说是工作中常用的命令,理解思路即可。

  • 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 )

0

can1

赞同来自:

学习了 

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: