NAME
find - search for files in a directory hierarchy
按目录层级结构搜索文件
SYNOPSIS
find pathname -options [-print -exec -ok ...]
OPTIONS
一、按文件名查找
-name pattern
按文件名查找,支持globbing字符
-iname “文件名称”
按文件名查找,忽略大小写,支持glob风格的通配符。
-links n
File has n links 链接数为n的文件
-samefile name
相同inode号的文件 File refers to the same inode as name.
-regex "PATTERN"
以emacs的PATTERN匹配文件全路径名,而不仅仅是名字,使用该选项时,可用-regextype type选项改变支持的PATTERN类型
-regextype type:
Changes the regular expression syntax understood by -regex and -iregex tests which occur later on the command line. Currently-implemented types are emacs (this is the default), posix-awk, posix-basic, posix- egrep and posix-extended.
例:
find ./ -name ip 查找当前目录下文件名为ip的文件或目录。
find ./ -iname ip 查找当前目录下文件名为ip的文件或目录,并忽略大小写。
find ./ -iname “ip?” 查找当前目录下文件名为ip后跟任意字符的文件或目录,并忽略大小写
find ./ -iname “ip*” 查找当前目录下文件名为ip后跟任意长度任意字符的文件或目录,并忽略大小写。
find -regex ".*/.*.conf" 查找当前目录下后辍名为.conf的文件
二、按目录层级搜索
-maxdepth level
最大搜索目录深度,指定目录为第1级。
-mindepth level
最大搜索目录深度,指定目录为第1级。
[root@promote tmp]# mkdir 1/2/3/4/5/6/7/8 -pv #创建测试文件
mkdir: 已创建目录 "1"
mkdir: 已创建目录 "1/2"
mkdir: 已创建目录 "1/2/3"
mkdir: 已创建目录 "1/2/3/4"
mkdir: 已创建目录 "1/2/3/4/5"
mkdir: 已创建目录 "1/2/3/4/5/6"
mkdir: 已创建目录 "1/2/3/4/5/6/7"
mkdir: 已创建目录 "1/2/3/4/5/6/7/8"
[root@promote tmp]# touch 1/a
[root@promote tmp]# touch 1/2/b
[root@promote tmp]# touch 1/2/3/c
[root@promote tmp]# touch 1/2/3/4/d
[root@promote tmp]# touch 1/2/3/4/5/e
[root@promote tmp]# touch 1/2/3/4/5/6/f
[root@promote tmp]# touch 1/2/3/4/5/6/7/g
[root@promote tmp]# touch 1/2/3/4/5/6/7/8/h
[root@promote tmp]# find -mindepth 6 #查找最小层级为6的文件
./1/2/3/4/5/6
./1/2/3/4/5/6/7
./1/2/3/4/5/6/7/8
./1/2/3/4/5/6/7/8/h
./1/2/3/4/5/6/7/g
./1/2/3/4/5/6/f
./1/2/3/4/5/e
[root@promote 1]# find -maxdepth 4 #查找最大层级为4的文件
.
./2
./2/3
./2/3/4
./2/3/4/5
./2/3/4/d
./2/3/c
./2/b
./a
[root@promote 1]#
三、按文件属主、属组查找
-user USERNAME :根据文件属主查找 File is owned by user uname
-group GROUPNAME :根据文件属组查找 File belongs to group gname
-uid n :根据指定UID查找文件或目录 File's numeric user ID is n
-gid n :根据指定GID查找文件或目录 File's numeric group ID is n.
-nouser :查找没有属主的文件或目录 No user corresponds to file's numeric user ID.
-nogroup :查找没有属组的文件或目录 No group corresponds to file's numeric group ID.
~]# find / -user basher #在根目录下查找文件属主为basher的文件或目录
~]# find / -group mail #在根目录下查找文件属组为mail的文件或目录
~]# find / -uid 1000 #在根目录下查找uid为1000的文件或目录
~]# find / -gid 1000 #在根目录下查找gid为1000的文件或目录
~]# find / -nouser #在根目录下查找没有属主的文件或目录
~]# find / -nogroup #在根目录下查找没有属组的文件或目录
四、按文件类型查找
-type TYPE
TYPE类型如下:
f:普通文件 d:目录文件 l:符号链接 b:块设备 c:字符设备 s:套接字文件 p:命名管道
五、按文件大小查找
-size [+|-]#UNIT
常用单位:k、M、G 不加单位默认单位为字节
#UNIT:匹配区间(#-1, #]
-#UNIT:匹配区间[0,#-1]
+#UNIT:匹配区间(#,∞)
~]# find /etc -size 5M #在/etc目录下查找5M大小的文件(查找结果中文件的大小为大于4M,小于等于5M。)
~]# find /etc -size -5M #在/etc目录下查找小于5M的文件(查找结果中文件小于等于4M。)
~]# find /etc -size +5M #在/etc目录下查找大于5M的文件(查找结果中文件大于5M。)
六、按时间戳查找
以天为单位:
访问时间:-atime[+|-]#
#:[#, #-1) #天前那一天
-#:(#, 0] #天内
+#:(∞, #-1] #天前
修改时间:-mtime[+|-]#
改变时间:-ctime[+|-]#
以分钟为单位
访问时间:-amin[+|-]#
修改时间:-mmin[+|-]#
改变时间:-cmin[+|-]#
[root@promote 1]# find / -maxdepth 2 -mtime 7 -ls #查找7天前那一天修改过的文件
21792 0 drwx------ 3 user4 user4 74 1月 19 16:08 /home/user4
21796 0 drwx------ 3 user5 user5 74 1月 19 16:08 /home/user5
21800 0 drwx------ 3 user6 user6 74 1月 19 16:08 /home/user6
21804 0 drwx------ 3 user7 user7 74 1月 19 16:08 /home/user7
21808 0 drwx------ 3 user8 user8 74 1月 19 16:08 /home/user8
21812 0 drwx------ 3 user9 user9 74 1月 19 16:08 /home/user9
[root@promote 1]# find / -maxdepth 1 -mtime -5 | xargs ls -ld #查找最近五天更改过的文件
dr-xr-xr-x. 21 root root 4096 1月 26 16:10 /
drwxr-xr-x. 3 root root 22 1月 26 16:11 /1
drwxr-xr-x. 20 root root 3240 1月 24 22:34 /dev
drwxr-xr-x. 130 root root 8192 1月 25 02:37 /etc
drwxr-xr-x. 37 root root 4096 1月 23 01:29 /home
dr-xr-xr-x. 440 root root 0 1月 24 22:34 /proc
dr-xr-x---. 15 root root 4096 1月 24 17:11 /root
drwxr-xr-x. 35 root root 1080 1月 24 14:42 /run
dr-xr-xr-x. 13 root root 0 1月 24 22:34 /sys
drwxrwxrwt. 4 root root 25 1月 26 19:32 /tmp
drwxr-xr-x. 20 root root 4096 1月 24 22:34 /var
[root@lxk tmp]# find -mtime +1 -ls #查找一天前修改过的文件
819205 4 drwxrwxrwt 2 root root 4096 Feb 24 2017 ./.font-unix
819218 4 -rw-r--r-- 1 root root 77 Jan 24 23:47 ./excute.sh
819203 4 drwxrwxrwt 2 root root 4096 Feb 24 2017 ./.X11-unix
819204 4 drwxrwxrwt 2 root root 4096 Feb 24 2017 ./.XIM-unix
819207 4 drwxrwxrwt 2 root root 4096 Feb 24 2017 ./.Test-unix
819206 4 drwxrwxrwt 2 root root 4096 Feb 24 2017 ./.ICE-unix
七、根据权限查找:
-perm[/|-]MODE
MODE:与权限精确匹配
/MODE:任何一类用户的任何一个权限满足条件即匹配,权限之间存在或的关系
Any of the permission bits mode are set for the file
-MODE:每一类用户的每个权限都要满足条件才匹配
All of the permission bits mode are set for the file.
八、组合查找测试
-a :与关系,需条件同时满足。
-o :或关系,满足一个即可。
-not 或 ! :查找条件取反
[root@promote tmp]# mkdir test
[root@promote tmp]# cd test
[root@promote test]# touch a b c d e f g
[root@promote test]# chmod 640 a
[root@promote test]# chmod 666 b
[root@promote test]# chmod 440 c
[root@promote test]# chmod 775 d
[root@promote test]# chmod 777 e
[root@promote test]# ll
总用量 0
-rw-r-----. 1 root root 0 1月 26 15:26 a
-rw-rw-rw-. 1 root root 0 1月 26 15:26 b
-r--r-----. 1 root root 0 1月 26 15:26 c
-rwxrwxr-x. 1 root root 0 1月 26 15:26 d
-rwxrwxrwx. 1 root root 0 1月 26 15:26 e
-rw-r--r--. 1 root root 0 1月 26 15:26 f
-rw-r--r--. 1 root root 0 1月 26 15:26 g
[root@promote test]# find -perm 644 -ls #权限精确查找
205828677 0 -rw-r--r-- 1 root root 0 1月 26 15:26 ./f
205828679 0 -rw-r--r-- 1 root root 0 1月 26 15:26 ./g
[root@promote test]# find -perm /644 -ls #任何一类用户的任何一个权限满足条件即匹配
202038490 0 drwxr-xr-x 2 root root 62 1月 26 15:26 .
203159056 0 -rw-r----- 1 root root 0 1月 26 15:26 ./a
205703301 0 -rw-rw-rw- 1 root root 0 1月 26 15:26 ./b
205828672 0 -r--r----- 1 root root 0 1月 26 15:26 ./c
205828675 0 -rwxrwxr-x 1 root root 0 1月 26 15:26 ./d
205828676 0 -rwxrwxrwx 1 root root 0 1月 26 15:26 ./e
205828677 0 -rw-r--r-- 1 root root 0 1月 26 15:26 ./f
205828679 0 -rw-r--r-- 1 root root 0 1月 26 15:26 ./g
[root@promote test]# find -perm -222 -ls #每一类用户的每个权限都要满足条件才匹配
205703301 0 -rw-rw-rw- 1 root root 0 1月 26 15:26 ./b
205828676 0 -rwxrwxrwx 1 root root 0 1月 26 15:26 ./e
[root@promote test]# find ! -perm -222 -ls #任意一类用户有没有写权限都行?
202038490 0 drwxr-xr-x 2 root root 62 1月 26 15:26 .
203159056 0 -rw-r----- 1 root root 0 1月 26 15:26 ./a
205828672 0 -r--r----- 1 root root 0 1月 26 15:26 ./c
205828675 0 -rwxrwxr-x 1 root root 0 1月 26 15:26 ./d
205828677 0 -rw-r--r-- 1 root root 0 1月 26 15:26 ./f
205828679 0 -rw-r--r-- 1 root root 0 1月 26 15:26 ./g
[root@promote test]# find -perm /222 -ls #至少一类用户有写权限
202038490 0 drwxr-xr-x 2 root root 62 1月 26 15:26 .
203159056 0 -rw-r----- 1 root root 0 1月 26 15:26 ./a
205703301 0 -rw-rw-rw- 1 root root 0 1月 26 15:26 ./b
205828675 0 -rwxrwxr-x 1 root root 0 1月 26 15:26 ./d
205828676 0 -rwxrwxrwx 1 root root 0 1月 26 15:26 ./e
205828677 0 -rw-r--r-- 1 root root 0 1月 26 15:26 ./f
205828679 0 -rw-r--r-- 1 root root 0 1月 26 15:26 ./g
[root@promote test]# find ! -perm /222 -ls #所有用户都没有写权限
205828672 0 -r--r----- 1 root root 0 1月 26 15:26 ./c
查找没有属主并且没有属组的文件:
写法一:
[root@promote ~]# find /tmp -nouser -a -nogroup -ls
1767500 0 -rw-rw-r-- 1 1032 1032 0 1月 27 19:31 /tmp/a
1767517 0 -rw-rw-r-- 1 1032 1032 0 1月 27 19:31 /tmp/b
3412681 0 -rw-rw-r-- 1 1032 1032 0 1月 27 19:31 /tmp/c
3412685 0 -rw-rw-r-- 1 1032 1032 0 1月 27 19:31 /tmp/e
3412686 0 -rw-rw-r-- 1 1032 1032 0 1月 27 19:31 /tmp/f
写法二:神经病写法
[root@promote ~]# find /tmp ! ! \( -nouser -o -nogroup \) -ls
1767500 0 -rw-rw-r-- 1 1032 1032 0 1月 27 19:31 /tmp/a
1767517 0 -rw-rw-r-- 1 1032 1032 0 1月 27 19:31 /tmp/b
3412681 0 -rw-rw-r-- 1 1032 1032 0 1月 27 19:31 /tmp/c
3412685 0 -rw-rw-r-- 1 1032 1032 0 1月 27 19:31 /tmp/e
3412686 0 -rw-rw-r-- 1 1032 1032 0 1月 27 19:31 /tmp/f
九、处理动作
-print:输出至标准输出,默认动作。
-ls :类似于对查找到的文件执行ls –l的操作,以长列表形式显示查找到的文件或目录。
-delete:删除查找到的文件(危险操作!!!)
-fls /path/to/somefile:把查找到的文件的长列表信息输出至指定文件。
-ok COMMAND {} \; :对查找到的文件执行COMMAND操作(交互式)
-exec COMMAND {} \; :对查找到的文件执行command操作(非交互式)
注:
find传递查找到的文件路径至后面的命令时,是先查找出所有符合条件的文件路径,并一次性传递给后面的命令;
但是有些命令不能接受过长的参数,此时命令执行会失败;使用xargs可规避此问题:find | xargs COMMAND
[root@localhost ~]# find /tmp -type f | xargs ls -l 查找/tmp下普通文件并通过xargs传递给ls长列表列出
-rw-r--r-- 1 root root 5 Jan 17 17:25 /tmp/a
-rw-r--r-- 1 root root 11 Jan 17 17:31 /tmp/b
-rw-r--r-- 1 root root 114 Jan 17 17:33 /tmp/c.
[root@localhost tmp]# find /tmp -type f -exec mv {} {}.txt \; 对/tmp下文件添加后辍名.txt(非交互式)
[root@localhost tmp]# ls
a.txt b.txt c.txt
[root@localhost tmp]# find /tmp -type f -ok rm -rf {} \; 查找/tmp下的普通文件并删除(交互式)
< rm ... /tmp/c.txt > ? y
< rm ... /tmp/a.txt > ? y
< rm ... /tmp/b.txt > ? y
转载于:https://blog.51cto.com/11975865/2065861