zoukankan      html  css  js  c++  java
  • find命令

    --本文原创

    命令格式

    find  /path/to/somewhere/  - options [- print -exec -ok ...]

    1   /path/to/somewhere/   是find命令所查找的路径,例如"."表示当前路径,可使用绝对或者相对路径

    2   -print 表示将匹配到的文件输出到标准输出中,默认是当前屏幕,可重定向输出流

    3  -exec 表示对find命令查找匹配到的文件执行exec给出的shell 命令,相应的命令形式为 'command' {} ; 注意{}和;之间的空格

    4  -ok 作用和-exec相同,只不过是一种更安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,会给出提示,让用户来确定是否执行

    查找模式

    1 -name 按文件名查找

    2 -perm 按照文件权限查找

       mode刚好等于

       -mode搜寻的权限必须囊括mode,例如要找-rwxr--r-- 即0744 使用 -perm -0744 时 一个为-rwsr-xr-x 即4755也被找出,因为4755包含0744

       +mode搜寻文档权限包含任一mode的权限,即找-perm +755时,为-rw------也被找出来

    例:搜索含有SUID,SGID,SBIT属性的文件,只要包含其中s或t都列出(而-7000则表示都要包含,即--s--s--t)

    [root@localhost ~]# find / -perm +7000

    3 -prune 不在当前指定的目录查找,如果同时使用-dept那么-prune将被忽略

    4 -user 按文件属主查找

    [root@localhost ~]# find /home/ -user Gandefeng

    5 -group 按文件属组查找

    6 -atime,-mtime,ctime

        n 表示在n天之前的一天之内,+n表示在n天只前,不含n天本身,-n表示n天之内,包含n天

                                                            +5         -5

     <--------------------------------------|<--5-->|-------------------------------------->|

      |______|______|______|______|______|______|______|______|______|______|______|

    11         10         9          8          7           6          5          4          3          2          1          0(现在)

    +5 代表大于等于6天前的

    find / -mtime +5

    -5 代表小于等于5天内的

    find / -mtime -5

    5 则表示5-6那一天的

    find / -mtime 5

    例如,将过去24小时内系统上mtime改变过的文件找出来,0表示当前

    [root@localhost ~]# find / -mtime 0

    7 -nogroup 查找无有效属组的文件,即该文件所属的组在/etc/groups中不存在

    8 -nouser 查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在,即用户被删除后残留的文件

    [root@localhost ~]# find /home/ -nouser

    9 -newer file1 file2 查找更改时间比file1新但比文件file2旧的文件,即file1<查找目标<file2

    10 -type 按文件类型查找  b , d , c , p , l , f, s

    11 -size n 表示查找大小为n的文件 带c表示大小以byte计算 k以kb算 即比50k大则 -size +50K

    12 -depth 表示查找文件时,首先查找当前目录中的文件,然后在其子目录中查找

    13 -fstype 表示查找位于某一类文件系统中的文件,这些文件系统通常可以在配置文件/etc/fstab中找到

    14 -mount 表示查找文件时不跨越文件系统mount点

    15  -follow 表示如果find命令遇到符号链接文件,就跟踪至链接所指向的文件

    16 -cpio 表示对匹配的文件使用cpio命令,将这些文件备份到磁带设备中

    一般用-exec或者-ok来执行shell 命令,任何形式的命令都可以在-exec中使用

    实例

    列出当前目录所有普通文件

    [root@localhost ~]# find . -type f -exec ls -l {} ;
    -rw-r--r--. 1 root root 18 12月 29 2013 ./.bash_logout
    -rw-r--r--. 1 root root 176 12月 29 2013 ./.bash_profile
    -rw-r--r--. 1 root root 176 12月 29 2013 ./.bashrc
    -rw-r--r--. 1 root root 100 12月 29 2013 ./.cshrc
    -rw-r--r--. 1 root root 129 12月 29 2013 ./.tcshrc
    -rw-------. 1 root root 1382 11月 13 20:14 ./anaconda-ks.cfg
    -rw-------. 1 root root 2 11月 13 12:15 ./.cache/dconf/user
    -rw-------. 1 root root 11 5月  13 13:03 ./.cache/abrt/lastnotification
    -rw-r--r--. 1 root root 463 11月 13 12:15 ./.dbus/session-bus/9ef6301b72a14a6e8a38e7e00cf69136-9
    -rw-r--r--. 1 root root 1433 11月 13 12:15 ./initial-setup-ks.cfg
    -rw-------. 1 root root 3212 5月  13 12:40 ./.bash_history
    -rw-------. 1 root root 66 11月 13 14:24 ./.xauth3HNIPU
    -rw-r--r--. 1 root root 397 5月  10 16:58 ./.ssh/known_hosts
    -rw-------. 1 root root 408 5月  10 17:02 ./.ssh/authorized_keys
    -rw-r--r--. 1 root root 408 5月  10 17:19 ./.ssh/my_rsa.pub
    -rw-------. 1 root root 1675 5月  10 17:19 ./.ssh/id_rsa
    -rw-------. 1 root root 4123 5月  13 12:16 ./.viminfo
    -rw-r--r--. 1 root root 16 5月  13 12:46 ./test

    查找passwd文件中某个用户是否存在

    [root@localhost ~]# find /etc/ -name passwd -exec grep 'gandefeng' {} ;
    gandefeng:x:1002:1002::/home/gandefeng:/bin/bash

    查找当前用户家目录中所有的文件,包含隐藏文件

    [gandefeng@localhost ~]$ find ~ -print
    /home/gandefeng
    /home/gandefeng/.mozilla
    /home/gandefeng/.mozilla/extensions
    /home/gandefeng/.mozilla/plugins
    /home/gandefeng/.bash_logout
    /home/gandefeng/.bash_profile
    /home/gandefeng/.bashrc
    /home/gandefeng/.cache
    /home/gandefeng/.cache/abrt
    /home/gandefeng/.cache/abrt/lastnotification
    /home/gandefeng/.config
    /home/gandefeng/.config/abrt
    /home/gandefeng/test
    /home/gandefeng/.viminfo
    /home/gandefeng/.bash_history

    xargs对find的配合使用

        在find和-exec 处理匹配文件时,find命令将所匹配到的文件一起传递给exec执行,但是有的系统传递给exec的命令长度是有限制的,find命令运行几分钟后会溢出错误,通常是参数太长或参数列溢出,所以,find把匹配到的文件传递给xargs 而xargs每次只获取一部分而不是全部,分批次处理,有的系统在使用-exec会为每一个匹配到的文件发起一个相应的进程,并非将所匹配到的文件全部作为参数一次执行,这样会出现过多的进程,系统性能下降,而xargs只有一个进程,而且还可以根据参数来调整xargs接受的量

    {}代表由find找到的内容,find找到的结果会放置到{}中

    -exec一直到;是关键词,代表find额外动作的开始(-exec)到结束(;),在这中间的就是find指令的额外动作

    因为分号;在bash环境有特殊意义,所以用反斜杠来跳脱

    例:查找当前目录中每一个普通文件,然后测试属于的文件类型

    [gandefeng@localhost ~]$ find . -type f |xargs file
    ./.bash_logout:                 ASCII text
    ./.bash_profile:                ASCII text
    ./.bashrc:                      ASCII text
    ./.cache/abrt/lastnotification: ASCII text
    ./test:                         ASCII text
    ./.viminfo:                     UTF-8 Unicode text
    ./.bash_history:                ASCII text

    xargs和管道 | 的区别:

    管道:是实现将前面的标准输出作为后面的标准输入
    xargs:是实现将标准输入作为命令的参数

    例如:

    [root@localhost ~]# echo '--help'|cat
    --help
    [root@localhost ~]# echo '--help'|xargs cat
    Usage: cat [OPTION]... [FILE]...
    Concatenate FILE(s), or standard input, to standard output.
    
      -A, --show-all           equivalent to -vET
      -b, --number-nonblank    number nonempty output lines
      -e                       equivalent to -vE
      -E, --show-ends          display $ at end of each line
      -n, --number             number all output lines
      -s, --squeeze-blank      suppress repeated empty output lines
      -t                       equivalent to -vT
      -T, --show-tabs          display TAB characters as ^I
      -u                       (ignored)
      -v, --show-nonprinting   use ^ and M- notation, except for LFD and TAB
          --help     display this help and exit
          --version  output version information and exit
    
    With no FILE, or when FILE is -, read standard input.
    ......
    后面省略

    管道符 | 所传递给程序的不是你简单地在程序名后面输入的参数,它们会被程序内部的读取功能如scanf和gets等接收,而xargs则是将内容作为普通的参数传递给程序,相当于你手写了cat --help,管道符后不加xargs相当于先将xargs后面的命令回车执行一下再从键盘里输入,管道符前面命令执行的结果内容加上xargs 相当于直接从键盘输入管道符前面命令执行的结果内容再回车,即回车的先后顺序不一样。

       

  • 相关阅读:
    js面试题-----算法类
    js面试题-----安全类
    js面试题-----通信类
    js面试题-----面向对象类
    js面试题-----HTTP协议类
    js面试题-----CSS盒模型
    Java-JVM 类的初始化
    加密的相关基础
    AngularJS-directive.js 基本指令
    AngularJS-liveRoomDirective.js 直播间指令
  • 原文地址:https://www.cnblogs.com/gandefeng/p/6849571.html
Copyright © 2011-2022 走看看