Check file time stamp

Hi,

I need help to read file in a directory on basis of time stamp.
e.g. If file access in last 2 minutes it should not be copy to remote directory.

Below is my script.

#!/bin/ksh
 
DATE=`date +"%Y-%m-%d_%H%M"`
SEPARATER=" "
exec < out_interfaces.cfg
while read source_path destination_path
do
 
cp $source_path destination_path
 
done

out_interfaces.cfg file has below data

/usr/local1/file1 /usr/remote1
/usr/local2/file2 /usr/remote2
/usr/local3/file3 /usr/remote3

Thanks,

Qamar

What's your system?

Also, why do you need to avoid copying these files? Are these files being uploaded by FTP or the like?

Please present the problem, as well as your suggested solution.

If you are trying to avoid copying a file while a program it still writing that file, consider using the unix fuser command to determine that fact.
Conventional system design would transfer files into a relay directory under a temporary filename, and rename each file after a successful transfer.

What's your system?
-My system is AIX.

Also, why do you need to avoid copying these files? Are these files being uploaded by FTP or the like?
-These files generated from other programs my script will run every 5 minutes through crontab.
I just want to check while copy these files the original script who writes these files on source should not be writing in the file while my script is running.
Once the copy finished I will delete this file.

Why not move the file?

As long as the destination is on the same filesystem, moving a file that's being written to is completely safe. Nothing impacts already-open file descriptors -- all you're doing is changing directory entries.

Otherwise, the cron entries responsible for creating the file should really be responsible for moving it, too. They actually know when they're done, no wild guess of two minutes is necessary.

No I can't move the file. because in some cases I need to copy same file in 2 different directories.
So once I copy the file on destination I will redirect source_path in a tmp file. once completed copying all the files I will again do a loop on my tmp file and delete all files which copied.

The cron entries responsible for creating the file should really be responsible for moving it, too. They actually know when they're done, no wild guess of two minutes is necessary.

The program who is writing on the source is opening and closeing file multiple times in 1 minutes.

Please post what Operating System and version you have. We need to know whether your find command has the -mmin switch of whether it just has the usual accuracy of 24 hours.

1 Like

Then even if we do what you ask it will never work, because it would wait for 2 minutes -- which would never happen...

Even if it does manage two minutes, waiting 2 minutes is no guarantee the program wouldn't start up again in the middle.

You need to cooperate with whatever's creating the files somehow, get information from it. Perhaps even halt it during the time you're copying files.

find command is working fine for me. But I need to redirect destination path also in my tmp_file.

exec < out_interfaces.cfg
while read source_path $destination_path
do
find $source_path -name "*" -amin +2 >> tmp_file
done

out_interfaces.sfg file has below data

/usr/local/scripts/tmp1 /usr/local/scripts/remote1
/usr/local/scripts/tmp2 /usr/local/scripts/remote2
/usr/local/scripts/tmp2 /usr/local/scripts/remote3
/usr/local/scripts/tmp3 /usr/local/scripts/remote3

and when I execute above script it shows below data in my tmp_file.

/usr/local/scripts/tmp1/test1
/usr/local/scripts/tmp2/tmp21
/usr/local/scripts/tmp2/tmp22
/usr/local/scripts/tmp2/tmp23
/usr/local/scripts/tmp2/tmp21
/usr/local/scripts/tmp2/tmp22
/usr/local/scripts/tmp2/tmp23
/usr/local/scripts/tmp3/tmp31
/usr/local/scripts/tmp3/tmp32
/usr/local/scripts/tmp3/tmp33

and I want below output in my tmp_file

/usr/local/scripts/tmp1/test1 /usr/local/scripts/remote1
/usr/local/scripts/tmp2/tmp21 /usr/local/scripts/remote2
/usr/local/scripts/tmp2/tmp22 /usr/local/scripts/remote2
/usr/local/scripts/tmp2/tmp23 /usr/local/scripts/remote2
/usr/local/scripts/tmp2/tmp21 /usr/local/scripts/remote3
/usr/local/scripts/tmp2/tmp22 /usr/local/scripts/remote3
/usr/local/scripts/tmp2/tmp23 /usr/local/scripts/remote3
/usr/local/scripts/tmp3/tmp31 /usr/local/scripts/remote3
/usr/local/scripts/tmp3/tmp32 /usr/local/scripts/remote3
/usr/local/scripts/tmp3/tmp33 /usr/local/scripts/remote3

---------- Post updated at 05:30 PM ---------- Previous update was at 11:58 AM ----------

Thank you everyone. My problem has been solved. I used below code.

 exec < out_interfaces.cfg
 while read source_path destination_path
 do
 {
 find $source_path -name "*" -amin +2 -exec echo $destination_path {} \;
 
 } >> tmp_file