find

  语法格式:find [查找路径] [条件选项] [处理动作]

    查找路径:默认是当前路径

AB


①    条件选项:

         1.1文件名

         1.2用户信息

         1.3文件类型

         1.4文件大小

         1.5时间信息

         1.6文件权限

②    处理动作:

 例子:find实例

find /usr -name pxelinux.0
find /usr/share -name pxe*
find /usr -name pxelinux.?
tail /etc/passwd
find / -user apache
find / -user named
find / -inum 1016273 --exec rm -fr {} ;
find / -size +204800 -exec rm -fr {} ;
find /etc -name init* -ok ls -l {} ;
find / -size 204800
# (计算单位是块)
find / -size +204800    
find / -perm 777 -exec ls -l {} ;
find / -perm +1 -exec ls -l {} ;
# (两天内修改过属性的文件)
find /etc -ctime -2        
# (改得多、改乱了……)
find /etc -mmin 30        
find / -type f -size -2048 -perm 755 -exec ls -l {} ;
View Code

1.条件选项 


1.1 文件名称查找

选项例子例子说明
-name -name "passwd" 严格区分文件名的大小写
-iname -iname "inittab" 不区分大小写

例子:

[root@hp430G2 mag]# find -name "file*"
./file1.txt
[root@hp430G2 mag]# find -iname "file*"
./FILE1.TXT
./file1.txt
View Code

 

1.2 用户信息查找

选项例子例子说明
-user -user wls 针对wls用户
-group -group bea  
-uid -uid 502 用户删除后仅仅显示其id号
-gid -gid 502  
-nouser -nouser 查找没有用户的文件
-nogroup -nogroup  

例子:

[root@hp430G2 data01]# find -user wls -name "samples.jar"
./wls/samples.jar
[root@hp430G2 data01]# find -group wls -name "samples.jar"
./wls/samples.jar
View Code

 

1.3 依据文件类型查找

-type 选项例子例子说明
f -type f 查找普通文件类型
s -type s 查找套接字文件

 

  type 查找某一类型的文件,如

           b - 块设备文件  d - 目录  c - 字符设备文件

           p - 管道文件  l - 符号链接文件  f - 普通文件。

# find /tmp -type s -exec ls -l {} ;

 

1.4 根据文件大小查找

选项例子例子说明
-size

-size 2

-size +2

-size +50M

2kB大小文件

大于2KB

50兆字节以上的文件

[root@hp430G2 soft]# find -size +80M
./gcc-6.1.0.tar.bz2

  找到的是大于80M的文件

  搜索大小

  计算单位是块大小,块大小在格式化的时候确定;如果没有人为指定、修改过,就是默认值; 面对大存储的时候,有可能是修改过的;

1.5 时间信息查找

选项例子例子说明
-atime -atime +5 最后访问时间在5天前
-ctime -ctime -1 24小时内设置过的文件
-mtime -mtime -7 最后修改时间在一周前的文件
-amin    
-cmin    
-mmin -mmin 5 刚刚修改过的文件(5分钟)
-newer FILE   比FILE文件的修改时间更近

 

       time    天

       min    分钟

       c 属性改变;    a 访问(浏览);    m 内容修改(编辑)

       -----------> ctime,atime,mtime;cmin,amin,mmin

  查看一个文件的三个时间

[root@hp430G2 mag]# stat deluser.sh
  File: `deluser.sh'
  Size: 756             Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d      Inode: 4465        Links: 1
Access: (0700/-rwx------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-10-20 01:46:11.018999925 +0800
Modify: 2016-10-18 19:33:03.994297982 +0800
Change: 2016-10-18 19:33:04.006297741 +0800
View Code

  干工程时,事先修改好的文档,另外一个同事部署过一次……

NARI:~# ls -l
-rwxrwxrwx    1 root     root       8147155 Jul 27 15:59 PDZ800
-rwxrwxrwx    1 root     root           736 Nov  2  2015 addStartUp.sh
-rwxrwxrwx    1 root     root            89 Aug 25 11:36 netinit
-rwxrwxrwx    1 root     root            68 Oct 23 16:21 ping.sh
-rwxrwxrwx    1 root     root           106 Jul  1  2014 setip.sh
NARI:~# stat PDZ800 
  File: PDZ800
  Size: 8147155         Blocks: 15920      IO Block: 4096   regular file
Device: dh/13d  Inode: 4969        Links: 1
Access: (0777/-rwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-09-02 16:45:24.000000000
Modify: 2016-07-27 15:59:10.000000000
Change: 2016-10-24 08:16:37.000000000
View Code

  刚刚修改过的文件(5分钟)

[root@hp430G2 mag]# find -mmin -5
.
./adduseer.sh

1.6 依据文件权限搜索

选项举例子例子说明
-perm

-perm 060

-perm -060

-perm /060

精确匹配(只能有4、2,而且左右的0也影响)

最小匹配(060和070;必须有4和2)

模糊匹配(有2或有4即可)

 

  精确匹配时,查到的文件是不同的

[root@hp430G2 perm]# ll
total 0
-rwxrwxrwx 1 root root 0 Oct 20 15:15 file0.p
-rwxr-xr-x 1 root root 0 Oct 20 15:11 file1.p
-rw-r--r-- 1 root root 0 Oct 20 15:12 file2.p
-rw------- 1 root root 0 Oct 20 15:12 file3.p
-rw-rw---- 1 root root 0 Oct 20 15:20 file4.p
[root@hp430G2 perm]# find -perm 060
[root@hp430G2 perm]# find -perm 660
./file4.p
[root@hp430G2 perm]# find -perm -660
./file4.p
./file0.p
[root@hp430G2 perm]# find -perm 760
[root@hp430G2 perm]# find -perm /060
.
./file4.p
./file2.p
./file0.p
./file1.p
View Code

  查找suid文件

find / -perm /7000
find / -perm /7000 -exec ls -l {} ;
View Code

1.7 根据深度查找

选项举例子例子说明
-maxdepth -maxdepth 3 目录深度为 3

例子:查找根目录下当前用户的目录

$ find /  -maxdepth 2 -type d -user argor 2>/dev/null
/www
/www/java
/www/html
/www/man
/www/baidu
/www/jsp
/www/download
/www/VHosts
/www/xml
/baks
/baks/www
/baks/lost+found
/baks/optwww
/baks/nginx-1.8.1
/home/argor
/proc/8692
/proc/8694
/proc/27631
/proc/27632
/proc/27633
/proc/27634
/proc/27635
/proc/29838
/proc/30297
/proc/30298
/proc/30385
/opt/www
/opt/maven_repo
/tmp/hsperfdata_argor
View Code

2.处理动作


选项例子例子解释
-exec -exec cmd {} ; 命令就是系统支持的任意命令
-ok -ok cmd {} ; 执行时需要交互确认
-ls -ls 效果像同:ls -dils
-delete   删除文件

修改权限

[root@hp430G2 perm]# find -perm -001 -ls
  4486    0 -rwxrwxrwx   1 root     root            0 Oct 20 15:15 ./file0.p
  4473    0 -rwxr-xr-x   1 root     root            0 Oct 20 15:11 ./file1.p
[root@hp430G2 perm]# find -perm -001 -exec chmod o-x {} ;
[root@hp430G2 perm]# ls -l file[0,1]*
-rwxrwxrw- 1 root root 0 Oct 20 15:15 file0.p
-rwxr-xr-- 1 root root 0 Oct 20 15:11 file1.p

对比ls命令的效果

# find / -name "home" -ls
find: ‘/run/user/1000/gvfs’: Permission denied
201327271    0 drwxr-xr-x   6 root     root          103 Dec 27 13:02 /home
205507217    4 -rw-------   1 donatello donatello      228 Dec 27 09:01 /home/donatello/.local/share/gvfs-metadata/home
# ls -dils /home/
201327271 0 drwxr-xr-x. 6 root root 103 Dec 27 13:02 /home/

备份文件

[root@hp430G2 perm]# find ./ -name "file*.p" -exec mv {} {}.bak ;

  复制到当前目录下

[root@hp430G2 perm]# find /data01/nari/tomcat/ -name "catalina.out" -exec cp {} . ;

引入“xargs

$ find /tmp -name core -type f -print | xargs /bin/rm -f
$ find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f
$ find /tmp -depth -name core -type f -delete
$ cut -d: -f1 < /etc/passwd | sort | xargs echo
$ xargs sh -c ’emacs "$@" < /dev/tty’ emacs
一切代码都是为了生活,一切生活都是调剂
原文地址:https://www.cnblogs.com/argor/p/7910804.html