0

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

I have attempted to use the following solution, but receive an error (Illegal variable name) when I do:

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

Q: Does anyone know how I can select files added to the directory in the past 5 mins without needing to manipulate the time tag (minutes, days, months, ...)?

Note: My script will run every 5 mins, but I need the solution to be contained within the script (i.e., not depend on the fact that it will run every 5 mins). I cannot hardcode the timestamp.

Thanks,

Matt

Marcus Koz
  • 255
  • 5
  • 21
  • When the script hasn't run for 8 minutes, do you still only want 5 minutes? To get the "not-copied" files, look for the last copied file, and search for files newer than that one. Otherwise you need to call a script that calculates a timestamp 5 minutes ago and use that for the touch. – Walter A Feb 16 '15 at 18:29
  • Solution not compliant with your note: Make a crontab entry touching a file every 5 minutes / finish your script (that is started by cron?) with a touch temp. Maybe change your requirements? – Walter A Feb 16 '15 at 18:35
  • Are you really using `TZ=ZZZ0` ? I don't recognize that as a valid entry, but I'm happy to learn something new ; -) Good luck. – shellter Feb 16 '15 at 18:55
  • @shellter No I am not using `TZ=ZZZ0`. It was part of a potential solution, but it does not work for me. I receive a "illegal variable name" error. Thanks – Marcus Koz Feb 16 '15 at 19:59
  • @WalterA I am attempting to calculate a timestamp (5 mins ago) and use touch as you stated in your first comment. However, I would like a solution to calculating the timestamp that does not force me to to deal with carrying over of hours, days, and months (11:55 -> +5 mins -> 12:00). – Marcus Koz Feb 16 '15 at 20:03
  • 2
    You don't need to use `touch -d`. Just use the following logic: 1) touch `temp2` 2) sleep at least 1 second, since that's the granularity of `find`. 3) find all files newer than `temp` and process them 4) `mv temp2 temp` 5) sleep however long you want, then go to 1). – Mark Plotnick Feb 16 '15 at 20:29
  • I'm not sure if I understand your logic. The script will run every 5 mins (no choice of mine). Would that not cause an issue with sleeping for that duration? Also, what effect does sleep have on any other processes that are being run? @MarkPlotnick – Marcus Koz Feb 16 '15 at 20:38
  • 1
    If you're implementing it by running a whole new process every 5 minutes instead of sleeping for 5 minutes, just don't do the last step that sleeps. But you'll also need to account for the case that it takes longer than 5 minutes to process all the files. You don't want more than one instance of your script to be running simultaneously. Probably need to use some kind of locking. – Mark Plotnick Feb 16 '15 at 20:42
  • 1
    I was searching for a long script I found somewhere with all kind of date/time calculations supported. Now I found something that sounds very simple: http://blog.fpmurphy.com/2008/10/ksh93-date-manipulation.html Does that work for you ? – Walter A Feb 16 '15 at 21:25
  • Thanks for the continued help. Unfortunately, when running those commands I get an error (printf: Error processing format now). @WalterA – Marcus Koz Feb 16 '15 at 21:41
  • First check out http://stackoverflow.com/questions/6467/date-arithmetic-in-unix-shell-scripts for more possibilities or go for my answer of sleeping 300 sec before doing your thing. After 5 minutes that should work fine. – Walter A Feb 16 '15 at 22:11

2 Answers2

0

When your script is designed to run every 5 minutes, the next solution might work:

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

Your something_with_file should finish in 5 minutes, you do not want different executions of your script processing the same file.
The 5 minutes delay shouldn't be a problem, since you start it every 5 minutes.

Walter A
  • 19,067
  • 2
  • 23
  • 43
0

I was able to get the functionality that I needed by using the following code block:

# 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 \;

Thanks for the help!

Marcus Koz
  • 255
  • 5
  • 21
  • Hi Matt. Taking in mind that I do not know `perl` programming enough, would it possible to reduce the first lines of your script, provided that perl can compute simple arithmetic operations as `cur_S-300`? – Jdamian Feb 20 '15 at 17:31