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?
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.
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: