Linux常用命令-搜索查找类

1、find命令

1.1、功能:在目录层次结构中搜索文件,默认在当前目录
1.2、用法:
    find [path...] [表达式]
    表达式可能由下列成份组成:操作符、选项、测试表达式以及动作 
    常用表达式:
        -name pattern :   按文件名搜索,文件名可以使用通配符(‘*’, ‘?’, and ‘[]’)
        -iname pattern:   按文件名搜索,不区分大小写
        -mtime n :        按文件最后修改时间n*24小时搜索
        -mmin n  :        按文件最后修改时间n分钟搜索
        -size n[cwbkMG] : 按文件大小搜索 c:字节 w:双字节 b:512字节的块 k M G
        -type c  :        按文件类型搜索 d:目录 f:文件 l:链接文件 b:块设备(有缓存) c:字符设备(没有缓存) p:命名管道(FIFO) s: socket
        -user uname :     按文件属主搜索
        -amin n :         按文件最后访问时间 n分钟搜索
        -atime n :         按文件最后访问时间 n*24小时搜索
        -maxdepth LEVELS :最大查找层级LEVELS 
        -perm mode/-mode : 按权限搜索,mode:权限等于xxx的的文件 -mode:权限包含xxx的文件
        -regex : 按正则表达式查找
     ##动作:   
        -ls :  以ls -ils格式列出搜索结果文件
        -print :打印搜索结果文件 默认
        -delete : 删除搜索结果文件
        -exec command :对搜索文件执行command
        -ok command : 对搜索文件执行command,首先跟用户确认是否执行
        
    实例.:
        ##在当前目录搜索文件
        [cat@centos6 target2]$ find -type f 
        ./source/aa.txt
        ./source/bb.txt
        ./target/cc.txt
        [cat@centos6 target2]$ 
        ##在当前目录搜索目录
        [cat@centos6 target2]$ find -type d
        .
        ./source
        ./target
        [cat@centos6 target2]$ 
        ##搜索大小是4k的文件
        [cat@centos6 target2]$ find -size 4k 
        .
        ./source
        ./target
        
        [cat@centos6 target2]$ 
        ##搜索大于4K的文件
        [cat@centos6 target2]$ find -size +4k 
        ./source/aa.txt
        ./target/cc.txt
        ##搜索小于4k的文件 
        [cat@centos6 target2]$ find -size -4k 
        ./source/bb.txt
        
        [cat@centos6 source]$ ll
        总用量 12
        -rw-rw-r--. 1 cat cat 5912 8月   6 16:42 aa.txt
        -rw-rw-r--. 1 cat cat 2956 8月   6 16:42 bb.txt
        [cat@centos6 source]$ 
        ##搜索等于5912byte的文件
        [cat@centos6 source]$ find -size 5912c
        ./aa.txt
        ##搜索小于5912byte的文件
        [cat@centos6 source]$ find -size -5912c
        .
        ./bb.txt
        [cat@centos6 source]$ ll
        总用量 12
        -rw-rw-r--. 1 cat cat 5912 8月   6 16:54 aa.txt
        -rw-rw-r--. 1 cat cat 2956 8月   6 16:42 bb.txt
        [cat@centos6 source]$ 
        ##搜索修改时间为2分钟前的文件 
        [cat@centos6 source]$ find -mmin 2
        ##搜索修改时间小于2分钟前的文件
        [cat@centos6 source]$ find -mmin +2
        .
        ./bb.txt
        [cat@centos6 source]$ 
        ##搜索修改时间大于2分钟前的文件
        [cat@centos6 source]$ find -mmin -2
        ./aa.txt
        [cat@centos6 source]$
        [cat@centos6 source]$ ll
        总用量 12
        -rw-rw-r--. 1 cat cat 5912 8月   6 16:54 aa.txt
        -rw-rw-r--. 1 sx  cat 2956 8月   6 16:42 bb.txt
        ##搜索属主为sx的文件
        [cat@centos6 source]$ find -user sx 
        ./bb.txt
        ##搜索目录/home下属主为sx的文件
        [root@centos6 /]# find /home -user sx
        /home/dog/target2/source/bb.txt
        /home/sx
        /home/sx/.bash_profile
        /home/sx/.bash_logout
        /home/sx/.mozilla
        /home/sx/.mozilla/plugins
        /home/sx/.mozilla/extensions
        /home/sx/.bash_history
        /home/sx/.gnome2
        /home/sx/.bashrc
        [root@centos6 /]#    
        ##搜索txt结尾的文件,注意:使用通配符时,名称标识要用单引号或双引号包括,否则报错
        [cat@centos6 ~]$ find ./ -name *txt
        find: 路径必须在表达式之前: ddd.txt
        [cat@centos6 ~]$ find ./ -name "*.txt"
        ./a.txt
        ./ddd.txt
        ./target/yyy2.txt
        ./target2/source/xxx2.txt
        ./target2/source/xxx.txt
        ./target2/target/xxx2.txt
        ./target2/target/source/xxx2.txt
        ./target2/target/source/xxx.txt
        ./target2/target/xxx.txt
        [cat@centos6 ~]$  
       ##搜索权限是644的文件
       [root@centos6 ~]# find /etc -perm 644 -ls|head -3
       130569    0 -rw-r--r--   1 root     root            0 7月 24 05:43 /etc/crypttab
       133352    4 -rw-r--r--   1 root     root           58 5月 12  2016 /etc/networks
       133120    4 -rw-r--r--   1 root     root          153 6月 22  2012 /etc/kde/env/imsettings-kde.sh
       [root@centos6 ~]# 
       ##搜索包含644权限的文件
       [root@centos6 ~]# find /etc -perm -644 -ls|head -5
       130562   12 drwxr-xr-x 102 root     root        12288 8月 19 21:18 /etc
       130569    0 -rw-r--r--   1 root     root            0 7月 24 05:43 /etc/crypttab
       133352    4 -rw-r--r--   1 root     root           58 5月 12  2016 /etc/networks
       133112    4 drwxr-xr-x   3 root     root         4096 7月 24 05:47 /etc/kde
       133113    4 drwxr-xr-x   2 root     root         4096 7月 24 05:47 /etc/kde/env
       ##搜索包含suid的文件
       [root@centos6 ~]# find /usr/bin /usr/sbin -perm -4000 -ls|head -5
       797925   20 -rws--x--x   1 root     root        20056 5月 11  2016 /usr/bin/chsh
       808323  124 ---s--x--x   1 root     root       123832 5月 11  2016 /usr/bin/sudo
       808288  180 ---s--x---   1 root     stapusr    183072 5月 11  2016 /usr/bin/staprun
       803775   52 -rwsr-xr-x   1 root     root        51784 11月 10  2015 /usr/bin/crontab
       791053   76 -rwsr-xr-x   1 root     root        75640 5月 11  2016 /usr/bin/gpasswd
       [root@centos6 ~]# 
       ##搜索包含sgid的文件
       [root@centos6 ~]# find /usr/bin /usr/sbin -perm -2000 -ls|head -5
       799634  140 -rwxr-sr-x   1 root     nobody     141384 5月 11  2016 /usr/bin/ssh-agent
       790483   16 -r-xr-sr-x   1 root     tty         15224 7月 24  2015 /usr/bin/wall
       788428   40 -rwx--s--x   1 root     slocate     38464 3月 12  2015 /usr/bin/locate
       797965   12 -rwxr-sr-x   1 root     tty         12016 5月 11  2016 /usr/bin/write
       803690  216 -rwxr-sr-x   1 root     postdrop   217832 11月 10  2015 /usr/sbin/postqueue
       [root@centos6 ~]# 
       ##搜索包含sticky的文件
       [root@centos6 ~]# find /usr/bin /usr/sbin -perm -1000 -ls|head -5
       ##列出目录下的所有文件
       [root@centos6 ~]# find /home/zhang
       /home/zhang
       /home/zhang/.bash_profile
       /home/zhang/.gnome2
       /home/zhang/.bash_logout
       /home/zhang/.mozilla
       /home/zhang/.mozilla/plugins
       /home/zhang/.mozilla/extensions
       /home/zhang/.bashrc
       [root@centos6 ~]#
       ##搜索结果 -ok 执行命令 ,执行命令前会询问
       [root@centos6 ~]# find . -name *.txt -ok cp -rf {} {}.bak \;
       < cp ... ./test.txt > ? 
       ##搜索结果 -exec 执行命令 ,执行命令前不会询问
       [root@centos6 ~]# find . -name *.txt -exec cp -rf {} {}.bak \;
       [root@centos6 ~]# ll test.txt*
       -rw-r--r--. 1 root root 0 8月  19 21:59 test.txt
       -rw-r--r--. 1 root root 0 8月  19 22:02 test.txt.bak
       [root@centos6 ~]# 
       ##删除搜索结果文件
       [root@centos6 ~]# find . -name "*test.txt*" -delete
       [root@centos6 ~]# find ./ -name test*|xargs rm -rf
       [root@centos6 ~]#                          
       [root@centos6 ~]# ll test.txt*             
       ls: 无法访问test.txt*: 没有那个文件或目录  
       ##将搜索结果文件执行cp命令
       [root@centos6 ~]# find ./ -name test*|xargs -I {} cp -rf {} {}.bak
       [root@centos6 ~]# 
       [root@centos6 ~]# ll test*
       -rw-r--r--. 1 root root 0 8月  19 22:09 test.txt
       -rw-r--r--. 1 root root 0 8月  19 22:10 test.txt.bak
        ##查找名称是test2的文件
        [root@centos6 dir1111]# find . -name test2
        ./test2
        [root@centos6 dir1111]# 
        ##查找名称不是test2的文件; ! 表示非的意思
        [root@centos6 dir1111]# find . ! -name test2
        .
        ./test6
        ./test1
        ./test3
        ./test5
        ./test8
        ./test10
        ./test9
        ./test7
        ./test4    
        ##查找文件test2或test6,加 -ls 只能显示最后一个文件
        [root@centos6 dir1111]# find -name "test2" -o -name "test6" -ls
        267138    0 -rw-r--r--   1 root     root            0 8月 19 22:15 ./test6
        ##查找文件test2或test6,加 -ls 显示所有文件 ,需要将表达式用()括起来
        [root@centos6 dir1111]# find . \( -name "test2" -o -name "test6" \) -ls
        267138    0 -rw-r--r--   1 root     root            0 8月 19 22:15 ./test6
        267124    0 -rw-r--r--   1 root     root            0 8月 19 22:15 ./test2
        [root@centos6 dir1111]# find .  -name "test2" -ls -o -name "test6"  -ls
        267138    0 -rw-r--r--   1 root     root            0 8月 19 22:15 ./test6
        267124    0 -rw-r--r--   1 root     root            0 8月 19 22:15 ./test2

   

2、grep命令

2.1、功能:在每个 FILE 或是标准输入中查找 PATTERN
2.2、用法:
    grep [选项]... PATTERN [FILE]...
    默认的 PATTERN 是一个基本正则表达式(缩写为 BRE)。
    常用选项:
        -i : 忽略大小写
        -v : 排除
        -n :输出同时打印行号
    eg.:
        ##过滤bash
        [cat@centos6 source]$ grep bash bb.txt
        root:x:0:0:root:/root:/bin/bash
        cat:x:500:501::/home/dog:/bin/bash
        sx:x:501:502:这是一个测试账号:/home/sx:/bin/bash
        root:x:0:0:root:/root:/bin/bash
        cat:x:500:501::/home/dog:/bin/bash
        sx:x:501:502:这是一个测试账号:/home/sx:/bin/bash
        [cat@centos6 source]$ 
        [cat@centos6 source]$ 
        ##过滤bash,打印行号
        [cat@centos6 source]$ grep -n bash bb.txt
        1:root:x:0:0:root:/root:/bin/bash
        31:cat:x:500:501::/home/dog:/bin/bash
        32:sx:x:501:502:这是一个测试账号:/home/sx:/bin/bash
        33:root:x:0:0:root:/root:/bin/bash
        63:cat:x:500:501::/home/dog:/bin/bash
        64:sx:x:501:502:这是一个测试账号:/home/sx:/bin/bash
        [cat@centos6 source]$ 
        ##过滤bash,忽略大小写,打印行号
        [cat@centos6 source]$ grep -ni bash bb.txt
        1:root:x:0:0:root:/root:/bin/bash
        31:cat:x:500:501::/home/dog:/bin/bash
        32:sx:x:501:502:这是一个测试账号:/home/sx:/bin/bash
        33:root:x:0:0:root:/root:/bin/bash
        63:cat:x:500:501::/home/dog:/bin/bash
        64:sx:x:501:502:这是一个测试账号:/home/sx:/bin/bash
        65:cat:x:500:501::/home/dog:/bin/BASH
        66:sx:x:501:502:这是一个测试账号:/home/sx:/bin/BASH
        ##过滤bash,排除sx
        [cat@centos6 source]$ grep -ni bash bb.txt|grep -v sx
        1:root:x:0:0:root:/root:/bin/bash
        31:cat:x:500:501::/home/dog:/bin/bash
        33:root:x:0:0:root:/root:/bin/bash
        63:cat:x:500:501::/home/dog:/bin/bash
        65:cat:x:500:501::/home/dog:/bin/BASH

3、locate命令

3.1、功能:在mlocate数据库中查找文件
3.2、用法:
    locate [选项]... [模式]...
    注意:第一使用这个命令前需要用命令updatedb创建mlocate数据库,crontab默认每天定时更新mlocate数据库。默认数据库文件:/var/lib/mlocate/mlocate.db
    常用选项:
        -c : 只显示搜索文件的数量
        -e : 只显示当前存在的文件
    eg.:
        ##更新mlocate数据库
        [root@centos6 ~]# updatedb
        ##查找ling.txt文件
        [root@centos6 ~]# locate ling.txt
        /home/linghc/ling.txt
        [root@centos6 ~]# 
        ##查找后缀是txt的文件数量
        [root@centos6 ~]# locate -c *.txt
        320
        [root@centos6 ~]# 
        ##查找后缀是txt的文件
        [root@centos6 ~]# locate  *.txt
        /etc/pki/nssdb/pkcs11.txt
        /home/linghc/hu.txt
        /home/linghc/ling.txt
        /lib/firmware/ivtv-firmware-license-end-user.txt    
        ... 
        ##查找当前存在且后缀是txt的文件
        [root@centos6 ~]# locate  -e *.txt
        /etc/pki/nssdb/pkcs11.txt
        /home/linghc/hu.txt
        /home/linghc/ling.txt
        /lib/firmware/ivtv-firmware-license-end-user.txt
        ...
        ##删除一个文件
        [root@centos6 ~]# rm /home/linghc/hu.txt
        rm:是否删除普通空文件 "/home/linghc/hu.txt"?y
        ##删除一个文件后,当前存在的文件数减一
        [root@centos6 ~]# locate  -ec *.txt
        319
        [root@centos6 ~]# 
        ##删除一个文件后,mlocate数据库中的文件数不变
        [root@centos6 ~]# locate  -c *.txt
        320
        [root@centos6 ~]#
   

4、whereis命令

4.1、功能:定位命令的二进制、源代码和手册文件
4.2、用法:
    whereis [选项] filename...
    常用选项:
        -b : Search only for binaries.
        -m : Search only for manual sections.
        -s : Search only for sources.

4.3、实例:
        [root@centos6 ~]# whereis passwd
        passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz
        [root@centos6 ~]# 
        [root@centos6 ~]# whereis -b passwd
        passwd: /usr/bin/passwd /etc/passwd
        [root@centos6 ~]# 
        [root@centos6 ~]# whereis -s passwd
        passwd:
        [root@centos6 ~]# 
        [root@centos6 ~]# whereis -m passwd
        passwd: /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz

5、which命令

5.1、功能:显示shell命令在$PATH中的的绝对路径
5.2、用法:
    which [options] [--] programname [...]
    -a --all : 在PATH中打印所有匹配的可执行文件,而不仅仅是第一个。
5.3、实例:
        ##新建passwd文件,没有执行权限
        [root@centos6 ~]# ll passwd
        -rw-r--r--. 1 root root 0 8月  20 10:54 passwd
        [root@centos6 ~]# pwd
        /root
        ##which搜索时,不显示没有执行权限的passwd
        [root@centos6 ~]# which passwd
        /usr/bin/passwd
        [root@centos6 ~]# 
        ##给passwd添加执行权限
        [root@centos6 ~]# chmod u+x passwd
        ##which再搜索时,显示
        [root@centos6 ~]# which passwd
        ./passwd
        [root@centos6 ~]# echo $PATH
        .:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
        [root@centos6 ~]# 
        ##显示所有匹配的命令,不只第一个
        [root@centos6 ~]# which -a passwd
        ./passwd
        /usr/bin/passwd
        [root@centos6 ~]#    

which 不搜索没有执行权限的文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值