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 1
.
./common
./resource
./main
./spring
./circle
./init
./property
./app.properties
./application-config.xml
./method
./zero
//把所有文件或文件夹移动到该目录的main/java下
------------------->$ find .  -maxdepth 1 -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 4
drwxrwxr-x 3 rain rain 4096 Aug  8 22:43 main/

------------------->$ cd main/java/

------------------->$ ll
total 40
-rw-rw-r-- 1 rain rain 1349 Aug  2 23:42 application-config.xml
-rw-rw-r-- 1 rain rain   31 Jul 30 18:42 app.properties
drwxrwxr-x 3 rain rain 4096 Jul 31 13:39 circle/
drwxrwxr-x 3 rain rain 4096 Aug  8 21:19 common/
drwxrwxr-x 3 rain rain 4096 Jul 31 15:29 init/
drwxrwxr-x 3 rain rain 4096 Aug  8 21:19 method/
drwxrwxr-x 3 rain rain 4096 Aug  8 21:20 property/
drwxrwxr-x 3 rain rain 4096 Jul 30 23:15 resource/
drwxrwxr-x 5 rain rain 4096 Aug  7 23:42 spring/
drwxrwxr-x 3 rain rain 4096 Jul 30 21:35 zero/

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

./a
./a/3.txt
./a/2.txt
./a/1.txt
./a/aa
./a/aa/3.txt
./a/aa/2.txt
./a/aa/1.txt

./b
./b/3.txt
./b/2.txt
./b/1.txt

./c
./c/3.txt
./c/2.txt
./c/1.txt
./c/cc
./c/cc/3.txt
./c/cc/2.txt
./c/cc/1.txt

./d
./d/3.txt
./d/2.txt
./d/1.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/3.txt
./c/2.txt
./c/cc/3.txt
./c/cc/2.txt
./c/cc/1.txt
./c/1.txt
./b/3.txt
./b/2.txt
./b/1.txt
./d/3.txt
./d/2.txt
./d/1.txt
------------------->$ find .  -path "./a/aa" -prune -o -type f -print
./c/3.txt
./c/2.txt
./c/cc/3.txt
./c/cc/2.txt
./c/cc/1.txt
./c/1.txt
./b/3.txt
./b/2.txt
./b/1.txt
./d/3.txt
./d/2.txt
./d/1.txt
./a/3.txt
./a/2.txt
./a/1.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/3.txt
./c/2.txt
./c/cc/3.txt
./c/cc/2.txt
./c/cc/1.txt
./c/1.txt
./d/3.txt
./d/2.txt
./d/1.txt

不加-type:

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

3. 使用!方法

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

------------------->$ find . -print
.
./c
./c/3.txt
./c/bb
./c/2.txt
./c/cc
./c/cc/3.txt
./c/cc/2.txt
./c/cc/1.txt
./c/1.txt
./b
./b/3.txt
./b/2.txt
./b/1.txt
./d
./d/3.txt
./d/2.txt
./d/1.txt
./a
./a/3.txt
./a/2.txt
./a/1.txt
./a/aa
./a/aa/3.txt
./a/aa/2.txt
./a/aa/1.txt
------------------->$ find . ! -path './a*' ! -path './b*' -print
.
./c
./c/3.txt
./c/bb
./c/2.txt
./c/cc
./c/cc/3.txt
./c/cc/2.txt
./c/cc/1.txt
./c/1.txt
./d
./d/3.txt
./d/2.txt
./d/1.txt
------------------->$ find . ! -path './a*' ! -path '*b*' -print
.
./c
./c/3.txt
./c/2.txt
./c/cc
./c/cc/3.txt
./c/cc/2.txt
./c/cc/1.txt
./c/1.txt
./d
./d/3.txt
./d/2.txt
./d/1.txt
------------------->$ find . ! -path './a' ! -path './b' -print
.
./c
./c/3.txt
./c/bb
./c/2.txt
./c/cc
./c/cc/3.txt
./c/cc/2.txt
./c/cc/1.txt
./c/1.txt
./b/3.txt
./b/2.txt
./b/1.txt
./d
./d/3.txt
./d/2.txt
./d/1.txt
./a/3.txt
./a/2.txt
./a/1.txt
./a/aa
./a/aa/3.txt
./a/aa/2.txt
./a/aa/1.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
原文地址:https://www.cnblogs.com/drizzlewithwind/p/5705915.html