zoukankan      html  css  js  c++  java
  • Linux-文本查找

    Linux-文本查找

    文件命名规则

    • 长度不能超过255个字符;

    • 不能使用/当文件名;

    • 严格区分大小写

    文本查找

    grep

    根据模式搜索文本,并将符合模式的文本行显示出来

    使用基本正则表达式定义的模式来过滤文本的命令

    [root@localhost ~]# ls
    abc  aBc  AbC  ABc  ABC  abc-12345  anaconda-ks.cfg  hello  passwd  test
    [root@localhost ~]# ls|grep abc
    abc
    abc-12345
    

    -i(ignore case)

    忽略大小写

    [root@localhost ~]# ls|grep -i abc
    abc
    aBc
    AbC
    ABc
    ABC
    abc-12345
    

    精准查找

    (例如c$表示查找以c结尾的文件)

    (例如^a表示查找以a开头的文件)

    [root@localhost ~]# ls|grep -i ^a
    abc
    aBc
    AbC
    ABc
    ABC
    abc-12345
    anaconda-ks.cfg
    [root@localhost ~]# ls|grep -i c$
    abc
    aBc
    AbC
    ABc
    ABC
    

    --color

    匹配到的内容高亮显示

    alias grep='grep --color=auto'

    [root@localhost ~]# alias
    alias cp='cp -i'
    alias egrep='egrep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias grep='grep --color=auto'
    alias l.='ls -d .* --color=auto'
    alias ll='ls -l --color=auto'
    alias ls='ls --color=auto'
    alias mv='mv -i'
    alias rm='rm -i'
    alias which='(alias; declare -f) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot'
    alias xzegrep='xzegrep --color=auto'
    alias xzfgrep='xzfgrep --color=auto'
    alias xzgrep='xzgrep --color=auto'
    alias zegrep='zegrep --color=auto'
    alias zfgrep='zfgrep --color=auto'
    alias zgrep='zgrep --color=auto'
    

    -v(invert)

    反过来,只打印没有匹配的,而匹配的反而不打印

    [root@localhost ~]# ls
    123456    abc  AbC  ABC        anaconda-ks.cfg  passwd
    20201106  aBc  ABc  abc-12345  hello            test
    [root@localhost ~]# ls|grep -iv abc
    123456
    20201106
    anaconda-ks.cfg
    hello
    passwd
    test
    

    -o(only)

    只显示被模式匹配到的字符串

    [root@localhost ~]# ls|grep abc
    abc
    abc-12345
    [root@localhost ~]# ls|grep -o abc
    abc
    abc
    

    -E(Extend)

    使用扩展正则表达式

    [root@localhost ~]# ls
    123456    abc  AbC  ABC        anaconda-ks.cfg  hello  passwd  xBi
    20201106  aBc  ABc  abc-12345  hBj              lBv    test
    [root@localhost ~]# ls|grep 'xBi|lBv'   		// "|"属于正则表达式中的,需要用-E开启扩展才能使用
    [root@localhost ~]# ls|grep -E 'xBi|lBv'		// 查找xBi和lBv文件
    lBv
    xBi
    [root@localhost ~]# ls|grep -E '^.B.$'			//"."表示任意字符
    aBc
    ABc
    ABC
    hBj
    lBv
    xBi
    [root@localhost ~]# ls|grep -E '^AB.$|^lB.$'	//查找AB.和lb.文件
    ABc
    ABC
    lBv
    

    -q(quiet)

    静默模式,不输出任何信息

    [root@localhost ~]# ls
    123456    abc  AbC  ABC        anaconda-ks.cfg  hello  passwd  xBi
    20201106  aBc  ABc  abc-12345  hBj              lBv    test
    [root@localhost ~]# ls|grep -q 'abc'
    [root@localhost ~]# echo $?
    0												// echo $?输出0表示找到了
    [root@localhost ~]# ls|grep -q 'qwerdf'
    [root@localhost ~]# echo $?
    1												// echo $?输出1表示没找到
    

    -A(After)

    显示匹配到的字符串所在的行及其后n行

    [root@localhost ~]# cat abc
    20201106
    yqh hqy
    abc cba
    Liunx xnuiL
    xxx yyy
    [root@localhost ~]# grep -A1 'yqh' abc
    yqh hqy
    abc cba
    

    -B(Before)

    显示匹配到的字符串所在的行及其前n行

    [root@localhost ~]# grep -B1 'yqh' abc
    20201106
    yqh hqy
    

    -C(Context)

    显示匹配到的字符串所在的行及其前后各n行

    [root@localhost ~]# grep -C1 'yqh' abc
    20201106
    yqh hqy
    abc cba
    

    find

    实时查找,精确性强,遍历指定目录中所有文件完成查找

    查找速度慢,支持众多查找标准

    -name

    对文件名作精确匹配

    [root@localhost ~]# find / -name abc
    /root/abc
    /opt/abc
    

    -iname

    文件名匹配时不区分大小写

    [root@localhost ~]# find / -iname abc
    /root/abc
    /opt/abc
    /opt/aBc
    /opt/AbC
    /opt/ABc
    /opt/ABC
    

    -user

    根据属主来查找

    [root@localhost ~]# useradd yqh
    [root@localhost ~]# ll /home/
    total 0
    drwx------. 2 yqh yqh 62 Nov  6 21:44 yqh
    [root@localhost ~]# find / -user yqh
    /home/yqh
    /home/yqh/.bash_logout
    /home/yqh/.bash_profile
    /home/yqh/.bashrc
    find: ‘/proc/5616/task/5616/fd/7’: No such file or directory
    find: ‘/proc/5616/task/5616/fdinfo/7’: No such file or directory
    find: ‘/proc/5616/fd/6’: No such file or directory
    find: ‘/proc/5616/fdinfo/6’: No such file or directory
    /var/spool/mail/yqh
    [root@localhost ~]# ll /home/yqh/ -a
    total 12
    drwx------. 2 yqh  yqh   62 Nov  6 21:44 .
    drwxr-xr-x. 3 root root  17 Nov  6 21:44 ..
    -rw-r--r--. 1 yqh  yqh   18 Aug 30  2019 .bash_logout
    -rw-r--r--. 1 yqh  yqh  141 Aug 30  2019 .bash_profile
    -rw-r--r--. 1 yqh  yqh  312 Aug 30  2019 .bashrc
    

    -group

    根据属组来查找

    [root@localhost ~]# find / -group yqh
    /home/yqh
    /home/yqh/.bash_logout
    /home/yqh/.bash_profile
    /home/yqh/.bashrc
    find: ‘/proc/5622/task/5622/fd/7’: No such file or directory
    find: ‘/proc/5622/task/5622/fdinfo/7’: No such file or directory
    find: ‘/proc/5622/fd/6’: No such file or directory
    find: ‘/proc/5622/fdinfo/6’: No such file or directory
    [root@localhost ~]# ll /var/spool/mail/yqh
    -rw-rw----. 1 yqh mail 0 Nov  6 21:44 /var/spool/mail/yqh
    

    用户归属

    [root@localhost ~]# ll
    total 8
    -rw-r--r--. 1 root root   45 Nov  6 21:19 abc
    -rw-------. 1 root root 1196 Nov  6 21:13 anaconda-ks.cfg
    [root@localhost ~]# chown yqh abc
    [root@localhost ~]# chown .yqh anaconda-ks.cfg 
    [root@localhost ~]# ll
    total 8
    -rw-r--r--. 1 yqh  root   45 Nov  6 21:19 abc
    -rw-------. 1 root yqh  1196 Nov  6 21:13 anaconda-ks.cfg
    [root@localhost ~]# userdel -r yqh
    [root@localhost ~]# 
    [root@localhost ~]# ll
    total 8
    -rw-r--r--. 1 1000 root   45 Nov  6 21:19 abc
    -rw-------. 1 root 1000 1196 Nov  6 21:13 anaconda-ks.cfg
    [root@localhost ~]# id 1000
    id: ‘1000’: no such user
    

    -nouser

    查找没有属主的文件,用户被删除的情况下产生的文件,只有uid没有属主

    [root@localhost ~]# find -nouser
    ./abc
    

    -nogroup

    查找没有属组的文件,组被删除的情况下产生的文件,只有gid没有属组

    [root@localhost ~]# find -nogroup
    ./anaconda-ks.cfg
    

    -type

    根据文件类型来查找(f,d,l,b,c,p,s)

    1) 普通文件(f):用cat、less、vi等查看或修改内容
    2) 目录文件(d):表示管理系统中的全部文件,包括文件名、子目录名及其指针
    3) 链接文件(l):软链接、硬链接(->)
    4) 设备文件(b): 支持并行,随机访问
    5) 串行端口设备(c):只能串行,不能随机访问,如鼠标、键盘
    6) 管道文件(p):先进先出
    7) 套接字文件(s):计算机内部通信时使用,和外部基于协议通信形成对比
    

    -s(soft)

    软链接与硬链接

    [root@localhost ~]# ln -s abc yqh							//软链接
    [root@localhost ~]# ll
    total 8
    -rw-r--r--. 1 yqh  root   45 Nov  6 21:19 abc
    -rw-------. 1 root yqh  1196 Nov  6 21:13 anaconda-ks.cfg
    lrwxrwxrwx. 1 root root    3 Nov  6 22:05 yqh -> abc
    [root@localhost ~]# ln abc hqy								//硬链接
    [root@localhost ~]# ll
    total 12
    -rw-r--r--. 2 yqh  root   45 Nov  6 21:19 abc
    -rw-------. 1 root yqh  1196 Nov  6 21:13 anaconda-ks.cfg
    -rw-r--r--. 2 yqh  root   45 Nov  6 21:19 hqy
    lrwxrwxrwx. 1 root root    3 Nov  6 22:05 yqh -> abc
    [root@localhost ~]# rm -f abc
    [root@localhost ~]# ll
    total 8
    -rw-------. 1 root yqh  1196 Nov  6 21:13 anaconda-ks.cfg
    -rw-r--r--. 1 yqh  root   45 Nov  6 21:19 hqy
    lrwxrwxrwx. 1 root root    3 Nov  6 22:05 yqh -> abc(在闪烁)
    [root@localhost ~]# find -type l
    ./yqh
    [root@localhost ~]# find -type f
    ./.bash_logout
    ./.bash_profile
    ./.bashrc
    ./.cshrc
    ./.tcshrc
    ./anaconda-ks.cfg
    ./hqy
    [root@localhost ~]# find -type d
    .
    

    -size

    根据文件大小进行查找.如1k、1M,+10k、+10M,-1k、-1M,+表示大于,-表示小于

    [root@localhost ~]# dd if=/dev/zero of=/opt/abc bs=1024M count=2
    2+0 records in
    2+0 records out
    2147483648 bytes (2.1 GB, 2.0 GiB) copied, 1.45364 s, 1.5 GB/s
    [root@localhost ~]# ll -h /opt/
    total 2.1G
    -rw-r--r--. 1 root root 2.0G Nov  6 22:20 abc
    -rw-------. 1 root root 1.2K Oct  26 18:50 anaconda-ks.cfg
    -rw-r--r--. 1 root root 169K Nov  6 22:21 qwe
    [root@localhost ~]# find /opt -size +1G					//查找大于1G的文件
    /opt/abc
    [root@localhost ~]# find /opt -size 2G					//查找2G的文件
    /opt/abc
    [root@localhost ~]# find /opt -size +2G					//查找大于2G的文件
    [root@localhost ~]# find /opt -size -2G					//查找小于1G的文件
    /opt
    /opt/anaconda-ks.cfg
    /opt/qwe
    

    -mtime

    修改时间

    [root@localhost ~]# find /opt -mtime +1					//查找1天以前修改过的文件
    /opt
    /opt/abc
    /opt/qwe
    [root@localhost ~]# find /opt -mtime -1					//查找1天以内修改过的文件
    /opt/anaconda-ks.cfg
    

    -mmin

    多少分钟修改过

    [root@localhost ~]# find /opt -mmin +5					//查找5分钟以前修改过的文件
    /opt
    /opt/abc
    /opt/anaconda-ks.cfg
    /opt/qwe
    [root@localhost ~]# find /opt -mmin -5					//查找5分钟以内修改过的文件
    /opt
    

    组合条件:

    -a

    表示两个条件同时满足

    [root@localhost ~]# find / -name abc -size +1G			//中间默认情况下是-a
    /opt/abc
    [root@localhost ~]# find / -name abc -a -size +1G
    /opt/abc
    

    -o

    表示两个条件满足其中一个

    [root@localhost ~]# find / -name abc -o -size +1G
    /proc/kcore
    find: ‘/proc/5727/task/5727/fd/6’: No such file or directory
    find: ‘/proc/5727/task/5727/fdinfo/6’: No such file or directory
    find: ‘/proc/5727/fd/5’: No such file or directory
    find: ‘/proc/5727/fdinfo/5’: No such file or directory
    /opt/abc
    [root@localhost ~]# ll -h /proc/kcore
    -r--------. 1 root root 128T Nov  6 17:34 /proc/kcore		///proc文件里的都是假的不要管它
    

    -not

    表示满足前面一个条件

    [root@localhost ~]# find / -name abc -not -size +1G			//查找文件名为abc且大小不为1G的文件
    /root/abc
    

    处理动作:默认为显示到屏幕上

    -print 显示

    [root@localhost ~]# find -type l
    ./yqh
    [root@localhost ~]# find -type l -print
    ./yqh
    

    -ls

    显示每一个文件的详细信息

    [root@localhost ~]# find -type l -ls
    101262838      0 lrwxrwxrwx   1  root     root            3 Nov  6 22:05 ./yqh -> abc
    

    -delete

    删除查找到的文件

    [root@localhost ~]# mkdir abc
    [root@localhost ~]# touch abc/123
    [root@localhost ~]# ls
    abc  anaconda-ks.cfg  hqy  yqh
    [root@localhost ~]# find -type d -delete
    find: cannot delete ‘./abc’: Directory not empty			//删除不了有文件的目录
    

    -fls

    查找到的所有文件的长格式信息保存至指定文件中

    [root@localhost ~]# ls
    abc  anaconda-ks.cfg  hqy  yqh
    [root@localhost ~]# find -type d -fls xixi
    [root@localhost ~]# ls
    abc  anaconda-ks.cfg  hqy  xixi  yqh
    [root@localhost ~]# cat xixi
    100663425      0 dr-xr-x---   3  root     root          159 Nov  6 22:55 .
     67534016      0 drwxr-xr-x   2  root     root           17 Nov  6 22:53 ./abc
    

    -ok

    对查找到的每个文件执行COMMAND,每次操作都需要用户确认

    [root@localhost ~]# find -name xixi
    ./xixi
    /tmp/xixi
    [root@localhost ~]# find -name xixi -ok rm -rf xixi ;
    < rm ... ./xixi > ? y									    //需要输入y/n确定或取消
    < rm ... ./tmp/xixi > ? y
    [root@localhost ~]# ls
    abc  anaconda-ks.cfg  hqy  yqh
    

    -exec

    对查找到的每个文件执行COMMAND,操作不需要确认

    [root@localhost ~]# find -name xixi
    ./xixi
    /tmp/xixi
    [root@localhost ~]# find -name xixi -exec rm -rf xixi ;
    [root@localhost ~]# ls
    abc  anaconda-ks.cfg  hqy  yqh
    [root@localhost ~]# find -type d -exec rm -r {} ; &>/dev/null
    [root@localhost ~]# ls
    anaconda-ks.cfg
    [root@localhost ~]# vi abc
    [root@localhost ~]# cat abc
    hello world
    [root@localhost ~]# find -name abc -exec mv {} 123 ;			 //把找到的文件改名为123
    [root@localhost ~]# ls
    123  anaconda-ks.cfg
    [root@localhost ~]# mkdir abc
    [root@localhost ~]# find /opt -type f -exec cp {} ./abc/ ;		 //把找到的文件复制到abc中
    [root@localhost ~]# ls abc
    abc  anaconda-ks.cfg  qwe
    

    -xargs

    通过管道将查找到的内容给xargs处理,xargs后面直接跟命令即可

    注意:find传递查找到的文件至后面指定的命令时,查找到所有

    符合条件的文件一次性传递给后面的命令,而有些命令不能接受过多参数

    此时命令执行可能会失败。而xargs可规避此问题

    [root@localhost ~]# ls
    123 anaconda-ks.cfg  hqy  yqh
    [root@localhost ~]# find -name 123|xargs rm -rf
    [root@localhost ~]# ls
    anaconda-ks.cfg  hqy  yqh
    
  • 相关阅读:
    python list添加元素的几种方法
    Python ---- list和dict遍历
    python 之 collections
    python list 中元素的统计与排序
    pandas dataframe 读取 xlsx 文件
    Python 缓存机制与 functools.lru_cache(zz)
    pip 使用
    python 中的异常处理
    python 时间日期处理
    python read txt file
  • 原文地址:https://www.cnblogs.com/yuqinghao/p/14017598.html
Copyright © 2011-2022 走看看