2016-01-19 81 views
0

我正在構建一個備份腳本,並且正在使用find -mtime。 昨天我用了很多find -mtime +1來搜索一天前修改過的文件。 在一天結束時,我整天使用的命令停止工作。我打破了Ubuntu的FIND命令,mtime不能正常工作

[email protected]:~$ mkdir test 
[email protected]:~$ cd test/ 
[email protected]:~/test$ touch -t 201601180830 yesterdayMorning 
[email protected]:~/test$ touch -t 201601181725 yesterdayAfternoon 
[email protected]:~/test$ ll 
total 32 
drwxrwxr-x 2 user user 4096 Jan 19 09:37 ./ 
drwx------ 9 user user 12288 Jan 19 09:36 ../ 
-rw-rw-r-- 1 user user  0 Jan 18 17:25 yesterdayAfternoon 
-rw-rw-r-- 1 user user  0 Jan 18 08:30 yesterdayMorning 

FIND -mtime n的結果

[email protected]:~/test$ find -mtime +1 
[email protected]:~/test$ find -mtime -1 
. 
./yesterdayAfternoon 
[email protected]:~/test$ find -mtime 0 
. 
./yesterdayAfternoon 
[email protected]:~/test$ 

我應該能夠找到一個名爲yesterdayMorning該文件,因爲當時我正在寫(上午09點48分的1月19日),該文件大於1天。

find -mtime -1(或0 too)顯示正確的結果,因爲該文件的最後修改時間少於24小時。

昨天下午5點前,我發誓它工作!

+0

你有沒有嘗試過的'-daystart'選項? –

+0

不,只在昨天全天使用-mtime。可能太多了..我最終忘記了用於哪種情況的確切mtime。 – Nihvel

回答

1

它實際上不是24小時前,但超過n天前。即對於-mtime +1,它必須在兩天前進行修改。

使用find -mtime +0來匹配昨天的文件。

+0

我也試過這個,但沒有任何結果複製在主要問題中,所以你也可以看到 編輯:我的壞我創建了錯誤的小時文件。是mtime +1顯示2天以前的文件。什麼??我以某種方式管理昨天顯示文件超過24小時與-mtime +1 – Nihvel

+0

-mtime +0(數學不正確)顯示我什麼我想要的!我很驚訝..謝謝隊友! – Nihvel

1

正如接受的答案-mtime +0中所述,在這種情況下適用於你。 注:

find using -mtime and -daystart 
-mtime n 
    File's data was last modified n*24 hours ago. 
-daystart 
    Measure times (for -amin, -atime, -cmin, -ctime, -mmin, and 
    -mtime) from the beginning of today rather than from 24 hours 
    ago. 
    This option only affects tests which appear later on the 
    command line. 

date 
Tue Jan 19 10:24:43 CET 2016 
~/test $ ls -n 
total 0 
-rw-r--r-- 1 1000 1000 0 Jan 18 10:15 yesterdayMorning10:15.txt 
-rw-r--r-- 1 1000 1000 0 Jan 18 10:45 yesterdayMorning10:45.txt 
~/test $ find -mtime +0 
./yesterdayMorning10:15.txt 

~/test $ find -mtime 0 
./yesterdayMorning10:45.txt 

~/test $ find -daystart -mtime +0 
./yesterdayMorning10:15.txt 
./yesterdayMorning10:45.txt 
+0

這是一個很好的解釋!沒有使用daystart,但似乎我會改進腳本。幹得好(謝謝! – Nihvel