天天看點

Linux指令之find

文章目錄

  • ​​1.find指令使用規範:find path -option​​
  • ​​2.-option屬性類型​​
  • ​​2.1.根據檔案名稱name查找:find /Users/gaoxinfu/open-source/redis-unstable/ -name server.h​​
  • ​​2.2.根據檔案類型type查找:​​
  • ​​2.2.1.列出檔案:find /Users/gaoxinfu/demo-linux/ -type f​​
  • ​​2.2.2.列出檔案夾:find /Users/gaoxinfu/demo-linux/ -type d​​
  • ​​2.3.根據時間查找:​​
  • ​​2.3.1.根據檔案建立時間ctime查找.:find . -ctime -1​​
  • ​​2.3.2.根據檔案更新時間mtime查找.:find . -mtime -1​​
  • ​​2.4.根據檔案大小查找:​​
  • ​​2.4.1.檢視檔案大于1k的檔案:find . -size 0​​
  • ​​2.4.2.檢視檔案大于1k的檔案:find . -size +1k​​
  • ​​2.5.不同類型可以混合使用:find . -ctime -1 -type f​​
  • ​​2.6.對查找完的結果進行操作:find . -size 0 -exec ls -la {} \;​​
  • ​​2.6.1.列出檔案大小為0的檔案詳情:find . -size 0 -exec ls -la {} \;​​
  • ​​2.6.1.find 後面的-exec command {} \ 語句說明​​
  • ​​3.附:檢視檔案目錄比較大的檔案夾和檔案​​

1.find指令使用規範:find path -option

find   path   -option   [   -print ]   [ -exec   -ok   command ]   {} \;      

2.-option屬性類型

2.1.根據檔案名稱name查找:find /Users/gaoxinfu/open-source/redis-unstable/ -name server.h

localhost:redis-5.0.5 gaoxinfu$ find /Users/gaoxinfu/open-source/redis-unstable/ -name server.h 
/Users/gaoxinfu/open-source/redis-unstable//src/server.h
localhost:redis-5.0.5      

當然我們查詢的資料可以根據正規表達式去比對檔案

localhost:redis-unstable gaoxinfu$ find /Users/gaoxinfu/open-source/redis-unstable/ -name server*
/Users/gaoxinfu/open-source/redis-unstable//tests/support/server.tcl
/Users/gaoxinfu/open-source/redis-unstable//src/server.c
/Users/gaoxinfu/open-source/redis-unstable//src/server.h
localhost:redis-unstable gaoxinfu$      

2.2.根據檔案類型type查找:

2.2.1.列出檔案:find /Users/gaoxinfu/demo-linux/ -type f

列出目前目錄以及及其子目錄下的所有檔案(不包含檔案夾)

localhost:demo-linux gaoxinfu$ pwd
/Users/gaoxinfu/demo-linux
localhost:demo-linux gaoxinfu$ ls -la
total 0
drwxr-xr-x   4 gaoxinfu  staff   128  3 21 20:45 .
drwxr-xr-x@ 99 gaoxinfu  staff  3168  3 21 20:42 ..
drwxr-xr-x   5 gaoxinfu  staff   160  3 21 20:44 a
drwxr-xr-x   2 gaoxinfu  staff    64  3 21 20:45 b
localhost:demo-linux gaoxinfu$ cd a/
localhost:a gaoxinfu$ pwd
/Users/gaoxinfu/demo-linux/a
localhost:a gaoxinfu$ ls -la
total 0
drwxr-xr-x  5 gaoxinfu  staff  160  3 21 20:44 .
drwxr-xr-x  4 gaoxinfu  staff  128  3 21 20:45 ..
-rw-r--r--  1 gaoxinfu  staff    0  3 21 20:44 test.txt
-rw-r--r--  1 gaoxinfu  staff    0  3 21 20:44 test1.txt
-rw-r--r--  1 gaoxinfu  staff    0  3 21 20:44 test2.txt
localhost:a gaoxinfu$ cd ../b
localhost:b gaoxinfu$ pwd
/Users/gaoxinfu/demo-linux/b
localhost:b gaoxinfu$ ls -la
total 0
drwxr-xr-x  2 gaoxinfu  staff   64  3 21 20:45 .
drwxr-xr-x  4 gaoxinfu  staff  128  3 21 20:45 ..
localhost:b gaoxinfu$ cd ..
localhost:demo-linux gaoxinfu$ pwd
/Users/gaoxinfu/demo-linux      

使用相對路徑查找:find . -type f

localhost:demo-linux gaoxinfu$ find . -type f
./a/test1.txt
./a/test2.txt
./a/test.txt      

使用絕對路徑查找:find /Users/gaoxinfu/demo-linux/ -type f

localhost:demo-linux gaoxinfu$ find /Users/gaoxinfu/demo-linux/ -type f
/Users/gaoxinfu/demo-linux//a/test1.txt
/Users/gaoxinfu/demo-linux//a/test2.txt
/Users/gaoxinfu/demo-linux//a/test.txt
localhost:demo-linux gaoxinfu$      

2.2.2.列出檔案夾:find /Users/gaoxinfu/demo-linux/ -type d

localhost:demo-linux gaoxinfu$ find /Users/gaoxinfu/demo-linux/ -type d
/Users/gaoxinfu/demo-linux/
/Users/gaoxinfu/demo-linux//a
/Users/gaoxinfu/demo-linux//b
localhost:demo-linux gaoxinfu$      

2.3.根據時間查找:

2.3.1.根據檔案建立時間ctime查找.:find . -ctime -1

将目前目錄及其子目錄下所有最近 1 天内建立過的檔案夾或檔案列出:find . -ctime -1

localhost:demo-linux gaoxinfu$ find . -ctime -1
.
./a
./a/test1.txt
./a/test2.txt
./a/test.txt
./b
localhost:demo-linux gaoxinfu$      

2.3.2.根據檔案更新時間mtime查找.:find . -mtime -1

将目前目錄及其子目錄下所有最近 1 天内更新過的檔案夾或檔案列出:find . -ctime -1

localhost:demo-linux gaoxinfu$ find . -mtime -1
.
./a
./a/test1.txt
./a/test2.txt
./a/test.txt
./b
localhost:demo-linux gaoxinfu$      

2.4.根據檔案大小查找:

-size n      

2.4.1.檢視檔案大于1k的檔案:find . -size 0

localhost:a gaoxinfu$ pwd
/Users/gaoxinfu/demo-linux/a
localhost:a gaoxinfu$ ls -la
total 16
drwxr-xr-x  5 gaoxinfu  staff   160  3 21 21:23 .
drwxr-xr-x  4 gaoxinfu  staff   128  3 21 20:45 ..
-rw-r--r--  1 gaoxinfu  staff  1948  3 21 21:23 test.txt
-rw-r--r--  1 gaoxinfu  staff    24  3 21 21:23 test1.txt
-rw-r--r--  1 gaoxinfu  staff     0  3 21 20:44 test2.txt      

查找0k的檔案

localhost:demo-linux gaoxinfu$ find . -size 0
./a/test2.txt
localhost:demo-linux gaoxinfu$      

2.4.2.檢視檔案大于1k的檔案:find . -size +1k

localhost:a gaoxinfu$ cd ..
localhost:demo-linux gaoxinfu$ pwd
/Users/gaoxinfu/demo-linux
localhost:demo-linux gaoxinfu$ find . -size +1k
./a/test.txt
localhost:demo-linux gaoxinfu$      

2.5.不同類型可以混合使用:find . -ctime -1 -type f

localhost:demo-linux gaoxinfu$ find . -ctime -1 -type f
./a/test1.txt
./a/test2.txt
./a/test.txt
localhost:demo-linux gaoxinfu$      

2.6.對查找完的結果進行操作:find . -size 0 -exec ls -la {} ;

2.6.1.列出檔案大小為0的檔案詳情:find . -size 0 -exec ls -la {} ;

localhost:demo-linux gaoxinfu$ find . -size 0 
./a/test2.txt
localhost:demo-linux gaoxinfu$ find . -size 0 -exec ls -la {} \;
-rw-r--r--  1 gaoxinfu  staff  0  3 21 20:44 ./a/test2.txt
localhost:demo-linux gaoxinfu$      

2.6.1.find 後面的-exec command {} \ 語句說明

command指令的終止,使用 ';' (分号)來判定,在後面必須有一個 ';'
'{}',使用{}來表示檔案名,也就是find前面處理過程中過濾出來的檔案,用于command指令進行處理前面過濾的檔案 指令操作
特别強調,對于不同的系統,直接使用分号;可能會有不同的意義, 使用轉義符 '\'在分号';'前一起使用明确說明 ,      

3.附:檢視檔案目錄比較大的檔案夾和檔案

du -h --max-depth=1 /home/*      

繼續閱讀