How to select all files added to a directory in the past 5 mins (HP-UX)?

Hey everyone,

I need to select all files that were added to a specific directory in the past 5 mins and copy them over to a different directory. I am using HP-UX OS which does not have support for amin, cmin, and mmin. B/c of this, I am creating a temp file and will use the find -newer command to compare files to a temporary file with an altered timestamp (5 mins ago). HP-UX does not support the -d option for the 'touch' command so I cannot do something like this:

touch -d "5 mins ago" temp

Q: Does anyone know how I can select files added to the directory in the past 5 mins?

Any help would be greatly appreciated.

Thanks,
Matt

GNU find has a -min option.

If you already have touch -d "5 mins ago" temp, you're probably already using GNU :slight_smile:

Unfortunately, HP-UX does not support the '-d' option for 'touch' and there is also no support for amin, cmin, and mmin for the 'find' command.

I now kinda wish I read your entire question before replying :o

what about perl in HP-UX?

find . | perl -ane 'chomp($_); $bef = time() - (5 * 60); $a = (lstat($_))[9]; if ($a > $bef) { print $_, "\n"}'
1 Like

I would use perl too, but to get the required start time. We can then create a reference file and use find based on that to select files.

Assuming you can't just use "date +%s", something like:-

#!/bin/ksh


date '+%Y %m %d %H %M %S' | read in_Y in_m in_d in_H in_M in_S                                   # Get current time values 
((in_m=$in_m-1))                                                                                 # Reduce month by one as epoch counts from 1/1/1970 as month zero

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

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 # Display answer in required format to be read in

touch -mt ${Y}${m}${d}${H}${M}.${S} /tmp/ref_file                                                # Create reference file

find . -type f -newer /tmp/ref_file                                                              # Find required files

It's a bit messy, but I hope you can see what is being done and why.

Does this help?

Robin

1 Like

Doing date delta can be done in many languages, but for shell the easiest is the GNU date (which you can add to your system, perhaps as a user local executable) or you can search here for my tool tm2tm.c and compile it.

I some situations, you can "touch marker_file ; sleep 300 ; find ... -newer marker_file ....", for instance if you want to poll a subtree every 5 minutes. If you want to avoid double reporting:

touch marker_old
 
while :
do
 sleep 300
 touch marker_new
 find ... -newer marker_old \( ! -newer marker_new \) ...
 mv -f marker_new marker_old
done
1 Like

Thanks, I was finally able to test this and it works great.