find

一、简介

1.find默认操作是打印出文件和目录,但是最好使用-print选项来指定

2.find若要传给xagrs命令,则应使用-print0选项来指定,此时分隔符由\n变为\0

3.find  路径  选项+参数

 

 

二、技巧

1.利用正则表达搜索

[root@localhost 1]# ll
total 0
-rw-r--r-- 1 root root 0 Apr  6 19:30 file1.txt
-rw-r--r-- 1 root root 0 Apr  6 19:31 FILE1.txt
-rw-r--r-- 1 root root 0 Apr  6 19:30 file2.txt
-rw-r--r-- 1 root root 0 Apr  6 19:31 FILE2.txt
[root@localhost
1]# find -name "*1*" -print ./file1.txt ./FILE1.txt
[root@localhost
1]# find -iname '*2*' -print ./file2.txt ./FILE2.txt

说明:

  (1)-name  文件名

  (2)-iname  忽略字母大小写

  (3)Shell会扩展没有引号或双引号" "中的通配符,单引号‘ ’能阻止Shell扩展,使字符串原封不动地传给find命令。

2.否定参数

[root@localhost 1]# ll
total 0
-rw-r--r-- 1 root root 0 Apr  6 19:30 file1.txt
-rw-r--r-- 1 root root 0 Apr  6 19:31 File1.txt
-rw-r--r-- 1 root root 0 Apr  6 19:30 file2.txt
-rw-r--r-- 1 root root 0 Apr  6 19:31 FILE2.txt
[root@localhost
1]# find -iname '*2.txt' ./file2.txt ./FILE2.txt
[root@localhost
1]# find ! -iname '*2.txt' . ./file1.txt ./File1.txt

说明

  (1)find采用排除法时可用!。一条语句可以使用多个!,只要!在该选项前面即可。

3.基于目录深度搜索

[root@localhost 1]# find -L /proc/ -maxdepth 1 -name 'bundleamaker.def' 2> /dev/null 
[root@localhost 1]# ll
total 0
lrwxrwxrwx 1 root root 1 Apr  6 19:57 1 -> 3
lrwxrwxrwx 1 root root 1 Apr  6 19:56 2 -> 1
lrwxrwxrwx 1 root root 1 Apr  6 19:57 3 -> 2

[root@localhost 1]# find -L . -name "1" find: ‘./2’: Too many levels of symbolic links find: ‘./3’: Too many levels of symbolic links find: ‘./1’: Too many levels of symbolic links

说明:

  (1)-L  跟随符号链接。注意:若碰上了指向自身的链接,find就会陷入死循环中。

  (2)-maxdepth  最大目录深度。本例,搜索范围限制在当前目录。

  (3)-mindepth   最小目录深度。用于查找较深的文件。

  (4)maxdepth和mindepth选项要靠前,从而保证效率。

4.根据文件类型搜索

[root@localhost 1]# find . -type f -print
[root@localhost 1]# find . -type d -print
[root@localhost 1]# find . -type l -print

说明:

  (1)-f   文件

  (2)-d  目录

  (3)-l   符号链接

  (4)-b  块设备

  (5)-p  FIFO

  (6)-c  字符设备

  (7)-s  套接字

5.根据文件时间戳搜索

[root@localhost 1]# find . -type f -atime -1 -print    #打印最近1天内被访问过的文件
[root@localhost 1]# find . -type f -atime  1 -print  #打印恰好在1天前被访问的文件
[root@localhost 1]# find . -type f -atime +1 -print  #打印访问时间超过1天的文件

说明:

  (1)-atime:The data was referenced but unchanged.【使用ls -lu查看文件的atime】

  (2)-mtime:The data inside was amended or deleted or added.【使用ls -l查看文件的mtime】

  (3)-ctime:The medata was changed, such as permission or owner was changed.【使用ls -lc查看文件的ctime】

  (4)均以“天”为单位,也可以使用amin/mmin/cmin以“分钟”为单位。

  (5)-表示小于、+表示大于,不带符号表示恰好这一天。

[root@localhost 1]# ll
total 12
-rw-r--r-- 1 root root 3 Apr  6 21:59 1
-rw-r--r-- 1 root root 2 Apr  6 21:59 2
-rw-r--r-- 1 root root 2 Apr  6 21:59 3

[root@localhost 1]# find . -type f -newer 1 -print ./2 ./3

说明: 

  (5)-newer  指定一个用于比较mtime的参考文件,然后找出比它更新mtime的文件。

6.基于文件大小搜索

[root@localhost 1]# find . -type f -size -1k  #文件小于1k
[root@localhost 1]# find . -type f -size  1k  #文件等于1k
[root@localhost 1]# find . -type f -size +1k  #文件大于1k

说明:

  (1)k   1024字节

  (2)M  1024k字节

  (3)G  1024M字节

  (4)c   1字节

  (5)w  2字节=1个字

  (6)b  512字节=1个块

7.基于文件权限和所有权搜索

[root@localhost 1]# find . -type f -perm 755 -print

[root@localhost 1]# find . -type f -user root -print

说明:

  (1)-perm  文件权限

  (2)user    文件所有者。当然也可以用UID

8.删除和执行其他操作

 
[root@localhost 1]# find . -type f -name "*.txt" -delete

[root@localhost 1]# find . -type f -user root -exec chown othe {} \;
[root@localhost 1]# ll
total 12
-rwxr-xr-x 1 othe root 3 Apr  6 21:59 1
-rw-r--r-- 1 othe root 2 Apr  6 21:59 2
-rw-r--r-- 1 othe root 2 Apr  6 21:59 3

[root@localhost 1]# find . -type f -group root -exec chgrp othe {} \;
[root@localhost 1]# ll
total 12
-rwxr-xr-x 1 othe othe 3 Apr  6 21:59 1
-rw-r--r-- 1 othe othe 2 Apr  6 21:59 2
-rw-r--r-- 1 othe othe 2 Apr  6 21:59 3

[root@localhost 1]# find . -type f -group root -exec chgrp othe {} +

说明;

  (1)-delete  删除

  (2)-exec  

      It is a builtin command of the Bash Shell. It allows u to execute a command, which compeletly replaces current process. The current process is destoryed and entirely replaced by your specified command.

      { } expands to the output "find"

      ; ends the command "exec", which \ escape the semicolon ;

      + also can ends the command "exec", which -exec can receive multiple parameters to get One-time execution(执行时间更短)

 

[root@localhost 1]# time  find . -type f -group othe -exec chgrp root {} +

real    0m0.027s
user    0m0.006s
sys    0m0.020s
[root@localhost 1]# ll
total 12
-rwxr-xr-x 1 othe root 3 Apr  6 21:59 1
-rw-r--r-- 1 othe root 2 Apr  6 21:59 2
-rw-r--r-- 1 othe root 2 Apr  6 21:59 3
[root@localhost 1]# time  find . -type f -group root -exec chgrp othe {} \;

real    0m0.055s
user    0m0.017s
sys    0m0.031s
[root@localhost 1]# ll
total 12
-rwxr-xr-x 1 othe othe 3 Apr  6 21:59 1
-rw-r--r-- 1 othe othe 2 Apr  6 21:59 2
-rw-r--r-- 1 othe othe 2 Apr  6 21:59 3

 

      + also can ends the command "exec", which -exec can receive multiple parameters to get One-time execution(执行时间更短)

 

[root@localhost 1]# find . -type f -group root -exec ./command.sh  {} +

      exec只能接受单个命令,若要在find -exec 后使用多个命令,可以将它们写到一个shell脚本中

 

[root@localhost 1]# find . -type f -name "*.txt" -exec printf "Text file: %s\n" {} +
Text file: ./file1.txt
Text file: ./file2.txt
Text file: ./file3.txt

         exec搭配-print来生成输出信息,print的使用和C语言一样(%s表示字符串)

 

[root@localhost 1]# find . -type f -name "*.txt" -exec cat {} > all_txt_file.txt +
cat: ./all_txt_file.txt: input file is output file
[root@localhost 1]# cat all_txt_file.txt 
1
2
3

         find的全部输出只有一个数据流,因此此处可以不使用>>符号 

9.跳过特定目录 

[root@localhost 2]# find .  -type f -print
./.git/file5.txt
./.git/file6.txt
./directoey/file3.txt
./directoey/file4.txt
./file1.txt
./file2.txt
[root@localhost
2]# find . -name "*.git" -prune -o -type f -print ./directoey/file3.txt ./directoey/file4.txt ./file1.txt ./file2.txt

说明:

  (1)-prune  修剪。If the file is a directory, do not descend into it. 该选项一定要放到前面去!

  (2)-o    或操作。本例中,.git目录不一定存在,所以使用关系。其实我也不是非常肯定.....

  (3)这条语句的意思是:跳过.git目录,打印.路径下的所有文件名

  (4)只针对目录,不针对文件跳过!

 
posted @ 2020-04-06 23:39  3月の狮子  阅读(248)  评论(0编辑  收藏  举报