Linux基础命令find练习

介绍

实时查找工具,通过遍历指定路径完成文件查找

特点:

  • 查找速度略慢
  • 精确查找
  • 实时查找
  • 查找条件丰富
  • 可能只搜索用户具备读取和执行权限的目录

示例

指定目录层级

The -depth option for example makes find traverse the file system in a depth-first order.
-depth 使find按深度优先顺序遍历文件系统

查找/etc/目录中的第一层和第二层

[root@localhost ~]# find /etc/ -maxdepth 2 -mindepth 1

查看文件名;不区分大小写

[root@localhost ~]# find / -name "a.txt"
/root/a.txt
[root@localhost ~]# find / -iname "a.txt"
/root/a.txt
/root/A.txt

glob通配符

[root@localhost ~]# find ~ -iname "*.txt"

练习
1、查找/var目录下属主为root,且属组为mail的所有文件

[root@localhost ~]# find /var -user root -group mail -type f -ls

2、查找/var目录下不属于root、lp、gdm的所有文件

[root@localhost ~]# find /var ! -user root -a ! -user lp -a ! -user gdm -ls
  • ! 取反
  • -a 与 -o 或

3、查找/var目录下最近一周内其内容修改过,同时属主不为root,也不是postfix的文件

[root@localhost ~]# find /var ! -user root -a ! -user postfix -a -mtime -7
  • mtime 文件修改时间
  • ctime 文件创建时间
  • atime 文件访问时间

4、查找当前系统上没有属主或属组,且最近一个周内曾被访问过的文件

[root@localhost ~]# find / -nouser -o -nogroup -a -atime -7

5、查找/etc目录下大于1M且类型为普通文件的所有文件

[root@localhost ~]# find /etc/ -type f -size +1M

6、查找/etc目录下所有用户都没有写权限的文件

[root@localhost ~]# find /etc/ ! -perm /222 -ls
[root@localhost ~]# find /etc/ ! -perm /u=w -ls
  • / 或 表示ugo 9位权限之间是或关系
  • - 与 表示ugo 9位权限之间是与关系

7、查找/etc目录下至少有一类用户没有执行权限的文件

[root@localhost ~]# find /etc/ \( ! -perm /u=x -o ! -perm /g=x -o ! -perm /o=x \) -ls
[root@localhost ~]# find /etc/ ! -perm -111 -ls

组合条件

  • (非 A) 或 (非 B) = 非(A 且 B)
  • (非 A) 且 (非 B) = 非(A 或 B)

8、查找/etc/init.d目录下,所有用户都有执行权限,且其它用户有写权限的文件

[root@localhost ~]# find /etc/init.d/ -perm -111 -a -perm -o=w

9、查找/etc/目录中大于3G的普通文件

find /etc/ -type f -size +3G

篾言:

知耻而后勇,温故而知新

你可能感兴趣的:(Linux,linux,前端,运维)