当前位置 主页 > 服务器问题 > Linux/apache问题 >

    Linux上的文件搜索命令实例详解

    栏目:Linux/apache问题 时间:2019-10-11 11:27

    locate

    基础了解

    在centos7上默认没有locate命令,需要先手动安装。安装步骤:http://www.cnblogs.com/feanmy/p/7676717.html

    locate命令搜索的后台数据库路径:/var/lib/mlocate/mlocate.db

    ls -hl /var/lib/mlocate
    total 1.2M
    -rw-r----- 1 root slocate 1.2M Oct 16 14:36 mlocate.db

    更新数据库使用updatedb,配置文件为/etc/updatedb.conf

    # 开启搜索限制
    PRUNE_BIND_MOUNTS = "yes"
    # 不搜索的文件系统
    PRUNEFS = "9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset debugfs devpts ecryptfs exofs fuse fuse.sshfs fusectl gfs gfs2 gpfs hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs sockfs sysfs tmpfs ubifs udf usbfs"
    # 不搜索的文件类型
    PRUNENAMES = ".git .hg .svn"
    # 不搜索的文件路径
    PRUNEPATHS = "/afs /media /mnt /net /sfs /tmp /udev /var/cache/ccache /var/lib/yum/yumdb /var/spool/cups /var/spool/squid /var/tmp"

    命令选项

    了解一个命令可以使用man,如 man locate,可以显示locate相关的选项。这里只介绍几个常用选项
    -c, --count            只输出找到的数量
    -i, --ignore-case      忽略大小写
    -q, --quiet            安静模式,不会显示任何错误讯息
    -r, --regexp REGEXP    使用基本正则表达式
        --regex            使用扩展正则表达式
    -n                     结果中显示n个文件

    使用示例

    1、locate /etc/pro :查询出/etc目录下所有以pro开头的文件

    locate /etc/pro
    /etc/profile
    /etc/profile.d
    /etc/protocols
    /etc/profile.d/256term.csh
    /etc/profile.d/256term.sh
    /etc/profile.d/colorgrep.csh
    /etc/profile.d/colorgrep.sh
    /etc/profile.d/colorls.csh
    /etc/profile.d/colorls.sh
    /etc/profile.d/lang.csh
    /etc/profile.d/lang.sh
    /etc/profile.d/less.csh
    /etc/profile.d/less.sh
    /etc/profile.d/vim.csh
    /etc/profile.d/vim.sh
    /etc/profile.d/which2.csh
    /etc/profile.d/which2.sh

    2、locate -c /etc/pro :显示匹配到的文件数

    locate -c /etc/pro
    17

    3、locate -i topoftencent.class.php :忽略文件名大小写

    locate -i topoftencent.class.php
    /var/www/html/fxyxManage/Spider/TopOfTencent.class.php

    虽然文件名大小写不一致,但使用-i选项依然能匹配出

    find

    基础了解

    使用find进行文件查找的速度要慢于locate,但是功能及参数要强于locate。在使用find时,要避免大范围的搜索

    命令选项

    这里介绍常用选项,其他选项可通过man find了解

    -name  filename       #查找名为filename的文件
    -iname filename       #查找文件filename,不区分大小写
    -gid  n           #查找属组gid为n的文件
    -user  username       #按文件属主来查找
    -group  groupname      #按组来查找
    -mtime  -n +n        #按文件更改时间来查找文件,-n指n天以内,+n指n天以前
    -atime  -n +n        #按文件访问时间来查GIN: 0px">
    -ctime  -n +n        #按文件创建时间来查找文件,-n指n天以内,+n指n天以前
    -nogroup           #查无有效属组的文件,即文件的属组在/etc/groups中不存在
    -nouser            #查无有效属主的文件,即文件的属主在/etc/passwd中不存
    -newer  f1 !f2        #查更改时间比f1新但比f2旧的文件
    -type             #查是块设备、目录、字符设备、管道、符号链接、普通文件
    -size   n[c]        #查长度为n块[或n字节]的文件