2012-09-26 76 views
0

我需要做的是按另一個文件的創建時間查找文件。例如,如果我在上午9點創建一個文件,然後我想查找所有在它之後1小時或者在它之前1小時創建的文件。我會怎麼做?按另一個文件的創建時間查找文件

我嘗試過使用「find」來試驗「-newer」,但我認爲「xargs」是我需要使用的。

感謝

+0

在linux上不存儲文件創建時間,只存取,修改和更改時間(http://unix.stackexchange.com/questions/24441/get-file-created-creation-time)。如果用'創建時間'代替'修改時間',您的需求是否仍然存在? – imp25

+0

是的,這是可以接受的。 – Nexus490

回答

0

看着這之後,我發現了一個辦法做到這一點,但它是不是最好的解決方案,因爲它需要將按時完成整數運算。

這個想法是從你的參考文件中取得Unix紀元(又名Unix時間)以來的秒數,對此做一些整數運算以獲得你的偏移時間(在你的例子中的前一小時或後一小時)。然後使用-newer參數使用查找。

示例代碼:

# Get the mtime of your reference file in unix time format, 
# assumes 'reference_file' is the name of the file you're using as a benchmark 
reference_unix_time=$(ls -l --time-style=+%s reference_file | awk '{ print $6 }') 

# Offset 1 hour after reference time 
let unix_time_after="$reference_unix_time+60*60" 

# Convert to date time with GNU date, for future use with find command 
date_time=$(date --date @$unix_time_after '+%Y/%m/%d %H:%M:%S') 

# Find files (in current directory or below)which are newer than the reference 
# time + 1hour 
find . -type f -newermt "$date_time" 

爲了您的高達備查文件一小時前創建的文件例如,你可以使用

# Offset 1 hour before reference time 
let unix_time_before="$reference_unix_time-60*60" 

# Convert to date time with GNU date... 
date_time=$(date --date @$unix_time_before '+%Y/%m/%d %H:%M:%S') 

# Find files (in current directory or below which were generated 
# upto 1 hour before the reference file 
find . -type f -not -newermt "$date_time" 

注意,上述所有基於的最後修改時間文件。

上面已經過GNU Find(4.5.10),GNU Date(8.15)和GNU Bash(4.2.37)的測試。

相關問題