find排除目录
在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排除目录的更多相关文章
- find查找时排除目录及文件
查找根目录下大于500M的文件,排除/proc目录 find / ! -path "/proc/*" -type f -size +500M | sort -rh|xargs ls ...
- [100]tar命令打包(排除目录或文件)
在linux中可以用tar打包目录以方便传输or备份,我们先来看一个例子 Linux下tar命令exclude选项排除指定文件或目录 test 文件夹有如下文件 [root@lee ~]# ll te ...
- linux下svn的co如何排除目录
某些原因想在svn co的时候排除某些目录,可以绕个圈子,分三步来完成: co外层目录: svn checkout --depth empty $URL [$LOCATION] 完成之后,会有一个只包 ...
- shell 排除目录
1.新建文件 exclude.txt,在文件中写需要排除的目录(只需要目录名称,不需要路径) 2.--exclude-from='/data/www/vhosts/git_track/git-shel ...
- svn up 排除目录更新
svn update --set-depth=exclude tmp 则可以排除tmp目录的更新
- Linux 下复制(cp)目录时排除一个或者多个目录的方法
cp 貌似没有排除目录的功能,可以使用 rsync 命令来实现了,如: [案例] /home/52php目录里面有data目录,data目录里面有 a.b.c.d.e 五个目录,现在要把data目录里 ...
- Linux tar命令exclude选项排除指定文件或目录
在linux中可以用tar打包目录以方便传输or备份,我们先来看一个例子 test 文件夹有如下文件 [root@lee ~]# ll test 总用量 -rw-r--r--. root root 4 ...
- 7-zip的压缩的时候排除某目录
安装暂且不说了. 看一下帮助. [root@localhost Server]# 7z -Zip [] - Igor Pavlov -- p7zip Version ,Utf16=on,HugeFil ...
- grep时排除指定的文件和目录
参考:http://winterth.duapp.com/notes/ar03s04.htmlhttp://blog.sina.com.cn/s/blog_7169c8ce0100qkyf.html ...
随机推荐
- Learning from the CakePHP source code - Part II
原文:http://debuggable.com/posts/learning-from-the-cakephp-source-code-part-ii:480f4dd6-57fc-4715-8709 ...
- Cookie小解2
Cookie最早用来标识和认证一个用户,其处理分为以下几步: 1.服务端向客户端发送Cookie 2.浏览器将Cookie保存 3.之后每次浏览器服务服务器时都会将Cookie发向服务器端 ----- ...
- mac版tomcat修改端口无法访问,80端口无法访问
在mac上安装好了tomcat,修改了端口为80,没想到关闭tomcat时提示出错,而且无法访问,原来我犯了两个错误. 1.我用的是mac上的文本编辑.app打开然后修改的,重新修改为8080也不行, ...
- 导航栏底部黑线隐藏 UINavigationBar hidden Bottom Line
3种方法: 1.大杀器 ,iOS 10.2 最新系统亲测无问题( 添加导航栏分类) https://github.com/samwize/UINavigationBar-Addition/ 2.io ...
- python升级2.7.5
一开始有这个需求,是因为用 YaH3C 替代 iNode 进行校园网认证时,一直编译错误,提示找不到 Python 的某个模块,百度了一下,此模块是在 Python2.7 以上才有的,但是系统的自带的 ...
- C#打印
public partial class Form1 : Form { PrintDocument printDocument; StringReader lineReader = null; pub ...
- Java Web 开发环境快速搭建
Java Web 开发环境快速搭建 在因某种原因更换开发设备后,可依据此文快速搭建开发环境,恢复工作环境. Java开发环境: Windows 10 (64-bit) Oralce JDK Eclip ...
- Java缓存框架
JBossCache/TreeCache JBossCache是一个复制的事务处理缓存,它允许你缓存企业级应用数据来更好的改善性能.缓存数据被自动复制,让你轻松进行Jboss服务器之间的集群工作 ...
- 【成长之路】【python】python基础2
1.位运算 &(与) | (或) ~ (非) ^(异或) 2.三元运算 c = a+b if a>b else a-b if (a>b): a+b else: a-b 3.小知识( ...
- Java知识总结
...