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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM