zoukankan      html  css  js  c++  java
  • linux文件管理之查找

    1 文件查找



    1.1 which


    查找可执行文件的路径
    which是通过 PATH环境变量到该路径内查找可执行文件,所以基本的功能是寻找可执行文件
    [root@www ~]# which [-a] command
    选项或参数:
    -a :将所有由 PATH 目录中可以找到的指令均列出,而不止第一个被找到的指令名称
    例一:搜寻 ifconfig 这个指令的完整文件名
    [root@MySQL-master tmp]# which -a ifconfig
    /sbin/ifconfig

    例二:用which查找which
    [root@MySQL-master tmp]# which which
    alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
    /usr/bin/which
    # 竟然会有两个 which ,其中一个是 alias 这玩意儿呢!那是啥?别名

    例三:请找出 cd 这个指令的完整文件名
    [root@MySQL-master tmp]# which cd
    /usr/bin/which: no cd in (/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/mysql/bin:/root/bin)
    #cd是内部命令

    1.2 locate


    whereis
    语法:
    [root@redhat ~]# whereis [-bmsu] 文件或者目录名称
    参数说明:
    -b : 只找二进制文件
    -m: 只找在说明文件manual路径下的文件
    -s : 只找source源文件
    -u : 没有说明文档的文件
    例一:请用不同的身份找出 ifconfig 这个档名
    [root@MySQL-master tmp]# whereis ifconfig
    ifconfig: /sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
    [root@MySQL-master tmp]# su - john
    [john@MySQL-master ~]$ whereis ifconfig
    ifconfig: /sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
    [john@MySQL-master ~]$ logout
    # 注意看,明明 which 一般使用者找不到的 ifconfig 即可以用 whereis 找到!
    # 这是因为系统真癿有 ifconfig 这个文件,但是使用者的 PATH 幵没有加入/sbin

    例二:只找出跟 passwd 有关的『说明文件』档名(man page)
    [root@MySQL-master tmp]# whereis -m passwd
    passwd: /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz

    例三:只找出二进制文件
    [root@MySQL-master tmp]# whereis -b passwd
    passwd: /usr/bin/passwd /etc/passwd

    和find相比,whereis查找的速度非常快,这是因为linux系统会将 系统内的所有文件都记录在一个数据库文件中,当使用whereis和下面即将介绍的locate时,会从数据库中查找数据,而不是像find命令那样,通 过遍历硬盘来查找,效率自然会很高。
    但是该数据库文件并不是实时更新,默认情况下时每天更新一次,因此,我们在用whereis和locate 查找文件时,有时会找到已经被删除的数据,或者刚刚建立文件,却无法查找到,原因就是因为数据库文件没有被更新。

    locate
    语法:
    [root@redhat ~]# locate 文件或者目录名称
    选项或参数:
    -i  :忽略大小写的差异;
    -r  :后面可接正则表示法的显示方式
    例一:找出系统中所有passwd 相关的
    [root@MySQL-master tmp]# locate passwd
    /etc/passwd
    /etc/passwd-
    /etc/pam.d/passwd
    /etc/security/opasswd
    /lib64/security/pam_passwdqc.so
    /lib64/security/pam_unix_passwd.so
    /usr/bin/gpasswd
    /usr/bin/htpasswd
    /usr/bin/kpasswd
    /usr/bin/lppasswd



         locate 寻找的数据是由『已建立的数据库 /var/lib/mlocate/』 里面的数据所搜寻到的,所以不用直接在去硬盘中存取数据,因此很快。
        那能否手动更新数据库哪?当然可以啊!更新 locate 数据库地方法非常简单,直接输入 updatedb 就可以了! updatedb 指令会去读取 /etc/updatedb.conf 这个配置文件的设定,然后再去硬盘里面迚行搜寻文件名的动作, 最后就更新整个数据库文件啰!因为 updatedb 会去搜寻硬盘,所以当你执行 updatedb 时,可能会等待数分钟癿时间喔!

    1.3 find


    语法:
    [root@redhat ~]# find 路径 参数
    参数说明:
    时间查找参数:
    -atime n :将n*24小时内存取过的的文件列出来
    -ctime n :将n*24小时内改变、新增的文件或者目录列出来
    -mtime n :将n*24小时内修改过的文件或者目录列出来
    -newer file :把比file还要新的文件列出来
    file://C:UsersanliuAppDataLocalTempct_tmp/1.png
    图中最右边为目前的时间,越往左边则代表越早之前的时间轴啦。
    • +4代表大于等于5天前的档名: find /var -mtime +4
    • -4代表小于等于4天内的文件档名:find /var -mtime -4
    • 4则是代表4-5那一天的文件档名: find /var -mtime 4

    例一:将过去系统上面 24 小时内有更动过内容 (mtime) 的文件列出
    [root@MySQL-master tmp]# find ./ -mtime 0
    # 那个 0 是重点!0 代表目前的时间,所以,从现在开始到 24 小时前

    例二:查找4天内改动过的内容
    [root@MySQL-master tmp]# find ./ -mtime -4
    #-mtime -n             # 文件内容改变时间在N天以内
    #-mtime +n             # 文件内容改变时间在N+1天以前
    #-mtime n              # 文件内容改变时间在N到N+!那天


    与使用者或组名有关参数:
    -gid n       :寻找群组ID为n的文件
    -group name  :寻找群组名称为name的文件
    -uid n       :寻找拥有者ID为n的文件
    -user name   :寻找用户者名称为name的文件
    -nouser  :寻找文件的拥有者不存在 /etc/passwd 的人!
    -nogroup  :寻找文件的拥有群组不存在于 /etc/group 的文件!
    当你自行安装软件时,很可能该软件的属性当中并没有文件拥有者,这是可能的!在这个时候,就可以使用 -nouser 不 -nogroup 搜寻。

    例三:搜寻 /home 底下属于john 的文件
    [root@MySQL-master tmp]# find /home -user john

    例四:搜寻系统中不属于任何人的文件
    [root@MySQL-master tmp]# find / -nouser

       -uid n :n 为数字,这个数字是使用者的帐号 ID,亦即 UID ,这个 UID 是记录在
                /etc/passwd 里面与帐号名称对应的数字。这方面我们会在第四篇介绍。
       -gid n :n 为数字,这个数字是群组名称的 ID,亦即 GID,这个 GID 记录在
                /etc/group,相关的介绍我们会第四篇说明~
       -user name :name 为使用者帐号名称喔,也就是按照文件属主来查找文件。
       -group name:name 为群组名称喔,按照文件所属的组来查找文件。
        -nouser 查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在
        -nogroup 查找无有效所属组的文件,即该文件所属的组在/etc/groups中不存在。当你自行安装软件时,很可能该软件的属性当中并没有文件拥有者,这是可能的!在这个时候,就可以使用 -nouser 与 -nogroup 搜寻。

    范例五:找出档名为 passwd 这个档案
    [root@MySQL-master tmp]# find / -name passwd
    /tmp/passwd
    /usr/bin/passwd
    /etc/passwd
    /etc/pam.d/passwd

    范例六:找出/Server/Scripts/所有脚本文件,即后缀为sh
    [root@MySQL-master tmp]# find /Server/Scripts/ -type f -name "*.sh"

    额外操作:
    -exec command :command 为其他挃令,-exec 后面可再接额外的指令来处理搜寻到癿结果。
    -print  :将结果打印到屏幕上,这个动作是预设!
    范例七:将上个范例(范例六)找到的档案使用 ls -l 列出来~
    [root@MySQL-master tmp]# find /Server/Scripts/ -type f -name "*.sh" -exec ls -l {} ;

    #{} 代表癿是『由 find 找到癿内容』,如上图所示,find 癿结果会被放置到 {} 位置中;
    #-exec 一直到 ; 是关键词,代表 find 额外动作癿开始 (-exec) 到结束 (;) ,在这中间的就是find 挃令内的额外劢作。 在本例中就是『 ls -l {} 』啰!
    ·  因为『 ; 』在 bash 环境下是有特殊意义的,因此利用反斜杠来脱义。

    范例八:找出系统中,大于 1MB 癿档案
    [root@MySQL-master tmp]# find / -size +1000k

    上面exec可以使用xargs来替代
    [root@MySQL-master tmp]# find /Server/Scripts/ -type f -name "*.sh" | xargs ls -l

    那么他们有什么区别呢?
    find . -name "*.txt" -exec rm {} ;
    find . -name "*.txt" | xargs rm {}

    -exec
         1.参数是一个一个传递的,传递一个参数执行一次rm
         2.文件名有空格等特殊字符也能处理
     -xargs
         1.一次将参数传给命令,可以使用-n控制参数个数
         2.处理特殊文件名需要采用如下方式:
         find . -name "*.txt" print0 |xargs -0 rm {}

    xargs是给命令传递参数的一个过滤器,也是组合多个命令的一个工具。它把一个数据流分割为一些足够小的块,以方便过滤器和命令进行处理。通常情况下,xargs从管道或者stdin中读取数据,但是它也能够从文件的输出中读取数据。xargs的默认命令是echo,这意味着通过管道传递给xargs的输入将会包含换行和空白,不过通过xargs的处理,换行和空白将被空格取代。
    [root@MySQL-master tmp]# xargs --help
    用法: xargs [-0prtx] [--interactive] [--null] [-d|--delimiter=delim]
           [-E eof-str] [-e[eof-str]]  [--eof[=eof-str]]
           [-L max-lines] [-l[max-lines]] [--max-lines[=max-lines]]
           [-I replace-str] [-i[replace-str]] [--replace[=replace-str]]
           [-n max-args] [--max-args=max-args]
           [-s max-chars] [--max-chars=max-chars]
           [-P max-procs]  [--max-procs=max-procs] [--show-limits]
           [--verbose] [--exit] [--no-run-if-empty] [--arg-file=file]
           [--version] [--help] [command [initial-arguments]]

    xargs常用场景:
    1、假如你有一个文件包含了很多你希望下载的URL, 你能够使用xargs 下载所有链接
    # cat list.txt | xargs wget –c

    2、查找所有的jpg文件并压缩
    # find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz

    3、个文件夹下文件太多,使用rm删除就会出现/bin/rm: Argument list too long错误,当然使用别的命令也是一样的错误,像ls,chmod等。
    解决方法:
    方法1:
    ls | xargs -n 10 rm -fr ls
    方法2:
    find . -maxdepth 1 -name "*.sh" -type f -exec rm -f {} ;

  • 相关阅读:
    第十二章学习笔记
    UVa OJ 107 The Cat in the Hat (戴帽子的猫)
    UVa OJ 123 Searching Quickly (快速查找)
    UVa OJ 119 Greedy Gift Givers (贪婪的送礼者)
    UVa OJ 113 Power of Cryptography (密文的乘方)
    UVa OJ 112 Tree Summing (树的求和)
    UVa OJ 641 Do the Untwist (解密工作)
    UVa OJ 105 The Skyline Problem (地平线问题)
    UVa OJ 100 The 3n + 1 problem (3n + 1问题)
    UVa OJ 121 Pipe Fitters (装管子)
  • 原文地址:https://www.cnblogs.com/anttech/p/10597687.html
Copyright © 2011-2022 走看看