zoukankan      html  css  js  c++  java
  • 【文本处理命令】之find搜索命令

    一、find搜索命令

      find命令用于按照指定条件查找文件。在系统工作中find命令是不可缺少的,我们一般要查找某个目录下的文件时,都是使用find命令查找,另外find命令也可以配合对查找出的文件进行删除或其他操作。他可以使用不同的文件特性作为查找的条件(文件名,大小,修改时间,权限等信息),匹配成功默认将信息显示在屏幕上。

    格式:

    find [查找路径] 查找条件 操作

    常用参数:

    -name name, -iname name : 文件名称符合 name 的文件。iname 会忽略大小写
    -perm    匹配权限(mode完全匹配,-mode为包含即可)
      
    -perm 444 #查找文件权限
      -perm -444 # -表示并且;查找文件权限中u位有r权限,并且g位有r权限,并且o位有r权限的文件
      -perm /444 # /表示或者;查找文件权限中u位有r权限,或者g位有r权限,或者o位有r权限的文件
      -perm /777 # 777=rwx rwx rwx 即9个条件中满足任意一个即可
    -user    匹配所有者
    -nouser    匹配无所有者的文件
    -group    匹配所有组
    -nogroup  匹配无所有组的文件
    -mtime -n,+n        匹配修改内容的时间(-n指n天以内,+n指n天以前)
    -atime -n,+n        匹配访问文件的时间(-n指n天以内,+n指n天以前)
    -ctime -n,+n        匹配修改文件权限时间(-n指n天以内,+n指n天以前)
    -amin n : 在过去 n 分钟内被读取过
    -cmin n : 在过去 n 分钟内被修改过
    -size n : 匹配文件的大小(50KB查找50KB的文件,+50KB位查找超过50KB的文件,-50KB是查找小于50KB的文件)。文件大小 是 n 单位,b 代表 512 位元组的区块,c 表示字元数,k 表示 kilo bytes,w 是二个位元组。
    --type c 指定类型,文件类型是 c 的文件。
        d: 目录文件
        c: 字符设备
        b: 设备文件
        p: 管道
        f: 一般文件
        l: 符号链接文件
    -maxdepth : 查找最大深度,即目录层次
    -mindepth : 查找最小深度
    -newer f1 !f2 匹配比f1新但比f2旧的文件 -prune 忽略某个目录 -exec command {} ; 后接执行的命令 -ok command {} ; 后接执行命令,执行之前会进行询问是否执行该命令
    -a  且(要同时满足)
    -o  或(只需满足其中一个条件即可)

    常见动作:

    常见动作 说明
    -exec 对查找的文件直接执行该参数后面的shell命令
    -ok 对查找的文件询问式执行该参数后的shell命令
    -delete 删除查找的文件
    -ls 列出查找的文件,详细信息
    -print 打印查找的文件(默认选项)

    注意语法结构:

    1)-exec或-ok后面写完命令必须以空格反斜杠;结尾(;)

    2){}表示find命令所找出来的内容

    实例:

    1)查找当前目录下最近20天之内更新的文件

    # find . -ctime -20

    2)查找/var/log目录中更改时间在7日以前的普通文件,并在删除之前询问

    # find /var/log -type f -mtime +7 -ok rm {} ;

    3)查找前目录中文件属主具有读、写权限,并且文件所属组的用户和其他用户具有读权限的文件(只匹配644)

    # find . -type f -perm 644 -exec ls -l {} ;

    4)模糊匹配644权限(包含644即可)

    # find . -type f -perm -644 -exec ls -l {} ;

    5)查找系统中所有文件长度为0的普通文件,并列出它们的完整路径

    # find . -type f -size 0 -exec ls -l {} ;

    6)-exec执行命令

    # 删除没有属主的用户
    # find / -nouser -exec rm -rf {} ; 
    
    # 删除查找的文件
    # find /home -name "*log" | xargs rm -rf {} ;

    7)-a和-o参数使用

    # 找到所有权限是644的普通文件
    # find /usr/local/ -perm 644 -a -type f | head
    
    # 找到以a开头或以a结尾的普通文件
    # find . -name "a*" -o -name "*a" -type f

     PS:使用-a的话则需要同时满足

    8)查询属主、属组

    # 查询属主为root的文件
    # find /etc/ -user root -type f | head -n 3 | xargs ls -l;
    
    # 查询属组为root的文件
    # find /etc/ -group root -type f | head -n 3 | xargs ls -l;
    
    # 查询没有属主、属组的文件
    # find /etc/ -nouser -type f | head -n 3 | xargs ls -l;
    # find /etc/ -nogroup | head -n 3 | xargs ls -l;

    9)在整个文件系统中找出归属于thy用户的文件复到指定目录

    # 模拟一个用户,将备份的passwd文件的属主和属组改为thy
    # useradd thy
    # chown thy.thy /tmp/passwd 
    # ll /tmp/passwd 
    -rw-r--r-- 1 thy thy 1708 Nov  4 09:56 /tmp/passwd
    
    # 使用find命令将属主为thy的文件复制到指定目录
    # find / -user thy -type f -exec cp -a {} /tmp/thy/ ;

    10)综合案例:按文件所有者和所有组查找 

    # 创建文件
    [root@localhost ~]# cd /tmp/test
    [root@localhost test]# touch file{1..5}
    [root@localhost test]# ls
    file1  file2  file3  file4  file5 
    
    # 实时监听这个目录(可以另外开一个shell窗口)
    [root@VM_0_10_centos ~]# watch -n 1 ls -l /tmp/test/

    # 添加用户
    [root@VM_0_10_centos test]# useradd teacher
    [root@VM_0_10_centos test]# useradd student
    [root@VM_0_10_centos test]# id teacher
    uid=1008(teacher) gid=1011(teacher) groups=1011(teacher)
    [root@VM_0_10_centos test]# id student
    uid=1009(student) gid=1012(student) groups=1012(student)
    # 修改用户主和属组
    [root@VM_0_10_centos test]# chown teacher.teacher /tmp/test/file1
    # chown和chgrp修改属组效果是一样的
    [root@VM_0_10_centos test]# chown .teacher /tmp/test/file2 
    [root@VM_0_10_centos test]# chgrp student /tmp/test/file2 
    [root@VM_0_10_centos test]# chown teacher.student /tmp/test/file3

    # 按文件所有者查找
    [root@VM_0_10_centos test]# find / -user teacher -type f
    /home/teacher/.bashrc
    /home/teacher/.bash_logout
    /home/teacher/.bash_profile
    /var/spool/mail/teacher
    /tmp/test/file3
    /tmp/test/file1
    
    # 按文件所有组查找
    [root@VM_0_10_centos test]# find / -group teacher -xtype f
    /home/teacher/.bashrc
    /home/teacher/.bash_logout
    /home/teacher/.bash_profile
    /tmp/test/file1
    [root@VM_0_10_centos test]# find / -group student -type f
    /home/student/.bashrc
    /home/student/.bash_logout
    /home/student/.bash_profile
    /tmp/test/file2
    /tmp/test/file3
    
    # 查找所有者为root,所属组为student的文件(默认表示且关系)
    [root@VM_0_10_centos test]# find / -user root -group student -type f
    /tmp/test/file2
    等价于
    [root@VM_0_10_centos test]# find / -user root -a -group student -type f
    /tmp/test/file2
    
    # 查找所有者为teacher或所属组为student的文件(表示或关系)
    [root@VM_0_10_centos test]# find / -user teacher -o -group student -type f
    /home/student/.bashrc
    /home/student/.bash_logout
    /home/student/.bash_profile
    /home/teacher
    /home/teacher/.bashrc
    /home/teacher/.bash_logout
    /home/teacher/.bash_profile
    /var/spool/mail/teacher
    /tmp/test/file2
    /tmp/test/file3
    /tmp/test/file1
    
    # 查找所属者不是student的用户
    [root@VM_0_10_centos test]# find /tmp/ -not -user root -type f -exec ls -l {} ;
    -rw-r--r-- 1 thy thy 1708 Nov  4 09:56 /tmp/passwd
    -rw-r--r-- 1 teacher student 0 Nov  5 11:16 /tmp/test/file3
    -rw-r--r-- 1 teacher teacher 0 Nov  5 11:16 /tmp/test/file1

     11)按文件所在深度(层次)查找

    # 最大深度
    [root@VM_0_10_centos test]# find /etc/ -maxdepth 2 | head -n 5
    /etc/
    /etc/group
    /etc/rc6.d
    /etc/audit
    /etc/audit/audit.rules
    
    # 最小深度
    [root@VM_0_10_centos test]# find /etc/ -maxdepth 1 | head -n 5
    /etc/
    /etc/group
    /etc/rc6.d
    /etc/audit
    /etc/mailcap
    
    # 查找/etc目录下最少层次为1最多层次为2的以.conf结尾的文件(这里将最大和最小深度的位置对换就会查不出结果)
    [root@VM_0_10_centos test]# find /etc/ -mindepth 1 -maxdepth 2 -name *.conf | head -n 3
    /etc/audit/auditd.conf
    /etc/sysctl.conf
    /etc/depmod.d/kvdo.conf

    12)对查找到的文件执行某些动作

    # 对查询到的文件进行分权限操作
    [root@VM_0_10_centos test]# find /tmp/test/ -perm 644 -exec chmod g+w {} ;
    [root@VM_0_10_centos test]# ll
    total 0
    -rw-rw-r-- 1 teacher teacher 0 Nov  5 11:16 file1
    -rw-rw-r-- 1 root    student 0 Nov  5 11:16 file2
    
    # 对查询的文件进行备份
    [root@VM_0_10_centos test]# find /tmp/test/ -type f -exec cp {} /tmp/test/test/  ;
    cp: ‘/tmp/test/test/file2’ and ‘/tmp/test/test/file2’ are the same file
    cp: ‘/tmp/test/test/file3’ and ‘/tmp/test/test/file3’ are the same file
    cp: ‘/tmp/test/test/file5’ and ‘/tmp/test/test/file5’ are the same file

    13)find查询指定时间修改时间内的数据,排除要查询的文件,rm删除查询出来的文件

    # find . -name "*.jpg" ! -name "old03*" ! -name "old06*" -mtime -1 -exec rm -rf {} ;

    参考博客

    https://blog.csdn.net/devwang_com/article/details/52457591

  • 相关阅读:
    微信JS SDK Demo
    微信JS SDK使用权限签名算法
    微信JS接口
    微信分享JS接口失效说明及解决方案
    微信支付开发(2) 扫码支付模式一
    不懂技术的人不要对懂技术的人说这很容易实现
    独立开发者经验分享
    微信公开课PRO版张小龙演讲全文
    RC4加密算法的原理及实现
    上传APP加入视频预览--精简点名
  • 原文地址:https://www.cnblogs.com/HeiDi-BoKe/p/11792477.html
Copyright © 2011-2022 走看看