如何 select 过去 5 分钟内添加到目录中的文件 - HP-UX

How to select files that were added to a directory in the past 5 mins - HP-UX

我需要 select 将过去 5 分钟内添加到特定目录的所有文件复制到另一个目录。我正在使用不支持 amin、cmin 和 mmin 的 HP-UX OS。 B/c 其中,我正在创建一个临时文件,并将使用 find -newer 命令将文件与具有更改时间戳(5 分钟前)的临时文件进行比较。 HP-UX 不支持 'touch' 命令的 -d 选项,所以我不能这样做:

touch -d "5 mins ago" temp

我尝试使用以下解决方案,但在执行时收到错误(非法变量名):

TZ=ZZZ0 touch -t "$(TZ=ZZZ0:5 date +%Y%m%d%H%M.%S)" temp

问: 有谁知道我如何在过去 5 分钟内将 select 文件添加到目录中,而无需操作时间标签(分钟、天、几个月,...)?

注意:我的脚本将每 5 分钟 运行,但我需要将解决方案包含在脚本中(即,不依赖于它将 运行 每 5 分钟)。我不能硬编码时间戳。

谢谢,

马特

当您的脚本设计为每 5 分钟 运行 时,下一个解决方案可能有效:

#!/bin/ksh
TMPFILE=/tmp/[=10=].$$
touch ${TMPFILE}
sleep 300
find somedir -newer ${TMPFILE} | while read file; do
   something_with_file ${file}
done
rm ${TMPFILE}

您的 something_with_file 应在 5 分钟内完成,您不希望处理同一文件的脚本的不同执行。
延迟 5 分钟应该不是问题,因为您每 5 分钟启动一次。

我能够通过使用以下代码块获得所需的功能:

# Fill date variables 
date '+%Y %m %d %H %M %S' | read in_Y in_m in_d in_H in_M in_S

# Decrease month count to account for first month being at index 0
((in_m=$in_m-1)) 

# Get current time in seconds since epoch
perl -e "use Time::Local; print timelocal($in_S,$in_M,$in_H,$in_d,$in_m,$in_Y), ;" | read cur_S

# Go back five minutes
((old_S=$cur_S-300))

# Change to required format
perl -e 'use POSIX qw(strftime);\
print scalar(strftime "%Y %m %d %H %M %S", localtime $ARGV[0]), "\n";' $old_S | read Y m d H M S

# Create temp file to act as time reference
touch -amt ${Y}${m}${d}${H}${M}.${S} ${tempFileDest}

cd $testSourceDir

# Find files created in past five minutes
find . -type f -newer temp -name $fileNameFormatTest -exec cp -pf {} $testDestDir \;

感谢您的帮助!