linux中find命令详解

本文发布时间: 2019-Mar-22
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 ’字串’ 查找属于用户名为所给字串的所有的文件。-empty 查找大小为 0的目录或文件。-path ’字串’ 查找路径名匹配所给字串的所有文件,字串内可用通配符*、?、[ ]。-perm 权限 查找具有指定权限的文件和目录,权限的表示可以如711,644。-size n[bckw] 查找指定文件大小的文件,n 后面的字符表示单位,缺省为 b,代表512字节的块。-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 {} ;)。其中 {} 代表当前查到的符合条件的文件名,;则是语法所要求的。% 上述命令中第一行的最后一个 是续行符。当命令太长而在一行写不下时,可输入一个 ,之后系统将显示一个 >,指示用户继续输入命令。问题: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.cpproot@ubuntu:/tmp1#find ./ -path ./1 -prune -o -type f -print #/tmp1中查找文件但忽略文件夹/1中的文件./file./2/2.cpproot@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 -printfind [-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-pruneelse-printfind忽略多个文件夹 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/39831419ref:Linux文件查找命令findlinux中Find命令的使用linux find 命令忽略某个或多个子目录的方法


(以上内容不代表本站观点。)
---------------------------------
本网站以及域名有仲裁协议。
本網站以及域名有仲裁協議。

2024-Mar-04 02:10pm
栏目列表