在linux find 进行查找的时候,有时候需要忽略某些目录不查找,可以使用 -prune 参数来进行过滤,但必须要注意要忽略的路径参数必须紧跟着搜索的路径之后,否则该参数无法起作用。

命令语法:

find <src-path> -path '<ignor-path>' -prune -o -print

find <src-path> -path '<ignor-path1>' -prune -o -path '<ignor-path2>' -prune -o -print

类似的命令为:(还是使用了匹配,否则只忽略指定的path,不忽略指定path下的文件)

find <src-path> ! -path '<ignor-path1>*' ! -path '<ignor-path2>*' -print

或者使用匹配法:

find <src-path> -path '*<keyword>*' -prune -o -print

find <src-path> ! -path '*<keyword>*' -print

特别:把当前目录下所有文件或文件夹移动到指定文件夹

//当前目录下的所有文件和文件夹
------------------->$ find . -maxdepth
.
./common
./resource
./main
./spring
./circle
./init
./property
./app.properties
./application-config.xml
./method
./zero
//把所有文件或文件夹移动到该目录的main/java下
------------------->$ find . -maxdepth -path './main' -prune -o -print | xargs -i mv {} main/java/
mv: cannot move '.' to 'main/java/.': Device or resource busy //.目录,即当前目录不会移到main/java下
//检查一下各个目录:
------------------->$ pwd
/src ------------------->$ ll
total
drwxrwxr-x rain rain Aug : main/ ------------------->$ cd main/java/ ------------------->$ ll
total
-rw-rw-r-- rain rain Aug : application-config.xml
-rw-rw-r-- rain rain Jul : app.properties
drwxrwxr-x rain rain Jul : circle/
drwxrwxr-x rain rain Aug : common/
drwxrwxr-x rain rain Jul : init/
drwxrwxr-x rain rain Aug : method/
drwxrwxr-x rain rain Aug : property/
drwxrwxr-x rain rain Jul : resource/
drwxrwxr-x rain rain Aug : spring/
drwxrwxr-x rain rain Jul : zero/

测试目录结构为(可通过find .命令列出当前目录下结果):

./a
./a/.txt
./a/.txt
./a/.txt
./a/aa
./a/aa/.txt
./a/aa/.txt
./a/aa/.txt ./b
./b/.txt
./b/.txt
./b/.txt ./c
./c/.txt
./c/.txt
./c/.txt
./c/cc
./c/cc/.txt
./c/cc/.txt
./c/cc/.txt ./d
./d/.txt
./d/.txt
./d/.txt

1. 排除某个文件夹(注意,-type d中包含当前目录.)

------------------->$ find .  -path "./a" -prune -o -type d -print
.
./c
./c/cc
./b
./d
------------------->$ find .  -path "./a/aa" -prune -o -type d -print
.
./c
./c/cc
./b
./d
./a
------------------->$ find .  -path "./a" -prune -o -type f -print
./c/.txt
./c/.txt
./c/cc/.txt
./c/cc/.txt
./c/cc/.txt
./c/.txt
./b/.txt
./b/.txt
./b/.txt
./d/.txt
./d/.txt
./d/.txt
------------------->$ find .  -path "./a/aa" -prune -o -type f -print
./c/.txt
./c/.txt
./c/cc/.txt
./c/cc/.txt
./c/cc/.txt
./c/.txt
./b/.txt
./b/.txt
./b/.txt
./d/.txt
./d/.txt
./d/.txt
./a/.txt
./a/.txt
./a/.txt

2. 排除某些文件夹(注意,-type d和没-type中包含当前目录.)

------------------->$ find .  -path "./a" -prune -o -path './b' -prune -o -type d -print
.
./c
./c/cc
./d
------------------->$ find .  -path "./a" -prune -o -path './b' -prune -o -type f -print
./c/.txt
./c/.txt
./c/cc/.txt
./c/cc/.txt
./c/cc/.txt
./c/.txt
./d/.txt
./d/.txt
./d/.txt

不加-type:

------------------->$ find .  -path "./a" -prune -o -path './b' -prune -o -print
.
./c
./c/.txt
./c/.txt
./c/cc
./c/cc/.txt
./c/cc/.txt
./c/cc/.txt
./c/.txt
./d
./d/.txt
./d/.txt
./d/.txt

3. 使用!方法

首先在./c文件夹下新建了bb文件夹,总目录结构为

------------------->$ find . -print
.
./c
./c/.txt
./c/bb
./c/.txt
./c/cc
./c/cc/.txt
./c/cc/.txt
./c/cc/.txt
./c/.txt
./b
./b/.txt
./b/.txt
./b/.txt
./d
./d/.txt
./d/.txt
./d/.txt
./a
./a/.txt
./a/.txt
./a/.txt
./a/aa
./a/aa/.txt
./a/aa/.txt
./a/aa/.txt
------------------->$ find . ! -path './a*' ! -path './b*' -print
.
./c
./c/.txt
./c/bb
./c/.txt
./c/cc
./c/cc/.txt
./c/cc/.txt
./c/cc/.txt
./c/.txt
./d
./d/.txt
./d/.txt
./d/.txt
------------------->$ find . ! -path './a*' ! -path '*b*' -print
.
./c
./c/.txt
./c/.txt
./c/cc
./c/cc/.txt
./c/cc/.txt
./c/cc/.txt
./c/.txt
./d
./d/.txt
./d/.txt
./d/.txt
------------------->$ find . ! -path './a' ! -path './b' -print
.
./c
./c/.txt
./c/bb
./c/.txt
./c/cc
./c/cc/.txt
./c/cc/.txt
./c/cc/.txt
./c/.txt
./b/.txt
./b/.txt
./b/.txt
./d
./d/.txt
./d/.txt
./d/.txt
./a/.txt
./a/.txt
./a/.txt
./a/aa
./a/aa/.txt
./a/aa/.txt
./a/aa/.txt

也可以排除当前目录:

------------------->$ find . -type d -print
.
./c
./c/bb
./c/cc
./b
./d
./a
./a/aa ------------------->$ find . ! -path '.' -type d -print
./c
./c/bb
./c/cc
./b
./d
./a
./a/aa
------------------->$ find /home/rain/test/find/ -type d -print
/home/rain/test/find/
/home/rain/test/find/c
/home/rain/test/find/c/bb
/home/rain/test/find/c/cc
/home/rain/test/find/b
/home/rain/test/find/d
/home/rain/test/find/a
/home/rain/test/find/a/aa ------------------->$ find /home/rain/test/find/ ! -path '/home/rain/test/find/' -type d -print
/home/rain/test/find/c
/home/rain/test/find/c/bb
/home/rain/test/find/c/cc
/home/rain/test/find/b
/home/rain/test/find/d
/home/rain/test/find/a
/home/rain/test/find/a/aa ------------------->$ find /home/rain/test/find/ ! -path '*/' -type d -print
/home/rain/test/find/c
/home/rain/test/find/c/bb
/home/rain/test/find/c/cc
/home/rain/test/find/b
/home/rain/test/find/d
/home/rain/test/find/a
/home/rain/test/find/a/aa

find排除目录的更多相关文章

  1. find查找时排除目录及文件

    查找根目录下大于500M的文件,排除/proc目录 find / ! -path "/proc/*" -type f -size +500M | sort -rh|xargs ls ...

  2. [100]tar命令打包(排除目录或文件)

    在linux中可以用tar打包目录以方便传输or备份,我们先来看一个例子 Linux下tar命令exclude选项排除指定文件或目录 test 文件夹有如下文件 [root@lee ~]# ll te ...

  3. linux下svn的co如何排除目录

    某些原因想在svn co的时候排除某些目录,可以绕个圈子,分三步来完成: co外层目录: svn checkout --depth empty $URL [$LOCATION] 完成之后,会有一个只包 ...

  4. shell 排除目录

    1.新建文件 exclude.txt,在文件中写需要排除的目录(只需要目录名称,不需要路径) 2.--exclude-from='/data/www/vhosts/git_track/git-shel ...

  5. svn up 排除目录更新

    svn update --set-depth=exclude tmp 则可以排除tmp目录的更新

  6. Linux 下复制(cp)目录时排除一个或者多个目录的方法

    cp 貌似没有排除目录的功能,可以使用 rsync 命令来实现了,如: [案例] /home/52php目录里面有data目录,data目录里面有 a.b.c.d.e 五个目录,现在要把data目录里 ...

  7. Linux tar命令exclude选项排除指定文件或目录

    在linux中可以用tar打包目录以方便传输or备份,我们先来看一个例子 test 文件夹有如下文件 [root@lee ~]# ll test 总用量 -rw-r--r--. root root 4 ...

  8. 7-zip的压缩的时候排除某目录

    安装暂且不说了. 看一下帮助. [root@localhost Server]# 7z -Zip [] - Igor Pavlov -- p7zip Version ,Utf16=on,HugeFil ...

  9. grep时排除指定的文件和目录

    参考:http://winterth.duapp.com/notes/ar03s04.htmlhttp://blog.sina.com.cn/s/blog_7169c8ce0100qkyf.html ...

随机推荐

  1. 转:drupal使用superfish建立下拉菜单

    参考地址:1. http://www.drupalla.com/project/superfish 2.http://drupalchina.cn/node/1798 但是按照这个做出来,我的主菜单和 ...

  2. jquery对象介绍及一些jquery小特效

    一.jquery对象的介绍. 引入jquery库后,通过形如$("#id")的方式得到的对象叫做jquery对象.如var $uu = $("#username" ...

  3. python获取

    def anc():pass print anc.__name__ def timeit(func): def run(*argv): print "this function name i ...

  4. sql 针对拼接语句的优化

    在日常的开发中尽量少采用拼接语句,但针对多条件联合查询,并有多字段可以偏序的情况下,的确采用拼接语句要方便简单得多,单数据库会因为传入的参数不同而产生不同的计划数,计划数多了,对数据库影响很大. 为了 ...

  5. Linux下nginx生成日志自动切割

    1.编辑切割日志的 shell 程序,目录自定 #vi /data/nginx/cut_nginx_log.sh 输入代码: #!/bin/bash # This script run at 00:0 ...

  6. hdu1002

    //c//https://github.com/ssdutyuyang199401/hduacm/blob/master/1002.c#include<stdio.h>#include&l ...

  7. Linux权限解释

    从左至右,第一位数字代表文件所有者的权限,第二位数字代表同组用户的权限,第三位数字代表其他用户的权限. 而具体的权限是由数字来表示的,读取的权限等于4,用r表示:写入的权限等于2,用w表示:执行的权限 ...

  8. iOS开发app自动更新的实现

    #define kStoreAppId @“xxxxxxxxx” // (appid数字串) -(void)checkAppUpdate { NSDictionary *infoDict = [[NS ...

  9. hdu_5968_异或密码(预处理+二分)

    题目链接:hdu_5968_异或密码 题意: 中午,不解释 题解: 前缀处理一下异或值,然后上个二分查找就行了,注意是unsigned long long #include<bits/stdc+ ...

  10. 向openwrt 源码添加ap143支持

    借鉴地址:http://www.pppei.net/blog/post/536 1.向文件 \target\linux\ar71xx\generic\profiles\atheros.mk 中添加ap ...