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 ...
随机推荐
- 4. printf 命令
1. printf 命令的语法 printf format-string [arguments...] 参数说明: format-string: 为格式控制字符串 arguments: 为参数列表. ...
- Swift2.3适配Swift3.0时出现的各种问题
昨晚上一波手贱把我的小5s升到iOS10.如此配套的话,Xcode7.3升级Xcode8.1看来也是势在必行了.公司程序是Swift2.3的,出于对苹果的恐惧迟迟不敢升级.但丑媳妇儿总要见公婆,借这个 ...
- we are happy 把空格换成 %20 剑指offer P44
public class StringReplace { public static void replaceSpace(String[] str, int length) { if(str == n ...
- OVERLAPPED相关的socket函数介绍
OVERLAPPED相关的socket函数介绍 上一篇文章介绍了<Windows核心编程>OVERLAPPED结构与内核对象IOCompletionPort相关概念,见http://www ...
- java中String类型的相关知识
String类方法整理说明: ·Length()用来求字符串的长度,返回值为字符串的长度: ·charAt()取该字符串某个位置的字符,从0开始,为char类型: ·getChars()将这个字符串中 ...
- TortoiseGit HTTPS方式保存密码最简单的方法
在TortoiseGit的设置 -> git 中选择 编辑本地 .git/config 在最后增加下面内容: [credential] helper = store
- 苹果4S
港版.4S.白.非翻新机.16G.联通3G移动2G电信2G 1000 美版.4S.白.翻新.16G.联通3G移动2G电信3G 980
- 编写高质量iOS代码的52个有效方法2-1
一.变量的定义位置(用{}声明示例变量或者用@property属性声明实例变量) 1.用{}声明示例变量: 此方法生命的实例变量,编译器在编译时,会自动计算其偏移量(表示该变量距离存放对象的内存区域的 ...
- jQuery 截取double数据 重新赋值
$('.prioritySort').each(function(i){ $(this).text($(this).text().substring(0,$(this).text().indexOf( ...
- 如何判断网页中引入jquery
直接上代码,不信自己测. <html xmlns="http://www.w3.org/1999/xhtml"><meta http-equiv="Co ...