linux中find命令的用法

同grep一樣,這是一個shell中用法非常豐富,重要性很大的命令,所以我也用一篇文章總結(jié)一下其用法。

先用一張圖片展示命令的基本語法格式:

Slice_find-crunch.png

基本、常用命令組合方式

find . -name ‘filename’
基于文件名搜索。把-name換成-iname,可以忽略文件名的大小寫

find . -path ‘*/aireason/*’
-name 換成 -path,會搜索整個路徑名,而不僅僅是文件名

-regex參數(shù)和-path用法類似,只不過前者用正則表達式匹配路徑名;-iregex忽略文件名的大小寫
find . -iregex “.*\(\.py\|\.sh\)$”
疑問:為什么要加反斜杠?

能用!表達否定
比如find . ! -name “*.txt”會找到所有不以txt為擴展名的文件

find默認對目錄進行遞歸搜索,直到遍歷所有制定目錄下的文件和文件夾。但可以用-maxdepth 、-mindepth來限定搜索的深度
最好先制定搜索深度,再指定搜索的文件類型,這樣搜索效率高

限定搜索的文件類型

-type參數(shù)來限定文件類型,有下面幾種類型

普通文件 f
符號鏈接 l
目錄 d
字符設(shè)備 c
塊設(shè)備 b
套接字 s
FIFO p

與時間有關(guān)的參數(shù) ??

指定時間

  • -atime: time of last access (ls -lu)
  • -mtime: time of last modification (ls -l)
  • -ctime: time of last status change (ls -lc)

-mmin, -amin,-cmin以分鐘為單位,其他含義相同

三者用法類似,后面接一個整數(shù),可以不帶正負號,或者帶正號、負號。下面是用法圖解,間距是24h(-mtime)或一分鐘(-mmin)

Group 388-crunch.png

比如 find . -type f -mtime +3代表查找當(dāng)前目錄下,訪問時間在距此刻四天(4*24h)之前的所有文件

指定參考文件

另一個和時間有關(guān)的參數(shù)是-newer,這個參數(shù)帶上一個參考文件,可用于尋找比參考文件更新的(更近的修改時間)的文件。
find . -newer file.txt,就能找到比file.txt更近時間修改的文件。

限定文件大小 ??

-size [+/-]number[k, b, c, w...]
后面接帶正號或負號的整數(shù),正好表示比數(shù)字大的文件,數(shù)字后面再接上單位,單位如下所示

?? b: 512 byte blocks (塊,512字節(jié))
?? c: Bytes (字節(jié))
?? w: Two-byte words (字,兩字節(jié))
?? k: Kilobyte (1024 bytes)
?? M: Megabyte (1024 kilobytes)
?? G: Gigabyte (1024 megabytes)

find . -type f -name '*.pdf' -size +50000k -exec ls -l {} \;
這句話可以查找文件大于50兆的pdf文件

-delete

-delete可用來刪除所匹配到的文件,比如下面命令可以刪除所有以swp為擴展名的文件
find . -type f -name "*.swp" -delete

Match based on the file permissions and ownership

It is possible to match files based on the file permissions. We can list out the files having specified file permissions as follows:
find . -type f -perm 644 -print # Print files having permission 644

-perm specifies that find should only match files with their permission set to a particular value.

As an example usage case, we can consider the case of the Apache web server. The PHP files in the web server require proper permissions to execute. We can find out the PHP files that don’t have proper execute permissions as follows:
find . -type f -name “*.php” ! -perm 644 -print

We can also search files based on ownership of the files. The files owned by a specific user can be found out using the -user USER option.
The USER argument can be a username or UID.

For example, to print the list of all files owned by the user slynux, you can use the following command:
find . -type f -user slynux -print

使用-exec對找到的文件進行進一步操作

用法: ::-exec COMMAND {} ;::

find . -name "*.c" -exec cat {} \; > all_c.txt
解釋一下,{}代表每個找到的文件,后面要用;結(jié)尾,但是分號在shell中有特殊含義,所以需要轉(zhuǎn)義。
然后,find命令的全部輸出只是一個數(shù)據(jù)流,所以不需要使用>>

-exec后面不支持接多個命令,但是可以把多個命令寫到腳本里,然后執(zhí)行腳本。

我經(jīng)常這樣使用該命令:
find . -name "*.sh" -exec ls -l {} \;

讓find跳過某些目錄

使用-prune(修剪)參數(shù)
find . \( -name ".git" -prune \) -o \( -type f \)
此命令打印出不包括.git目錄在內(nèi)的所有文件,分為兩部分,第一部分\( -name ".git" -prune \)用來排除某文件夾,第二部分\( -type f \)說明了要執(zhí)行的動作

其他搜索文件的命令

Use a simpler command

Generally, source for a project is likely to be in one place, perhaps in a few subdirectories nested no more than two or three deep, so you can use a (possibly) faster command such as
cd /path/to/project; ls *.c */*.c */*/*.c

Speeding up locate

Ensure it is indexing the locations you are interested in. Read the man page and make use of whatever options are appropriate to your task.

   -U <dir>
          Create slocate database starting at path <dir>.

   -d <path>
          --database=<path> Specifies the path of databases to search  in.


   -l <level>
          Security  level.   0  turns  security checks off. This will make
          searchs faster.   1  turns  security  checks  on.  This  is  the
          default.

I used the "speeding up locate" part of RedGrittyBrick's answer. I created a smaller db:

updatedb -o /home/benhsu/ben.db -U /home/benhsu/ -e "uninteresting/directory1 uninteresting/directory2"

then pointed locate at it: locate -d /home/benhsu/ben.db

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。