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

    http://blog.csdn.net/pipisorry/article/details/39831419

    linux find命令语法

    find [起始文件夹] 寻找条件 操作

    find PATH OPTION [-exec COMMAND { } ;]

    因为find命令会依据我们给的option,也就是寻找条件从我们给出的文件夹開始对当中文件及其下子文件夹中的文件进行递归搜索。

    寻找条件

    该命令中的寻找条件能够是一个用逻辑运算符 not、and、or 组成的复合条件。

    (1) and:逻辑与,在命令中用“-a”表示,是系统缺省的选项。表示仅仅有当所给的条 件都满足时,寻找条件才算满足。比如:

    find –name ’tmp’ –xtype c -user ’inin’% 该命令寻找三个给定条件都满足的全部文件

    (2) or:逻辑或,在命令中用“-o”表示。

    该运算符表示仅仅要所给的条件中有一个满足 时,寻找条件就算满足。

    比如:

    find –name ’tmp’ –o –name ’mina*’% 该命令查询文件名称为’tmp’或是匹配’mina*’的全部文件。

    (3) not:逻辑非。在命令中用“!”表示。该运算符表示查找不满足所给条件的文件 。比如:

    find ! –name ’tmp’% 该命令查询文件名称不是’tmp’的全部文件。

    说明:当使用非常多的逻辑选项时。能够用括号把这些选项括起来。

    为了避免Shell本身对括号引起误解,在话号前须要加转义字符“”来去除括号的意义。

    例:

    find (–name ’tmp’ –xtype c -user ’inin’ )

    查询条件option參数

    -name ’字串’ 查找文件名称匹配所给字串的全部文件,字串内可用通配符 *、?、[ ]。

    -lname ’字串’ 查找文件名称匹配所给字串的全部符号链接文件,字串内可用通配符 *、?、[ ]。

    -gid n 查找属于ID号为 n 的用户组的全部文件。

    -uid n 查找属于ID号为 n 的用户的全部文件。

    -group ’字串’ 查找属于用户组名为所给字串的全部的文件。

    -user ’字串’ 查找属于username为所给字串的全部的文件。

    -empty 查找大小为 0的文件夹或文件。

    -path ’字串’ 查找路径名匹配所给字串的全部文件,字串内可用通配符*、?、[ ]。

    -perm 权限 查找具有指定权限的文件和文件夹,权限的表示能够如711。644。

    -size n[bckw] 查找指定文件大小的文件,n 后面的字符表示单位。缺省为 b,代表512字节的块。 -size +500M文件大小大于500M

    -type x 查找类型为 x 的文件,x 为下列字符之中的一个:

    b 块设备文件;c 字符设备文件。d 文件夹文件;p 命名管道(FIFO)

    f 普通文件。l 符号链接文件(symbolic links)。s socket文件。-xtype x 与 -type 基本同样,但仅仅查找符号链接文件。

    以时间为条件查找

    -amin n 查找n分钟曾经被訪问过的全部文件。

    -atime n 查找n天曾经被訪问过的全部文件。

    -cmin n 查找n分钟曾经文件状态被改动过的全部文件。

    -ctime n 查找n天曾经文件状态被改动过的全部文件。

    -mmin n 查找n分钟曾经文件内容被改动过的全部文件。

    -mtime n 查找n天曾经文件内容被改动过的全部文件。

    -print:将搜索结果输出到标准输出。

    样例:在root以及子文件夹查找不包括文件夹/root/bin的,greek用户的,文件类型为普通文件的,3天之前的名为test-find.c的文件。并将结构输出,find命令例如以下:

    find / -name "test-find.c" -type f -mtime +3 -user greek -prune /root/bin -print

    当然在这当中。-print是一个默认选项,我们不必刻意去配置它

    exec选项

    -exec:对搜索的结构指令指定的shell命令。注意格式要正确:"-exec 命令 {} ;"

    在}和之间一定要有空格才行;

    {}表示命令的參数即为所找到的文件;命令的末尾必须以“ ;”结束。

    样例:对上述样例搜索出来的文件进行删除操作,命令例如以下:

    find / -name "test-find.c" -type f -mtime +3 -user greek -prune /root/bin -exec rm {} ;

    find命令指令实例:

    find . - name ‘main*’ - exec more {} ;    % 查找当前文件夹中全部以main开头的文件。并显示这些文件的内容。

    find . (- name a.out - o - name ‘*.o’)> - atime +7 - exec rm {} ;    % 删除当前文件夹下全部一周之内没有被訪问过的a .out或*.o文件。

    % “(” 和 “)” 表示括号(),当中的 “” 称为转义符。之所以这样写是因为对 Shell 而言,(和)另有不同的含义。而不是这里的用于组合条件的用途。

    % “-name a.out” 是指要查找名为a.out的文件;

    % “-name ‘*.o’” 是指要查找全部名字以 .o 结尾的文件。这两个 -name 之间的 -o 表示逻辑或(or),即查找名字为a.out或名字以 .o结尾的文件。

    % find命令在当前文件夹及其子文件夹下找到这佯的文件之后,再进行推断,看其最后訪问时间 是否在7天曾经(条件 -atime +7),若是,则对该文件运行命令 rm(-exec rm {} ;)。

    当中 {} 代表当前查到的符合条件的文件名称,;则是语法所要求的。

    % 上述命令中第一行的最后一个 是续行符。当命令太长而在一行写不下时,可输入一个 。之后系统将显示一个 >。指示用户继续输入命令。

    find中使用正則表達式
    find path -regex "xxx"

    find path -iregex "xxx"

    find . -regex '.*topic[0-9]+' #查找当前文件夹(包括子文件夹)下文件名称为topic+num的文件

    [通配符和正則表達式 linux]

    [linux find 命令中 怎样使用正則表達式]

    皮皮Blog



    使用演示样例

    查找当前文件夹下大小>500M的文件及其大小

    find . -size +500M | xargs du -ha

    find . -size +100M ! -path "*git*" 文件路径中不包括git字样的文件

    linux中find命令查找时不包括某些文件夹。find 命令忽略某个或多个子文件夹的方法

    解决方式:

    在linux中用find 进行查找的时候,有时候须要忽略某些文件夹不查找,能够使用 -prune 參数来进行过滤,要忽略的路径參数必须紧跟着搜索的路径之后,否则该參数无法起作用。

    man find

    ...
    -path pattern
                  File  name matches shell pattern pattern.  The metacharacters do
                  not treat `/' or `.' specially; so, for example,
                            find . -path "./sr*sc"
                  will print an entry for a directory called `./src/misc' (if  one
                  exists).   To  ignore  a whole directory tree, use -prune rather
                  than checking every file in the tree.  For example, to skip  the
                  directory  `src/emacs'  and  all files and directories under it,
                  and print the names of the other files found, do something  like
                  this:
                            find . -path ./src/emacs -prune -o -print
                  Note that the pattern match test applies to the whole file name,
                  starting from one of the start points named on the command line.
                  It  would  only  make sense to use an absolute path name here if
                  the relevant start point is also an absolute path.   This  means
                  that this command will never match anything:
                            find bar -path /foo/bar/myfile -print
                  The  predicate -path is also supported by HP-UX find and will be
                  in a forthcoming version of the POSIX standard.
    ...
    

    也能够使用參数-wholename,只是不建议了
     -wholename pattern
                  See -path.    This alternative is less portable than -path.
    -prune 使用这一选项能够使find命令不在当前指定的文件夹中查找。假设同一时候使用-depth选项,那么-prune将被find命令忽略。

    还要注意 (  前后都有空格

    eg:

    root@ubuntu:/tmp1#find ./ -type f    #/tmp1文件夹下全部文件夹里面的全部文件
    ./file
    ./1/1.cpp
    ./2/2.cpp
    root@ubuntu:/tmp1#find ./ -path ./1 -prune -o -type f -print    #/tmp1中查找文件但忽略文件夹/1中的文件
    ./file
    ./2/2.cpp
    root@ubuntu:/tmp1#find ./ ( -path ./1 -o -path ./2 ) -prune -o -type f -print    #/tmp1中查找文件同一时候忽略文件夹/1 /2中的文件
    ./file
    

    针对文件模式“./1”使用 -path(或者也能够-wholename) 測试。假设该模式已找到。-prune 可防止 find 下到该文件夹中。

    find ./ -path ./1 -prune -o -type f -print
    find [-path ..] [expression] 在路径列表的后面的是表达式
    -path ./1 -prune -o -print 是 -path ./1 -a -prune -o
    布尔类型“-o”使 find 能够针对其它文件夹处理该命令的其余部分。因为每一个表达式之间有一个假设的隐式 and 运算符 (-a),因此,假设左側的表达式计算结果为 false,and 之后的表达式将不进行计算。因此须要 -o 运算符。
    【详解:-print 的简写表达式按顺序求值, -a 和 -o 都是短路求值,与 shell 的 && 和 || 相似假设 -path ./1为真。则 求值-prune , -prune 返回真。与逻辑表达式... -a -prune为真;否则不求值 -prune。与逻辑表达式为假。
    假设 -path ./1 -a -prune 为假,则求值 -print ,-print返回真,或逻辑表达式为真。否则不求值 -print,或逻辑表达式为真。】
    这个表达式组合用伪码能够写为
    if -path ./1  then
              -prune
    else
              -print
    find忽略多个文件夹
    find ./ ( -path ./1 -o -path ./2 ) -prune -o -type f -name "*"-print
    圆括号表示表达式的结合。
    表示引用,即指示 shell 不正确后面的字符作特殊解释,而留给 find 命令去解释其意义。


    查找某一确定文件,-name等选项加在-o 之后
    eg:统计我写了多少行c/c++代码:
    root@ubuntu:/media/E/mine/C++#find ./ ( -path ./SvdFeature -o -path ./SvdFeatureDemo -o -path ./SvdFeatureInfer -o -path ./testSVD ) -prune -o ( -name '*.c' -o -name '*.cpp' -o -name '*.h' -type f )-print| xargs wc -l | more
    ...
    16867 总用量
    from:http://blog.csdn.net/pipisorry/article/details/39831419

    ref:Linux文件查找命令find

    linux中Find命令的使用

    linux find 命令忽略某个或多个子文件夹的方法


  • 相关阅读:
    Introduction to XQuery in SQL Server 2005
    [译]Cassandra 架构简述
    冬日绘版实录
    网页实现串口TCP数据通讯的两种方案
    (转)感知哈希算法
    CoreParking
    单线程扫描电脑所有文件与并行计算扫描电脑所有文件所用时间?
    强名称程序集
    一些题(六)
    一些题(五)
  • 原文地址:https://www.cnblogs.com/wzjhoutai/p/6892670.html
Copyright © 2011-2022 走看看