私がする必要があるのは、別のファイルの作成時間までにファイルを見つけることです。たとえば、午前9時以降にファイルを作成した場合、その1時間後または1時間前に作成されたすべてのファイルを検索したいと思います。どうすればいいですか?
「find」を使用しながら「-newer」を試してみましたが、「xargs」を使用する必要があると思います。
ありがとう
これを見た後、これを行う方法を見つけましたが、整数演算を時間通りに行う必要があるため、これは最も良い解決策ではありません。
アイデアは、参照ファイルからUnixエポック(別名Unix時間)からの秒数を取得し、これに対して整数演算を行ってオフセット時間(例では1時間前または後)を取得することです。次に、パラメーターで find を使用し-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"
参照ファイルの最大 1 時間前に作成されたファイルの例では、使用できます
# 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) でテストされています。