find
是Unix/Linux命令行工具箱最棒的命令之一,工作方式:沿著文件層次結(jié)構(gòu)向下遍歷,匹配符合條件的文件锻煌,執(zhí)行相應(yīng)的操作妓布。
先預(yù)覽下測試的目錄結(jié)構(gòu)
? findtest tree
.
|____1.txt
|____one
| |____one_1.txt
| |____two
| | |____two_2.txt
|____three
| |____three_1.txt
|____two
| |____two_1.txt
-name
-name
指定了文件名所必須匹配的字符串,可以使用通配符*
匹配某一特征的文件
'*'在shell中是任意字符宋梧,同其他正則語言中的 '.'
- 列出當(dāng)前目錄下所有的
.txt
? findtest find . -name "*.txt"
./1.txt
./one/one_1.txt
./one/two/two_2.txt
./three/three_1.txt
./two/two_1.txt
- 列出有
two
相關(guān)的文件
? findtest find . -name 'two*'
./one/two
./one/two/two_2.txt
./two
./two/two_1.txt
這里看到列出的有文件和目錄匣沼,如果只需要顯示文件或者目錄怎么辦不呢?
-type
-type
可以指定要搜索的文件類型
- 只列出目錄
? findtest find . -name 'two*' -type d
./one/two
./two
- 只列出文件
? findtest find . -name 'two*' -type f
./one/two/two_2.txt
./two/two_1.txt
-type
有多種選項(xiàng)
文件類型 | 類型參數(shù) |
---|---|
普通文件 | f |
目錄 | d |
符號鏈接 | l |
字符設(shè)備 | c |
塊設(shè)備 | b |
套接字 | s |
FIFO | p |
find
在搜索文件只是第一步捂龄,通常我們會對搜索到的文件進(jìn)行某些操作释涛,比如刪除,拷貝等
-delete
刪除匹配的到的文件
? findtest tree
.
|____1.txt
|____one
| |____one_1.txt
| |____two
| | |____two_2.txt
|____three
| |____three_1.txt
|____two
| |____two_1.txt
? findtest find . -name 'three_1.txt' -delete
? findtest tree
.
|____1.txt
|____one
| |____one_1.txt
| |____two
| | |____two_2.txt
|____three
|____two
| |____two_1.txt
? findtest
前后目錄對比發(fā)現(xiàn)我們刪除了three_1.txt
-exec
-delete
命令只能單一的刪除倦沧,如果我們希望執(zhí)行更多的操作只能使用-exec
find
命令可以借助-exec
與其他命令結(jié)合唇撬。{}代表匹配到的每一項(xiàng)。
- 將匹配到的
.txt
拷貝到three
中
? findtest tree
.
|____1.txt
|____one
| |____one_1.txt
| |____two
| | |____two_2.txt
|____three
|____two
| |____two_1.txt
? findtest find . -name "*.txt" -exec cp {} ./three \;
cp: ./three/1.txt and ./three/1.txt are identical (not copied).
cp: ./three/one_1.txt and ./three/one_1.txt are identical (not copied).
cp: ./three/two_2.txt and ./three/two_2.txt are identical (not copied).
? findtest tree
.
|____1.txt
|____one
| |____one_1.txt
| |____two
| | |____two_2.txt
|____three
| |____1.txt
| |____one_1.txt
| |____two_1.txt
| |____two_2.txt
|____two
| |____two_1.txt
最后的結(jié)果雖然是我們預(yù)期的展融,但是中間有異常提示窖认,這個暫時不用管,等學(xué)習(xí)了后續(xù)的選項(xiàng)就可以避免
cp: ./three/1.txt and ./three/1.txt are identical (not copied).
-path
選項(xiàng)-path
可以使用通配符來匹配文件路徑愈污,-name
是匹配的是文件名,-path
則將文件路徑作為一個整體進(jìn)行匹配
- 列出
three
中的.txt
? findtest find . -name '*.txt' -path '**/three/**'
./three/1.txt
./three/one_1.txt
./three/two_1.txt
./three/two_2.txt
? findtest
!
'!'同其他語言一樣耀态,意為‘否定,取反’
- 列出非
three
中的.txt
? findtest find . -name '*.txt' ! -path '**/three/**'
./1.txt
./one/one_1.txt
./one/two/two_2.txt
./two/two_1.txt
有了'!'暂雹、‘-path’首装、‘-name’ 就可以解決我們上邊的cp異常了
- 刪除
three
中的.txt
,還原目錄
? findtest find . -name '*.txt' -path '**/three/**' -delete
? findtest tree
.
|____1.txt
|____one
| |____one_1.txt
| |____two
| | |____two_2.txt
|____three
|____two
| |____two_1.txt
? findtest
- 將匹配到的
.txt
拷貝到three
中
? findtest find . -name '*.txt' ! -path "**/three/**" -exec cp {} ./three \;
? findtest tree
.
|____1.txt
|____one
| |____one_1.txt
| |____two
| | |____two_2.txt
|____three
| |____1.txt
| |____one_1.txt
| |____two_1.txt
| |____two_2.txt
|____two
| |____two_1.txt
? findtest
現(xiàn)在警告已經(jīng)完全沒有了
atime杭跪、mtime仙逻、ctime
Unix文件系統(tǒng)中的每一個文件都有三種時間戳:
- 訪問時間 (atime):用戶最近一次訪問文件的時間
- 修改時間 (mtime):文件內(nèi)容最后一次被修改的時間
- 變化時間 (ctime):文件源數(shù)據(jù)(例如權(quán)限)最后一次改變的時間
-atime
mtime
ctime
可作為find
的時間選項(xiàng),它們可以指定整數(shù)值涧尿,單位是天系奉。通常前邊還帶有-
+
,-
表示小于姑廉,+
表示大于
- 找出最近一天訪問的
.txt
? findtest find . -name '*.txt' -atime -1
./1.txt
./one/one_1.txt
./one/two/two_2.txt
./three/1.txt
./three/one_1.txt
./three/two_1.txt
./three/two_2.txt
./two/two_1.txt
- 列出十天前訪問的
.txt
? findtest find . -name '*.txt' -atime +10
? findtest
因?yàn)檫@個目錄是我今天創(chuàng)建的缺亮,所以沒有匹配內(nèi)容
相應(yīng)的還有以分鐘為單位的時間選項(xiàng)
- amin
- mmin
- cmin
-size
find
的文件大小短選項(xiàng)-size
- 列出小于
1k
的.txt
? findtest find . -name '*.txt' -size -1k
./1.txt
./one/one_1.txt
./one/two/two_2.txt
./three/1.txt
./three/one_1.txt
./three/two_1.txt
./three/two_2.txt
./two/two_1.txt
其他的單位:
單位 | 解釋 |
---|---|
b | 塊(512字節(jié)) |
c | 字節(jié) |
w | 字(2字節(jié)) |
k | 1024字節(jié) |
M | 1024K字節(jié) |
G | 1024M字節(jié) |
-prune
搜索某些目錄是可能需要跳過一些目錄,從而提高性能桥言。比如.git
.svn
,將某些目錄從搜索路徑中排除這種技巧成為修剪
- 列出非
three
中的.txt
? findtest find . \( -name "three" -prune \) -o \( -name '*.txt' \)
./1.txt
./one/one_1.txt
./one/two/two_2.txt
./three
./two/two_1.txt
? findtest find . ! -path "**/three/**" -name '*.txt'
./1.txt
./one/one_1.txt
./one/two/two_2.txt
./two/two_1.txt
? findtest
結(jié)束語:常用find
探索世界~~