在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. 301跳转:IIS服务器网站整站301永久重定向设置方法(阿里云)

    欢迎来到重庆SEO俱乐部:搜索引擎优化学习交流QQ群224306761. 承接:seo优化.网站建设.论坛搭建.博客制作.全网营销 博主可接:百度百家.今日头条.一点资讯等软文发布,有需要请联系PE! ...

  2. gridview XML

    GridView动态添加模板列   http://blog.csdn.net/wljhk2006/article/details/24723219 XML与DataTable互转类 http://bl ...

  3. maven 阿里镜像

    <mirror> <id>alimaven</id> <mirrorOf>central</mirrorOf> <name>al ...

  4. Note_JavaWeb_MyBatis3

    Jar包 mybatis-3.2.8.jar junit4.4.jar log4j-1.2.17.jar 常用类 Resources SqlSession SqlSessionFactory SqlS ...

  5. HDU 3362 Fix(状压dp)

    Fix Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  6. SpringMVC 基于注解的Controller @RequestMapping @RequestParam..

    概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Spring 2.5 又为 Spring MVC 引入了注解驱动功能.现在你无须让 Controller 继承任何接口,无需在 ...

  7. transform translate transition 的区别

    transform是变形,下面有translate transform: rotate旋转/scale缩放/skew扭曲/translate移动/matrix矩阵变形transform连写:rotat ...

  8. MPI发送接收例子

    原文地址:http://blog.csdn.net/ziren235/article/details/1704203 #include"mpi.h" int main(int ar ...

  9. C/C++的静态库与动态库

    C/C++编程中相关文件后缀(以Linux系统下为例): .a:           静态库(archive) .c/.cpp:  C/C++源程序 .h/.hpp: C/C++源程序的头文件 .i: ...

  10. osg蝴蝶纹理

    #include <osgViewer/Viewer> #include <osgDB/WriteFile> #include <osg/StateSet> #in ...