Marcus Koz
Marcus Koz

Reputation: 255

How to copy all files added to a specific directory in the past 5 minutes?

I would like to know how to copy over files that were added to a specific directory in the past five minutes. The files are not necessarily created within the directory (i.e., may be copied over from a different directory). The files have a specific format which includes the date and time of creation, however I would prefer if the selection of the files is not dependent on the date/time stamp within the file name. How can this be done?

Upvotes: 0

Views: 1023

Answers (2)

Marcus Koz
Marcus Koz

Reputation: 255

I was able to get the functionality that I needed by using this 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!

Note: This is a work around for HP-UX which does not support the -amin, -cmin, -mmin options for the 'find' command.

Upvotes: 0

Two-Bit Alchemist
Two-Bit Alchemist

Reputation: 18467

Changing the directory of a file does not update modified time or access time, but does update change time. You simply need to use the -ctime flag on find to catch this:

$ mkdir test
mkdir: created directory ‘test’
$ cd test
$ touch a
$ stat a
  File: ‘a’
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d  Inode: 2117512     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/moriarty)   Gid: (  100/   users)
Access: 2015-02-13 10:24:16.777863605 -0600
Modify: 2015-02-13 10:24:16.777863605 -0600
Change: 2015-02-13 10:24:16.777863605 -0600
 Birth: -
$ mkdir b
mkdir: created directory ‘b’
$ mv a b
$ stat b/a
  File: ‘b/a’
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d  Inode: 2117512     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/moriarty)   Gid: (  100/   users)
Access: 2015-02-13 10:24:16.777863605 -0600
Modify: 2015-02-13 10:24:16.777863605 -0600
Change: 2015-02-13 10:24:26.354530678 -0600
 Birth: -
$ find b -cmin -5
b
b/a

If you want find to ignore the directory just pass it a -type f meaning only files are to be considered.

$ find b -type f -cmin -5
b/a

Then you can use its -exec argument to pass it the cp command, which it will execute over every result. Use -print or just the bare command above (no -exec) to see what it is catching.

$ mkdir c
mkdir: created directory ‘c’
$ find b -type f -cmin -5 -exec cp '{}' c \;
$ ls c
a

Edit:

The only way I can think of to do this on a system like HP-UX that doesn't support -cmin is basically to create a temporary dummy file with a timestamp indicating the time you want (five minutes ago in your case) and compare against that. You can set the timestamp on a Unix file as you desire with touch -t and then use the -newerc argument to find to select files with a newer change time than the standard one you just made. I found an online reference purporting to be a manual for HP-UX from around 2007 that indicates that it does support the newer[xy] syntax.

Upvotes: 2

Related Questions