2015-10-29 17 views
17

我正在嘗試查找在過去x分鐘內修改的文件,例如最近一小時。在網上許多論壇和教程建議使用find命令和選項-mmin,像這樣:如何查找在最近x分鐘內修改的文件(發現-mmin無法按預期工作)

find . -mmin -60 |xargs ls -l

但是,該命令沒有爲我正常工作。正如你可以從下面的列表,請參閱,這也顯示了對修飾早於1小時前的文件:

-rw------- 1 user user 9065 Oct 28 23:13 1446070435.V902I67a5567M283852.harvester 
-rw------- 1 user user 1331 Oct 29 01:10 1446077402.V902I67a5b34M538793.harvester 
-rw------- 1 user user 1615 Oct 29 01:36 1446078983.V902I67a5b35M267251.harvester 
-rw------- 1 user user 72365 Oct 29 02:27 1446082022.V902I67a5b36M873811.harvester 
-rw------- 1 user user 69102 Oct 29 02:27 1446082024.V902I67a5b37M142247.harvester 
-rw------- 1 user user 2611 Oct 29 02:34 1446082482.V902I67a5b38M258101.harvester 
-rw------- 1 user user 2612 Oct 29 02:34 1446082485.V902I67a5b39M607107.harvester 
-rw------- 1 user user 2600 Oct 29 02:34 1446082488.V902I67a5b3aM465574.harvester 
-rw------- 1 user user 10779 Oct 29 03:27 1446085622.V902I67a5b3bM110329.harvester 
-rw------- 1 user user 5836 Oct 29 03:27 1446085623.V902I67a5b3cM254104.harvester 
-rw------- 1 user user 8970 Oct 29 04:27 1446089232.V902I67a5b3dM936339.harvester 
-rw------- 1 user user 165393 Oct 29 06:10 1446095400.V902I67a5b3eM290158.harvester 
-rw------- 1 user user 105054 Oct 29 06:10 1446095430.V902I67a5b3fM265065.harvester 
-rw------- 1 user user 1615 Oct 29 06:24 1446096244.V902I67a5b40M55701.harvester 
-rw------- 1 user user 1620 Oct 29 06:24 1446096292.V902I67a5b41M337769.harvester 
-rw------- 1 user user 10436 Oct 29 06:36 1446096973.V902I67a5b42M707215.harvester 
-rw------- 1 user user 7150 Oct 29 06:36 1446097019.V902I67a5b43M415731.harvester 
-rw------- 1 user user 4357 Oct 29 06:39 1446097194.V902I67a5b56M446687.harvester 
-rw------- 1 user user 4283 Oct 29 06:39 1446097195.V902I67a5b57M957052.harvester 
-rw------- 1 user user 4393 Oct 29 06:39 1446097197.V902I67a5b58M774506.harvester 
-rw------- 1 user user 4264 Oct 29 06:39 1446097198.V902I67a5b59M532213.harvester 
-rw------- 1 user user 4272 Oct 29 06:40 1446097201.V902I67a5b5aM534679.harvester 
-rw------- 1 user user 4274 Oct 29 06:40 1446097228.V902I67a5b5dM363553.harvester 
-rw------- 1 user user 20905 Oct 29 06:44 1446097455.V902I67a5b5eM918314.harvester 

其實,它只是列出了當前目錄下的所有文件。我們可以把這些文件作爲例子之一,並檢查其修改時間確實是ls命令中顯示:

stat 1446070435.V902I67a5567M283852.harvester 

    File: ‘1446070435.V902I67a5567M283852.harvester’ 
    Size: 9065  Blocks: 24   IO Block: 4096 regular file 
Device: 902h/2306d Inode: 108680551 Links: 1 
Access: (0600/-rw-------) Uid: (1001/ user) Gid: (1027/ user) 
Access: 2015-10-28 23:13:55.281515368 +0100 
Modify: 2015-10-28 23:13:55.281515368 +0100 
Change: 2015-10-28 23:13:55.313515539 +0100 

我們可以看到,這個文件肯定是最後一次修改超過1個小時前!我也試過find -mmin 60find -mmin +60,但它也沒有工作。

爲什麼會發生這種情況,以及如何正確使用find命令?

回答

30

如果在最近一小時內修改的目錄中有沒有文件,我可以重現您的問題。在這種情況下,find . -mmin -60什麼都不返回。但是,命令find . -mmin -60 |xargs ls -l會返回與ls -l運行時沒有參數時發生的一致的目錄中的每個文件。

爲了確保ls -l當一個文件被發現只運行,嘗試:

find . -mmin -60 -type f -exec ls -l {} + 
+1

Ummm..clever .. + 1 – heemayl

+0

您還可以使用-r標誌xargs的,以阻止其運行,如果沒有輸入。當然,我也強烈建議在使用xargs時防止文件名中的空格造成問題。例如:'查找。 -mmin 60 -print0 | xargs -0r ls -l'。除了主動攻擊者發現之外,Newline通常是非常安全的。 -mmin 60 | xargs'-rd \ n'ls -l'上面的答案中的-exec選項當然也解決了這兩個問題。 –

-1

這個命令可以幫助你先生

find -type f -mtime -60 
+0

find -type f -mtime -90 –

+0

這會找到超過60天** **的文件,而不是分鐘。 – MSalters

2

手冊發現的:

Numeric arguments can be specified as 

    +n  for greater than n, 

    -n  for less than n, 

    n  for exactly n. 

    -amin n 
      File was last accessed n minutes ago. 

    -anewer file 
      File was last accessed more recently than file was modified. If file is a symbolic link and the -H option or the -L option is in effect, the access time of the file it points to is always 
      used. 

    -atime n 
      File was last accessed n*24 hours ago. When find figures out how many 24-hour periods ago the file was last accessed, any fractional part is ignored, so to match -atime +1, a file has to 
      have been accessed at least two days ago. 

    -cmin n 
      File's status was last changed n minutes ago. 

    -cnewer file 
      File's status was last changed more recently than file was modified. If file is a symbolic link and the -H option or the -L option is in effect, the status-change time of the file it points 
      to is always used. 

    -ctime n 
      File's status was last changed n*24 hours ago. See the comments for -atime to understand how rounding affects the interpretation of file status change times. 

例如:

find /dir -cmin -60 # creation time 
find /dir -mmin -60 # modification time 
find /dir -amin -60 # access time 
0

這可能適合你。我使用它在部署期間清理文件夾以刪除舊的部署文件。

clean_anyfolder() { 
    local temp2="$1/**"; //PATH 
    temp3=($(ls -d $temp2 -t | grep "`date | awk '{print $2" "$3}'`")) 
    j=0; 
    while [ $j -lt ${#temp3[@]} ] 
    do 
      echo "to be removed ${temp3[$j]}" 
      delete_file_or_folder ${temp3[$j]} 0 //DELETE HERE 
     fi 
     j=`expr $j + 1` 
    done 
} 
8

的問題是,

find . -mmin -60 

輸出:

. 
./file1 
./file2 

注意一個點線?
這使得ls列出了與執行ls -l .時完全相同的整個目錄。

一種解決方案是僅列出文件(不是目錄):

find . -mmin -60 -type f | xargs ls -l 

但最好是直接使用選項-exec的find:

find . -mmin -60 -type f -exec ls -l {} \; 

或者只是:

find . -mmin -60 -type f -ls 

其中,順便說一下,即使包括目錄也是安全的:

find . -mmin -60 -ls 
+0

謝謝!這個點正是問題的根源! – hashbang

+0

@hashbang通常的感謝方式是對答案進行投票。如果需要,請點擊答案左側的投票數上方的向上箭頭。 –

0

其實這裏有一個以上的問題。主要的是xargs默認執行你指定的命令,即使沒有參數傳遞。要改變這種狀況,你可以使用一個GNU擴展xargs

--no-運行,如果空
-r
如果標準輸入不包含任何非空白,不運行命令。通常,即使沒有輸入,該命令也會運行一次。這個選項是一個GNU擴展。

簡單的例子:

find . -mmin -60 | xargs -r ls -l 

但是這可能會匹配所有子目錄,包括.(當前目錄),並ls將列出他們每個人單獨。所以輸出會很亂。解決方案:通過向-dls,以禁止列出目錄內容:

find . -mmin -60 | xargs -r ls -ld 

現在你不喜歡.(當前目錄)在您的名單?解決方案:排除發現輸出的第一個目錄級別(0):

find . -mindepth 1 -mmin -60 | xargs -r ls -ld 

現在,你只需要在你的列表中的文件?解決方案:排除目錄:

find . -type f -mmin -60 | xargs -r ls -l 

現在你有一些名稱包含空格,引號或反斜槓的文件嗎?解決方法:使用空值終止輸出(查找)和輸入(xargs的)(這些也都是GNU的擴展,據我所知):

find . -type f -mmin -60 -print0 | xargs -r0 ls -l 
2

我通過同樣需要工作,我相信你的時間表是不正確。

嘗試這些:

  • 15分鐘的變化:找到。 -mtime -.01
  • 1小時變更:查找。 -mtime -.04
  • 12小時更改:查找。 -mtime -.5

您應該使用24小時作爲您的基礎。 -time後的數字應該是24小時。因此-.5相當於12小時,因爲12小時是24小時的一半。

0

要搜索/目標目錄的文件及其所有子目錄,已經修改了最近60分鐘:

$ find /target_directory -type f -mmin -60 

要查找最近修改的文件,在更新的反向排序時間(即,最近更新的文件在前):

$ find /etc -type f -printf '%TY-%Tm-%Td %TT %p\n' | sort -r 
相關問題