find 查询命令 & 正则表达式

今日内容

  • find
  • 正则表达式
  • Linux 三剑客之 grep

内容详细

一、find 按名称或属性查询文件

按名称查询

    find [查找目录] [参数] []
    通配符 : *  表示匹配多个字符
            ?   表示匹配一个字符
    忽略大小写 : -i

find /etc -name '*.repo'
find /etc -iname '*Host'

[root@localhost ~]# find /etc/yum.repos.d -name '*.repo'
/etc/yum.repos.d/CentOS-Base.repo
/etc/yum.repos.d/CentOS-CR.repo

[root@localhost ~]# find /etc -iname 'Hos*'
/etc/host.conf
/etc/hosts

按照时间查询


    + 表示几天之前; - 表示几天以内
    -mtime : 按文件内容被修改时间
    -ctime : 按文件属性被修改时间
    -atime : 按文件最新被访问时间

    find /etc -mtime +5
    find /etc -ctime -3
    find /etc -atime -5

[root@localhost ~]# find /root -mtime -3
/root
/root/anaconda-ks.cfg
/root/.bash_history

[root@localhost ~]# find /root -ctime -3
/root
/root/.bash_logout
/root/.bash_profile

按文件大小查询

    + 为大于; - 为小于;没符号是等于;数字后面跟单位(M, b, k..)
             b'    for 512-byte blocks
              '    for bytes
              w'    for two-byte words
              '    for Kilobytes (units of 1024 bytes)
              M'    for Megabytes (units of 1048576 bytes)
              '    for Gigabytes (units of 1073741824 bytes)
    find /etc -size +5M

[root@localhost ~]# find /etc -size 2M
/etc/selinux/targeted/contexts/files/file_contexts.bin

[root@localhost ~]# find /etc -size +100k
/etc/pki/ca-trust/extracted/java/cacerts
/etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt

按 所属用户 查找

查找所有所属主是 root 的文件
    find /etc -user root

[root@localhost etc]# find /var/spool/ -user root
/var/spool/lpd
/var/spool/mail
/var/spool/anacron

按所属组查找

查找所有所属组是 root 的文件
    find /etc -group root

[root@localhost etc]# find /var/log -group root
/var/log/tallylog
/var/log/grubby_prune_debug
/var/log/lastlog

按照文件类型查询

    命令: find /root -type []

文件类型:
    普通文件    : f
    文件夹      : d
    链接文件    : l
    设备文件:
        磁盘文件 : b
        字符文件 : c
    套接字文件   : s
    管道文件     : p

[root@localhost test]# echo {1..4}.txt | xargs touch
[root@localhost test]# find /test -type f
/test/1.txt
/test/2.txt
/test/3.txt
/test/4.txt

[root@localhost test]# find /test -type d
/test/index1
/test/index2

按 index node 号查找

    find /root -inum []

[root@localhost test]# find /test -inum 202280969
/test/10.txt

按权限查找

    find /root -perm []

[root@localhost test]# find /test -perm 644
/test/1.txt
/test/2.txt
/test/3.txt
/test/4.txt

[root@localhost test]# find /test -perm 755
/test/index1
/test/index2

其它参数

    -a : 并且(可以省略,默认时并且)
    -o : 或者
    -maxdepth : 查询的目录深度(必须放置与第一个参数位)

    -exec : 将find处理好的结果交给其它命令继续处理 (结果以 {} 表示)

知识储备

知识储备:
dd 可以造出指定大小的文件,造出来的文件是用 '@' 符号填充
    dd : 生成文件(生成指定大小的文件)
        if      :从什么地方读
        of      : 写入到什么文件
        bs      : 每次写入多少内存的内容
        count   : 写入多少次
dd if=/dev/zero of=5.txt bs=1M count=5

知识储备
    | : 前面一个命令的结果交给后面一个命令处理
    xargs : 把处理的文本变成以空格分割的一行
     : 提前执行命令,然后将结果交给其他命令来处理

[root@localhost test]# echo {a..d} | xargs mkdir
drwxr-xr-x. 2 root root 6 Dec 20 20:25 a
drwxr-xr-x. 2 root root 6 Dec 20 20:25 b
drwxr-xr-x. 2 root root 6 Dec 20 20:25 c
drwxr-xr-x. 2 root root 6 Dec 20 20:25 d

tar -czPf /tmp/etcv2.tar.gz find /etc/ -type f | xargs

实操

    案例1:查询/etc目录下hosts文件
        [root@localhost ~]# find /etc/ -name 'hosts'
        /etc/hosts
    案例2:查询/etc目录下名称中包含hosts文件
        [root@localhost ~]# find /etc/ -name '*hosts*'

    案例3:要求把/etc目录下,所有的普通文件打包压缩到/tmp目录
        [root@localhost /tmp]# tar -czPf /tmp/etcv2.tar.gz find /etc/ -type f | xargs

二、Linux 三剑客之 grep

Linux三剑客之一,文本过滤器(根据文本内容过滤文件)。

语法格式

    grep [参数] [匹配规则] [操作对象]

参数

参数:
    -n : 过滤文本时,将过滤出来的内容在文件内的行号显示出来
    -A : 匹配成功之后,将匹配行的后n行显示出来
    -B : 匹配成功之后,将匹配行的前n行显示出来
    -C : 匹配成功之后,将匹配行的前后各n行显示出来
    -c :  只显示匹配成功的行数
    -o :  只显示匹配成功的内容
    -v :  反向过滤
    -q :  静默输出
    -i : 忽略大小写
    -l :  匹配成功之后,将文本的名称打印出来
    -R|-r : 递归匹配

    -E : 使用拓展正则   等价于  egrep

知识储备

知识储备:
    $? : 上一行命令执行的结果,0代表执行成功,其他数字代表执行失败。
    wc :  匹配行数
        -l : 打印匹配行数
        -c : 打印匹配的字节数
在/etc目录下,有多少个文件包含root。
    grep -rl 'root' /etc/ | wc -l

三、正则表达式

1、正则表达式的分类(grep)

​ 1、普通正则表达式
​ 2、拓展正则表达式

2、普通正则表达式

特殊符号

    ^ : 以某字符开头
    $ : 以某字符结尾
    . : 匹配除换行符之外的任意单个字符

    [] : 某组字符串的任意一个字符
    [a-z]    : 匹配小写字母
    [A-Z]    : 匹配大写字母
    [a-zA-Z] : 匹配字母
    [0-9]    : 匹配数字
    [^]      : 取反

    ()      : 分组
    \       : 取消转义
    \n      : 代表第n个分组

量词

    * :匹配前导字符的任意个数

3、拓展正则

    {}     :匹配的次数
        {n}     : 匹配n次
        {n,}    :至少匹配n次
        {n,m}   :匹配 n 到 m 次
        {,m}    :最多匹配m次
    +      :匹配至少有一个前导字符
    ?      : 匹配一个或零个前导字符
    |      :或

4、实操案例

    案例1:在/etc/passwd文件中,匹配以ftp开头的行
        grep '^ftp' /etc/passwd

    案例2:在/etc/passwd文件中,匹配以bash结尾的行
        grep 'bash$' /etc/passwd

    案例3:匹配本机中有哪些ip
        ip a | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}'

    案例4:要求将/etc/fstab中的去掉包含 # 开头的行,且要求 # 后至少有一个空格
        grep -vE '^#\ +' /etc/fstab

    案例5:找出文件中至少有一个空格的行
        grep -E '\ +' xxx

    案例6:将 nginx.conf 文件中以#开头的行和空行,全部删除
        grep -vE '^\ *#|^$' /etc/nginx/nginx.conf

Original: https://www.cnblogs.com/elijah-li/p/15713013.html
Author: elijah_li
Title: find 查询命令 & 正则表达式

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/524274/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球