Linux --find 实例详解

          LINUX 文件查找是有接触过LINUX的人都会碰到的内容,如何快速精准的定位到我们所需要的文件呢?采用find是一个不错的办法

LINUX 常用的文件查找有find , locate , 那这两者有什么区别呢?

          1. locate  非实时查找,它依赖于文件数据库,这个数据库由(crontab)来创建更新,可以手动updatedb生成数据库,这个过程会比较慢。但locate也有它的优点,查询速度快

         2. find  实时查找,精确,可以遍历指定目录中所有文件来完成查找,而且支持很多的查找标准和正则。因为是实时查找,所以同locate相比,速度会慢些。但是它功能强大,所以我们这里重点也只介绍find的一些常见用法。


1. 查找格式

 find  查找路径 查找标准  查找后的action

 默认值:查找路径默认当前目录, 查找标准默认为所有, action 默认为显示打印


2. 匹配标准

(1)   -name  : ‘FILENAME’ 根据文件名精确查找 , 区分大小写, 文件名可以是通配符 * ?[ ] 等。 

           -iname :     同name,不区分大小写

eg1

[root@localhost find_tdir]# ls
tdir1  Tdir1  tdir2  tfile1  Tfile1  tfile2
[root@localhost find_tdir]# find -name "tfile1"
./tfile1
[root@localhost find_tdir]# find -iname "tdir1"
./tdir1
./Tdir1
[root@localhost find_tdir]# find -name "*dir1"
./tdir1
./Tdir1
    (2)  -regex  :基于正则表达式进行的文件名匹配

eg2

[root@localhost find_tdir]# find -regex ".*dir.*"
./tdir2
./tdir1
./Tdir1
  (3)-user , -group , -uid , -gid , -nouser , -nogroup  :分别是居于用户名,组名,用户IP,组ID,没有属主,没有属组 查找文件

eg3

[root@localhost find_tdir]# ls -lh

总用量 12K
drwxr-xr-x 2 root root 4.0K 5月  10 10:22 tdir1
drwxr-xr-x 2 root root 4.0K 5月  10 10:23 Tdir1
drwxr-xr-x 2 root root 4.0K 5月  10 10:22 tdir2
-rw-r--r-- 1 pylt pylt    0 5月  10 10:21 tfile1  #属主,属组都是pylt
-rw-r--r-- 1 root pylt    0 5月  10 10:23 Tfile1  #属组是pylt
-rw-r--r-- 1 root root    0 5月  10 10:21 tfile2
[root@localhost find_tdir]# id root  #id 查看用户账号uid ,gid 等信息
uid=0(root) gid=0(root) 组=0(root)
[root@localhost find_tdir]# id pylt
uid=501(pylt) gid=501(pylt) 组=501(pylt)
[root@localhost find_tdir]# find -user pylt
./tfile1
[root@localhost find_tdir]# find -group pylt
./tfile1
./Tfile1
[root@localhost find_tdir]# find -uid 0
.
./tfile2
./tdir2
./tdir1
./Tdir1
./Tfile1
[root@localhost find_tdir]# find -gid 501
./tfile1
./Tfile1
我把pylt的用户和组删除一下,就会产生一些没有用户和组的文件

[root@localhost find_tdir]# ll -lh
总用量 12K
drwxr-xr-x 2 root root 4.0K 5月  10 10:22 tdir1
drwxr-xr-x 2 root root 4.0K 5月  10 10:23 Tdir1
drwxr-xr-x 2 root root 4.0K 5月  10 10:22 tdir2
-rw-r--r-- 1  501  501    0 5月  10 10:21 tfile1
-rw-r--r-- 1 root  501    0 5月  10 10:23 Tfile1
-rw-r--r-- 1 root root    0 5月  10 10:21 tfile2
[root@localhost find_tdir]# find -nouser 
./tfile1
[root@localhost find_tdir]# find -nogroup
./tfile1
./Tfile1
 (4) -type :根据文件类型查找

              f --> 文件  ,  d-->目录 ,  c -->字符设备, b --> 块设备,  l -->链接文件, p-->管道文件,  s--> socket文件

下面就简单举两个例子

eg4

[root@localhost find_tdir]# find -type f
./tfile2
./tfile1
./Tfile1
[root@localhost find_tdir]# find -type d
.
./tdir2
./tdir1
./Tdir1
   (5) -size : 根据文件大小查找,可以直接跟单位K,M,G, 不跟单位则默认是字节

+ 5K  :表示大于5K ;-  5K  : 表示小于5K ;   5K  : 表示等于5K

我用dd创建固定大小的文件,下面我们看下实例:

eg5

[root@localhost find_tdir]# ls -lh
总用量 44K
-rw-r--r-- 1 root root  20K 5月  10 11:02 ddf1
-rw-r--r-- 1 root root  10K 5月  10 11:03 ddf2
drwxr-xr-x 2 root root 4.0K 5月  10 10:22 tdir1
drwxr-xr-x 2 root root 4.0K 5月  10 10:23 Tdir1
drwxr-xr-x 2 root root 4.0K 5月  10 10:22 tdir2
-rw-r--r-- 1  501  501    0 5月  10 10:21 tfile1
-rw-r--r-- 1 root  501    0 5月  10 10:23 Tfile1
-rw-r--r-- 1 root root    0 5月  10 10:21 tfile2
[root@localhost find_tdir]# find -size 10k
./ddf2
[root@localhost find_tdir]# find -size +10k
./ddf1
[root@localhost find_tdir]# find -size -10k
.
./tfile2
./tdir2
./tdir1
./Tdir1
./tfile1
./Tfile1
   (6) 条件组合

      - a  相当于 &&  :条件 同时满足

      -o  相对于 || :任意一个条件满足

      -not  相对于 !: 否定

eg6

[root@localhost find_tdir]# ls -lh
总用量 44K
-rw-r--r-- 1 root root  20K 5月  10 11:02 ddf1
-rw-r--r-- 1 root root  10K 5月  10 11:03 ddf2
drwxr-xr-x 2 root root 4.0K 5月  10 10:22 tdir1
drwxr-xr-x 2 root root 4.0K 5月  10 10:23 Tdir1
drwxr-xr-x 2 root root 4.0K 5月  10 10:22 tdir2
-rw-r--r-- 1  501  501    0 5月  10 10:21 tfile1
-rw-r--r-- 1 root  501    0 5月  10 10:23 Tfile1
-rw-r--r-- 1 root root    0 5月  10 10:21 tfile2
[root@localhost find_tdir]# find -type f -a -size +10k
./ddf1
[root@localhost find_tdir]# find -not -type d
./ddf1
./tfile2
./tfile1
./ddf2
./Tfile1
[root@localhost find_tdir]# find -size +10k -o -type d
.
./ddf1
./tdir2
./tdir1
./Tdir1
     (7)  根据时间戳格式匹配

           -atime 访问时间

           -mtime 修改时间

           -ctime 改变时间

 + 5 代表大于5天 ;  -5   代表5天之内 ;   5  代表刚好5天

我用touch 修改ddf2的mtime 为04041212

eg7 

[root@localhost find_tdir]# touch -t 04041212 ddf2
[root@localhost find_tdir]# stat ddf1
  File: "ddf1"
  Size: 20480     	Blocks: 40         IO Block: 4096   普通文件
Device: fd00h/64768d	Inode: 137517      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2014-05-10 11:02:52.153882782 +0800
Modify: 2014-05-10 11:02:52.154906549 +0800
Change: 2014-05-10 11:02:52.154906549 +0800
[root@localhost find_tdir]# stat ddf2
  File: "ddf2"
  Size: 10240     	Blocks: 24         IO Block: 4096   普通文件
Device: fd00h/64768d	Inode: 137518      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2014-04-04 12:12:00.000000000 +0800
Modify: 2014-04-04 12:12:00.000000000 +0800
Change: 2014-05-10 11:26:24.074880282 +0800
[root@localhost find_tdir]# 
[root@localhost find_tdir]# find -mtime +7
./ddf2
[root@localhost find_tdir]# find -mtime -7 -a -size +10k
./ddf1
   (8) 根据文件权限查找

-perm   644  精确匹配 ;  /644  只有一位匹配就满足 ;  -644  文件权限完全包含才644才满足

eg8

[root@localhost find_tdir]# ls -lh

总用量 44K
-rw-r--r-- 1 root root    0 5月  10 11:22 04240920
-rw-r--r-- 1 root root  20K 5月  10 11:02 ddf1
-rw-r--r-- 1 root root  10K 4月   4 12:12 ddf2
drwxr-xr-x 2 root root 4.0K 5月  10 10:22 tdir1
drwxr-xr-x 2 root root 4.0K 5月  10 10:23 Tdir1
drwxr-xr-x 2 root root 4.0K 5月  10 10:22 tdir2
-rw-r--r-- 1  501  501    0 5月  10 10:21 tfile1
-rw-r--r-- 1 root  501    0 5月  10 10:23 Tfile1
-rw-r--r-- 1 root root    0 5月  10 10:21 tfile2
[root@localhost find_tdir]# find -perm 544
[root@localhost find_tdir]# find -perm 644
./ddf1
./tfile2
./04240920
./tfile1
./ddf2
./Tfile1
[root@localhost find_tdir]# find -perm 777
[root@localhost find_tdir]# find -perm /777
.
./ddf1
./tfile2
./tdir2
./04240920
./tdir1
./Tdir1
./tfile1
./ddf2
./Tfile1

3 . 常用的action

     -print 显示

     -ls 类似每一个文件的详细

     -ok  command {} \;    对匹配到的所有文件进行操作,\; 为固定格式,{} 为匹配到的内容

     -exec command {} \;  同-ok  ;区别是-ok 每次操作都需要用户确认,而-exec 则不需要。通常-exec 比较常用

[root@localhost find_tdir]# ls -lh
总用量 44K
-rw-r--r-- 1 root root    0 5月  10 11:22 04240920
-rw-r--r-- 1 root root  20K 5月  10 11:02 ddf1
-rw-r--r-- 1 root root  10K 4月   4 12:12 ddf2
drwxr-xr-x 2 root root 4.0K 5月  10 10:22 tdir1
drwxr-xr-x 2 root root 4.0K 5月  10 10:23 Tdir1
drwxr-xr-x 2 root root 4.0K 5月  10 10:22 tdir2
-rw-r--r-- 1  501  501    0 5月  10 10:21 tfile1
-rw-r--r-- 1 root  501    0 5月  10 10:23 Tfile1
-rw-r--r-- 1 root root    0 5月  10 10:21 tfile2
[root@localhost find_tdir]# find -type f -exec chmod 777 {} \;
[root@localhost find_tdir]# ls -lh
总用量 44K
-rwxrwxrwx 1 root root    0 5月  10 11:22 04240920
-rwxrwxrwx 1 root root  20K 5月  10 11:02 ddf1
-rwxrwxrwx 1 root root  10K 4月   4 12:12 ddf2
drwxr-xr-x 2 root root 4.0K 5月  10 10:22 tdir1
drwxr-xr-x 2 root root 4.0K 5月  10 10:23 Tdir1
drwxr-xr-x 2 root root 4.0K 5月  10 10:22 tdir2
-rwxrwxrwx 1  501  501    0 5月  10 10:21 tfile1
-rwxrwxrwx 1 root  501    0 5月  10 10:23 Tfile1
-rwxrwxrwx 1 root root    0 5月  10 10:21 tfile2













      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值