文件查找和压缩-查找文件

查找文件

locate

非实时查找(数据库搜索)

工作特点

  • 查找速度快
  • 模糊查找
  • 非实时查找
  • 搜索的是文件的全路径,不仅仅是文件名
  • 可能只搜索用户具备读取和执行权限的目录

语法

  • locate [OPTION]… PATTERN…
  • -r 使用正则表达式
  • -i 不区分大小写
  • -n N 列出符合条件的前N项

查询root2018开头的文件和目录,使用正则表达式

[root@centos7 data]#locate -n 5 -r "root2018.*"
/data/root2018-07-26
/data/root2018-07-27
/data/root2018-07-28
/data/root2018-07-29
/data/root2018-07-30

系统上预建的文件索引数据库

  • /var/lib/mlocate/mlocate.db
  • 手动更新数据库 updatedb
[root@centos7 root2018-07-31]#ll /var/lib/mlocate/mlocate.db
-rw-r-----. 1 root slocate 3265180 Jul 31 09:14 /var/lib/mlocate/mlocate.db
[root@centos7 root2018-07-31]#ll
total 4
-rw-r--r--. 1 root root 6 Jul 31 09:10 f1
-rw-r--r--. 1 root root 0 Jul 31 10:35 f20180731.txt
[root@centos7 root2018-07-31]#touch f20180731.txt
[root@centos7 root2018-07-31]#locate /data/root2018-07-31/f1
/data/root2018-07-31/f1
[root@centos7 root2018-07-31]#
[root@centos7 root2018-07-31]#
[root@centos7 root2018-07-31]#updatedb
[root@centos7 root2018-07-31]#ll /var/lib/mlocate/mlocate.db
-rw-r-----. 1 root slocate 3269057 Jul 31 10:38 /var/lib/mlocate/mlocate.db
[root@centos7 root2018-07-31]#locate f20180731.txt
/data/root2018-07-31/f20180731.txt
[root@centos7 root2018-07-31]#

find

实时查找工具,遍历所有磁盘路径完成查找
如果不写路径,则查找的是当前目录
两个条件,中间不写即-a

工作特点

  • 查找速度略慢
  • 精确查找
  • 实时查找
  • 可能只搜索用户具备读取和执行权限的目录

基本用法

  • find [OPTIONS] [查找路径][查找条件][处理动作]

查找路径

指定具体目标路径;默认为当前目录

查找条件

指定的查找标准,可以文件名、大小、类型、权限等标准进行;默认为找出指定路径下的所有文件
多个条件可以用(),这样可以让后边动作统一执行

  • -maxdepth level 最大搜索目录深度,指定目录为第1级

  • -mindepth level 最小搜索目录深度

    [root@centos7 root2018-07-31]#find /data/ -mindepth 2 -maxdepth 4 -name f1
    /data/root2018-07-27/f1
    /data/root2018-07-28/f1
    /data/root2018-07-29/f1
    /data/root2018-07-29/dir1/f1
    /data/root2018-07-30/f1
    /data/root2018-07-31/f1
    
  • -name 匹配文件名不用"“精确匹配,使用通配符需要用”"

  • -iname 文件名,不区分大小写

  • -inum n 查找inode=n的文件

  • -links n 连接数为n的文件

  • -regex “PATTERN” 正则表达式

  • -prune 排除,裁剪,如在根目录上搜索,但排除/etc/这个目录

  • -user USERNAME 查找属主为指定用户(UID)的文件

  • -group GRPNAME 查找属组为指定组(GID)的文件

  • -uid UserID 查找属主为指定的UID号的文件

  • -gid GroupID 查找属组为指定的GID号的文件

  • -nouser 查找没有属主的文件

  • -nogroup 查找没有属组的文件

  • -size [+|-]#UNIT 常用单位: k, M, G, c( byte)

  • -type TYPE 文件类型d,f,c,l,s,b,p

f: 普通文件
d: 目录文件
l: 符号链接文件
s:套接字文件
b: 块设备文件
c: 字符设备文
p: 管道文件

  • -perm [/|-]MODE MODE精确匹配;/MODE,ugo中有其中有一个满足即可;-MODE,ugo都需要满足

  • -atime [+|-]# #: [#,#+1); +#: [#+1,∞]; -#: [0,#);

  • -mtime

  • -ctime

  • -amin

  • -mmin
    – cmin

  • -a 与

  • -o 或

  • -not, ! 非

    # -type,-size测试
    
    [root@centos7 root2018-07-31]#ll
    total 16
    -rw-r--r--. 1 root root 300 Jul 31 12:55 file1
    -rw-r--r--. 1 root root 295 Jul 31 12:55 file2
    -rw-r--r--. 1 root root 299 Jul 31 12:55 file3
    -rw-r--r--. 1 root root 290 Jul 31 12:56 file4
    
    [root@centos7 root2018-07-31]#find /data/root2018-07-31/ -type f -size 299c
    /data/root2018-07-31/file3
    
    [root@centos7 root2018-07-31]#find /data/root2018-07-31/ -type f -size 1k
    /data/root2018-07-31/file1
    /data/root2018-07-31/file2
    /data/root2018-07-31/file3
    /data/root2018-07-31/file4
    
    [root@centos7 root2018-07-31]#find /data/root2018-07-31/ -type f -size +295c
    /data/root2018-07-31/file1
    /data/root2018-07-31/file3
    
    [root@centos7 root2018-07-31]#find /data/root2018-07-31/ -type f -size -295c
    /data/root2018-07-31/file4
    
    

    查权限测试案例

    [root@centos7 root2018-07-31]#ll
    total 16
    drwxr-xr-x. 2 root root   6 Jul 31 14:49 dir1
    -rw-r--r--. 1 root root 300 Jul 31 12:55 file1
    -rw--w--w-. 1 tom  zhu  295 Jul 31 12:55 file2
    -rwxrw-rw-. 1 zhu  zhu  299 Jul 31 12:55 file3
    -rw-r-xr-x. 1 zhu  tom  290 Jul 31 12:56 file4
        #-perm /权限,至少一个匹配即可
        #-perm /644
        #file1权限为644,完全匹配
        #file2权限为622,u:6和6匹配
        #file3:744,ugo都匹配
        #file4:655,ugo都匹配
    
    [root@centos7 root2018-07-31]#find . -type f  -perm /644
    ./file1
    ./file2
    ./file3
    ./file4
        #-perm /权限,至少一个匹配即可
        #-perm /114
        #file1权限为644,go匹配
        #file2权限为622,即622:110 010 010,114:001 001 100 ,对应二进制没有一位对得上,不匹配
        #file3:744,ugo都匹配
        #file4:655,ugo都匹配
    
    [root@centos7 root2018-07-31]#find . -type f  -perm /114
    ./file1
    ./file3
    ./file4
        #-perm -权限,ugo二进制位都需要有匹配上的
        #-perm -114
        #file1权限为644,644:110 100 100,114:001 001 100 ,ug没有一位对上,不符合
        #file2权限为622,即622:110 010 010,114:001 001 100 ,对应二进制没有一位对得上,不符合
        #file3:744,111 100 100,114:001 001 100,g不匹配,所以不符合
        #file4:655,110 101 101,114:001 001 100 , u不匹配,所以不符合
    
    [root@centos7 root2018-07-31]#find . -type f  -perm -114
        #-perm -权限,ugo二进制位都需要有匹配上的
        #-perm -644
        #file1权限为644,644:110 100 100,644:110 100 100 ,ugo匹配,符合
        #file2权限为622,即622:110 010 010,644:110 100 100 ,go不匹配,不符合
        #file3:744,111 100 100,644:110 100 100,ugo匹配,符合
        #file4:655,110 101 101,644:110 100 100,ugo匹配,符合

    [root@centos7 root2018-07-31]#find . -type f  -perm -644
    ./file1
    ./file3
    ./file4

动作处理

  • -print 默认的处理动作,显示至屏幕
  • -ls 类似于对查找到的文件执行“ls -l” 命令
  • -delete 删除查找到的文件
  • -fls file 查找到的所有文件的长格式信息保存至指定文件中,类似重定向输出
  • -ok COMMAND {} ; 对查找到的每个文件执行由COMMAND指定的命令,对于每个文件执行命令之前,都会交互式要求用户确认。{}表示find命令查找出来的结果
  • -exec COMMAND {} ; 对查找到的每个文件执行由COMMAND指定的命令
  • {} 用于引用查找到的文件名称自身find传递查找到的文件至后面指定的命令时,查找到所有符合条件的文件一次性传递给后面的命令

#-ls

[root@centos7 root2018-07-31]#find . -user zhu -ls
50388903    4 -rwxrw-rw-   1 zhu      zhu           299 Jul 31 12:55 ./file3
50388904    4 -rw-r-xr-x   1 zhu      tom           290 Jul 31 12:56 ./file4

# -fls 类似结果重定向

[root@centos7 root2018-07-31]#find . -user root -fls ../find.log
[root@centos7 root2018-07-31]#cat ../find.log
50388900    0 drwxr-xr-x   3 root     root           86 Jul 31 15:28 .
50388901    4 -rw-r--r--   1 root     root          300 Jul 31 12:55 ./file1
292513    0 drwxr-xr-x   2 root     root            6 Jul 31 14:49 ./dir1
50388905    4 -rw-r--r--   1 root     root          302 Jul 31 15:28 ./find.log
# -ok COMMAND {} \; COMMAND为mv,每次询问

[root@centos7 root2018-07-31]#find /data/dir2/root2018-07-31 -group tom -ok mv {} /data/dir2/ \;
< mv ... /data/dir2/root2018-07-31/file2 > ? y
[root@centos7 root2018-07-31]#ll ..
total 4
-rw-r--r--. 1 zhu  tom     0 Jul 31 15:36 file2
drwxr-xr-x. 2 root root   45 Jul 31 15:41 root2018-07-31

#-exec COMMAND {} \; COMMAND也为mv,不询问,直接操作

[root@centos7 root2018-07-31]#mv ../file2 .
[root@centos7 root2018-07-31]#find /data/dir2/root2018-07-31 -group tom -exec mv {} /data/dir2/ \;
[root@centos7 root2018-07-31]#ll ../
total 4
-rw-r--r--. 1 zhu  tom     0 Jul 31 15:36 file2
drwxr-xr-x. 2 root root   45 Jul 31 15:41 root2018-07-31

[root@centos7 root2018-07-31]#ll
total 0
-rw-r--r--. 1 root root 0 Jul 31 16:02 file1.conf
-rw-r--r--. 1 root root 0 Jul 31 16:02 file2.conf
[root@centos7 root2018-07-31]#find /data/ -name "*.conf" -exec cp  {} {}.orig \;
[root@centos7 root2018-07-31]#ll
total 0
-rw-r--r--. 1 root root 0 Jul 31 16:02 file1.conf
-rw-r--r--. 1 root root 0 Jul 31 16:10 file1.conf.orig
-rw-r--r--. 1 root root 0 Jul 31 16:02 file2.conf
-rw-r--r--. 1 root root 0 Jul 31 16:10 file2.conf.orig





xagrs

  • 参数替换命令,-n N 以N个单位传递,提高效率
  • 把管道传过来的字符当做标准输入,并以换行或者空格为分割字符的标记,把字符分割开,并一个一个传入后续指令中
  • 一些命令不允许放入过多的参数,可以用xagrs来做转换,相当于把参数切割开
# find 找出来的结果,按一行一行的输入到ls -l /path/file 中

[root@centos7 data]#find /sbin/ -perm 700  |xargs ls -l 
-rwx------. 1 root root 881168 Apr 10 16:24 /sbin/build-locale-archive
-rwx------. 1 root root 790576 Apr 10 16:24 /sbin/glibc_post_upgrade.x86_64
-rwx------. 1 root root 139224 Apr 11 08:10 /sbin/iprdbg
-rwx------. 1 root root   6232 Mar 27  2015 /sbin/redhat_lsb_trigger.x86_64
-rwx------. 1 root root  36272 Apr 11 11:22 /sbin/unix_update
[root@centos7 root2018-08-03]#echo f{20..30}|xargs touch
[root@centos7 root2018-08-03]#ls
}   f10    f2   f{20..30  f22  f24  f26  f28  f3   f4  f6  f8
f1  f1.sh  f20  f21       f23  f25  f27  f29  f30  f5  f7  f9
[root@centos7 root2018-08-03]#ls |xargs rm -f
[root@centos7 root2018-08-03]#ls
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值