Search code examples
bashshellunixkshhp-ux

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


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


Solution

  • 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!