Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

linux find 指令:找出比某檔案新、又比另一檔案舊的檔案

想找出「修改時間比 A 新、但比 B 舊」的所有檔案,find 的 -newer 選項就能勝任。它的一般形式為:

newest_file_name ! oldest_file_name

其中 ! 是邏輯非符號。例如:

$ find ./ -newer age.txt ! -newer belts.txt -exec ls -l {} \;

這條指令會找出修改時間比 age.txt 新、卻比 belts.txt 舊的檔案。

用 touch 打造「時間基準檔」

若想查找「兩小時以內」修改的檔案,通常手上並沒有剛好兩小時前修改的檔案可當基準。這時可以先用 touch 自造一個時間戳正好符合需求的檔案。

假設現在是 23:40,想查找兩小時內的檔案,就先建立一個時間為 21:40 的基準檔:

$ touch -t 05042140 time.txt
$ ls -l time.txt
-rw-r--r-- root root 0 May 4 21:40 time.txt

接著就能用 -newer 在當前目錄下找出所有兩小時內修改的檔案:

$ find ./ -newer time.txt -print

另一個實用小技巧,是先用 touch 造出起訖時間兩個基準檔,再搭配 find 做區間篩選:

# touch -u -t 201212171700 startemp
# touch -u -t 201212171740 endtemp
# find ./ -newer startemp ! -newer endtemp -exec ls -l {} \;
Copyright © Linux教程網 All Rights Reserved