目录:
- 一、find命令的见解
- 二、find命令的格式
- 三、find命令的特点
- 四、用法详解(敲黑板,划重点!!!)
- 五、实例(敲黑板,划重点!!!)
一、对find命令的见解
我对find命令的理解是,在你对目录有读取和执行权限的情况下,它允许您按文件名、文件类型、文件大小、文件权限、用户、组甚至是时间戳来实时性的查找文件!!!当然,强大的功能也必然会有许多的参数。下面就让我来带领大家一起领略一下find命令的风采吧。
二、find命令的格式
find [option] [查找路径] [查找条件] [处理动作]
查找路径:指定目标的路径,可以是相对路径,也可以是绝对路径,默认为当前路径
查找条件:它允许您按文件名、文件类型、文件大小、文件权限、用户、组甚至是时间戳来实时性的查找文件。
查找动作:对符合条件的文件做操作,默认输出到屏幕。
说了这么多,大家可能看的还是云里雾里吧,具体的用法还是要结合实例来认识。
三、find命令的特点:
查找速度略慢
精确查找
实时查找
可能只搜索用户具备读取和执行权限的目录
四、用法详解
注:没打路径的都是默认的当前目录下的路径,find默认的是递归查找。
1、-mindepth N 最小搜索深度
[root@localhost app]# find -mindepth 1
./dir
./dir/mkdir
./dir/ss
2、-maxdepth N 最大搜索深度
[root@localhost app]# find -maxdepth 2
./dir
./dir/mkdir
./dir/ss
3、mindepth N -maxdepth N 只搜索指定深度
[root@localhost app]# find -maxdepth 2 -mindepth 2
./dir/mkdir
./dir/ss
./dir/dd
4、-name 以名称为搜索条件 默认精确查找
[root@localhost app]# find -name ss
./dir/ss
./ss
5、-inum inode号 以inode号为查找条件(注:ls -i 文件名 //查看文件的inode编号)
[root@localhost app]# ls -i ss
52122275 ss
[root@localhost app]# find -inum 52122275
./ss
6、-samefile file1 找出与file1 inode相同的文件 (注:ln 是创建相同iNode编号的文件)
[root@localhost app]# ln ss sss
[root@localhost app]# find -inum 52122275
./ss
./sss
7、-links n 找到硬链接数为n的文件
[root@localhost app]# find -links 1
./dir/mkdir
./.a.sh.swp
./a.sh
8、-user USERNAME:查找属主为指定用户(UID)的文件
[root@localhost app]# mkdir dd
[root@localhost app]# chown liubei dd
[root@localhost app]# ll
total 16
-rwxr-xr-x. 1 root root 188 Jul 24 04:49 a.sh
-rw-r--r--. 1 root root 193 Jul 24 05:13 creat.sh
drwxr-xr-x. 2 liubei root 6 Jul 28 19:17 dd
drwxrwSrwx. 4 root g1 39 Jul 24 03:02 dir
-rw-r--r--. 1 root root 4287 Jul 27 20:13 file1
-rw-r--r--. 2 root root 0 Jul 27 08:21 ss
-rw-r--r--. 2 root root 0 Jul 27 08:21 sss
[root@localhost app]# find -user liubei
./dd
9、-group GRPNAME: 查找属组为指定组(GID)的文件
[root@localhost app]# ll
total 16
-rwxr-xr-x. 1 root root 188 Jul 24 04:49 a.sh
-rw-r--r--. 1 root root 193 Jul 24 05:13 creat.sh
drwxr-xr-x. 2 liubei root 6 Jul 28 19:17 dd
drwxrwSrwx. 4 root g1 39 Jul 24 03:02 dir
-rw-r--r--. 1 root root 4287 Jul 27 20:13 file1
-rw-r--r--. 2 root root 0 Jul 27 08:21 ss
-rw-r--r--. 2 root root 0 Jul 27 08:21 sss
[root@localhost app]# find -group g1
./dir
./dir/mkdir
./dir/ss
./dir/dd
10、-uidUserID:查找属主为指定的UID号的文件
[root@localhost app]# find -uid 0
./dir
./dir/mkdir
./.a.sh.swp
11、 -gidGroupID:查找属组为指定的GID号的文件
[root@localhost app]# find -gid 0
./.a.sh.swp
./a.sh
./creat.sh
./ss
12、 -nouser:查找没有属主的文件
-nogroup:查找没有属组的文件
find / -nouser -o -nogroup
这两个命令可能不会在您的系统上生成实际的结果。但是,它可用于识别或许在经常移动后没有用户或组的文件。
13、-type //匹配文件类型
f: 普通文件
d: 目录文件
l: 符号链接文件
s:套接字文件
b: 块设备文件
c: 字符设备文件
p: 管道文件
[root@localhost app]# ll
total 16
-rwxr-xr-x. 1 root root 188 Jul 24 04:49 a.sh
-rw-r--r--. 1 root root 193 Jul 24 05:13 creat.sh
drwxr-xr-x. 2 liubei root 6 Jul 28 19:17 dd
drwxrwSrwx. 4 root g1 39 Jul 24 03:02 dir
-rw-r--r--. 1 root root 4287 Jul 27 20:13 file1
-rw-r--r--. 2 root root 0 Jul 27 08:21 ss
-rw-r--r--. 2 root root 0 Jul 27 08:21 sss
[root@localhost app]# find -type d
./dir
./dir/ss
./dir/dd
./dd
14、-size [+-]N //根据文件大小查找
常用单位:k, M, G,c(byte),6k 表示(5k,6k],-6k 表示[0,5k],+6k 表示(6k,∞)
[root@localhost app]# ls -lh
total 16K
-rwxr-xr-x. 1 root root 188 Jul 24 04:49 a.sh
-rw-r--r--. 1 root root 193 Jul 24 05:13 creat.sh
drwxr-xr-x. 2 liubei root 6 Jul 28 19:17 dd
drwxrwSrwx. 4 root g1 39 Jul 24 03:02 dir
-rw-r--r--. 1 root root 4.2K Jul 27 20:13 file1
-rw-r--r--. 2 root root 0 Jul 27 08:21 ss
-rw-r--r--. 2 root root 0 Jul 27 08:21 sss
[root@localhost app]# find -size +4k
./dir/mkdir
./.a.sh.swp
./file1
15、根据时间戳查找
[root@localhost app]# find -atime -1
./dir
./dir/ss
可以根据三个时间戳(atime、ctime、mtime)查找,也可以以分钟为单位查找(amin、cmin、mmin)。本例-1表示一天以内,+1的话就表示一天以前的时间。
16、根据权限查找 -perm [/-]MODE
/表示或关系,-表示且关系。
/MODE:任何一类(u,g,o)对象的权限中只要能一位匹配即可,或关系,+ 从centos7开始淘汰
-MODE:每一类对象都必须同时拥有指定权限,与关系
0 表示不关注
find-perm755会匹配权限模式恰好是755的文件
只要当任意人有写权限时,find -perm +222就会匹配
只有当每个人都有写权限时,find -perm -222才会匹配
只有当其它人(other)有写权限时,find -perm -002才会匹配
这样说可能有些难懂,下面的实例会加深你对这一点的理解。
17、处理动作
-print (默认) 查找的结果打印到终端
-ls 查找的结果显示详细信息
-delete 将查找的结果删除,不交互
-ok shell command {} \; 交互执行shell命令
-exec shell command {} \; 非交互执行shell命令
五、实例。
1、查找/var目录下属主为root,且属组为mail的所有文件
find -user root -group mail
2、查找/var目录下不属于root、lp、gdm的所有文件
find /var -not \( -user root -o -user lp -o -user gdm \) -ls
find /var -not -user root -not -user lp -not -user gdm
3、查找/var目录下最近一周内其内容修改过,同时属主不为root,也不是postfix的文件
find /var -mtime -7 -not -user root -not -user postfix
find /var -mtime -7 -not \( -user root -o -user postfix \)
4、查找当前系统上没有属主或属组,且最近一个周内曾被访问过的文件
find -nouser -o -nogroup -atime -7
5、查找/etc目录下大于1M且类型为普通文件的所有文件
find /var -maxdepth 2 -size +1M -a -type f
6、查找/etc目录下所有用户都没有写权限的文件
find /etc/ -perm -not -222
7、查找/etc目录下至少有一类用户没有执行权限的文件
find /etc -not -perm /111 -type f -ls
8、查找/etc/init.d目录下,所有用户都有执行权限,且其它用户有写权限的文件
find /etc/init.d -perm -113 -ls
以上纯属个人兴趣爱好编辑,难免有所不足。如有不同意见,请联系QQ1930818140,欢迎大家前来咨询交流!