在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. iOS 打包上传AppStore相关(3)-iTunes相应配置以及使用蒲公英网站进行应用托管分发(链接/二维码)

    上一篇讲到我们最终生成了一个格式为 .xcarchive 的文件(可以右键并Show in Finder)查看.本篇我们就进行最后的设置,打包上传.另外,还有一个小福利,那就是打测试包分发链接测试. ...

  2. swift 图像的压缩上传

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [Str ...

  3. POJ 2411 Mondriaan's Dream/[二进制状压DP]

    题目链接[http://poj.org/problem?id=2411] 题意:给出一个h*w的矩形1<=h,w<=11.用1*2和2*1的小矩形去填满这个h*w的矩形,问有多少种方法? ...

  4. 架设自己的FTP服务器 Serv-U详细配置图文教程

    转自:http://www.jb51.net/article/31635.htm 所有不是很要求安全的情况下是可以用serv_U的,当然我们也可以通过一些设置,保证serv_u安全运行.这里就分享下s ...

  5. ubuntu 安装python mysql模块

    Installation Starting with a vanilla Lucid install , install pip and upgrade to the latest version: ...

  6. CodeForces 705A Hulk

    水题. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #includ ...

  7. WEB前端组件思想【日历】

    DEMO2: 思路:首先获取元素节点元素--->根据点击事件隐藏显示元素--->建立showdate方法(判断12月 则右边年份+1,月份1 )--->还要设置btn开关 防止多次重 ...

  8. CABasicAnimation 几种停止的回调

    一.编写一个简单的动画,使一个UIview从屏幕的左上角移动到左下角,间隔时间3S // // ViewController.m // CAAnimationTest // // Created by ...

  9. c++的输入和输出流

    C++编译系统提供了用于输入输出的iostream类库.iostream这个单词是由3个部 分组成的,即i-o-stream,意为输入输出流.在iostream类库中包含许多用于输入输出的 类.常用的 ...

  10. Chapter 16_3 多重继承

    在Lua中进行面向对象编程时有几种方法,上一小结介绍了一种使用__index元方法的做法. 下面要介绍另一种方法,可以在Lua中实现多继承. 关键一点,在于用函数作为__index元字段. 多重继承意 ...