Operating System - HP-UX
1834602 Members
4020 Online
110069 Solutions
New Discussion

Bourne Shell moving/copying files ≥nerating date&time stamp

 
SOLVED
Go to solution
Laurie A. Krumrey
Regular Advisor

Bourne Shell moving/copying files ≥nerating date&time stamp

Hi All,

I have a script that copies files every min
in cron if they are present. Files can
be the same name and we do not want to over
write the file name but append the date and
time stamp.

SCRIPT:
find /virtual/easttrust/upload -type f |xargs -i cp {} /hal/easttrust/download
find /virtual/easttrust/upload -type f |xargs -i mv {} /tmp

For output I want this in both my directories:

filename.date and time stamp

Then I want to delete any files over 30 days
old so I don't get a disk full error.

Thanks,
Laurie
Happiness is a choice
6 REPLIES 6
Jeff Machols
Esteemed Contributor

Re: Bourne Shell moving/copying files ≥nerating date&time stamp

find . -mtime +30 | xargs rm
Uday_S_Ankolekar
Honored Contributor

Re: Bourne Shell moving/copying files ≥nerating date&time stamp

Hi,

You can make use of this variable to have date and time stamp

DATE=`/usr/bin/date '+%H%M'`

-USA..

Good Luck..
Steven Gillard_2
Honored Contributor

Re: Bourne Shell moving/copying files ≥nerating date&time stamp

Use date with the +format option (see the date man page), eg:

EXT=`date +%Y%m%d-%H%M`
for FILE in `find /hal/easttrust/upload -type f` ; do
cp $FILE /hal/easttrust/download/${FILE}.${EXT}
mv $FILE /tmp/${FILE}.${EXT}
done

Then to remove the old files just use find with -mtime +30. Maybe move the files to /tmp/old_upload or something instead of /tmp so you can limit the find command to those files only.

Regards,
Steve
Sanjay_6
Honored Contributor

Re: Bourne Shell moving/copying files ≥nerating date&time stamp

Hi Laurie,

The way i see it, you should first change the name of the files in the source directory (upload) to look like filename.date and then move them to the other directory. you can then put a cron job to delete all files older than 30 days.

Hope this helps.

Regds
Volker Borowski
Honored Contributor

Re: Bourne Shell moving/copying files ≥nerating date&time stamp

Let me see if I get it right:

- You receive files in .../upload
- You like to copy these files to .../download while adding a timestamp to the name
- You move the file away then, so that the procedure can work again, if the fiel is send again

??????

Since you do this every minute, how do you ensure, that you do not copy a file that is still in transmission ?
Since you did choose one minute as a period, you seem to expect these files quite frequently, so how do you ensure, that the file is not being sent right between those two "find" statements (which means it will be moved without beinig copied first)?

I would recommend to change the sending part in a way, that before a transmission of file "abc" it is checked if a file "abc.READY" exists, and if yes, the send is deleayed.
If "abc.READY" does not exist, you send "abc" and after the transmission you rename it "abc.READY" from the remote site.

Change you find commands just to process *.READY files, add the suggestions of the first two responses, and I guess you will be more safe.

Just my 2 cent
Volker
Darrell Allen
Honored Contributor
Solution

Re: Bourne Shell moving/copying files ≥nerating date&time stamp

Hi Laurie,

WRKFILE=/tmp/wrkfile.$$

DT=`date +%Y%m%d%H%M`

find /virtual/easttrust/upload -type f >$WRKFILE
while read file
do
BNAME=`basename $file`
cp $file /hal/easttrust/download/$BNAME.$DT
mv $file /tmp/download/$BNAME.$DT
done <$WRKFILE

rm $WRKFILE

# remove files older than 30 days
find /hal/easttrust/download -type f -mtime +30 | xargs rm
find /tmp/download -type f -mtime +30 | xargs rm

# End script


I don't see why you need a second copy of the file in /tmp but I'd choose to use a directory created just for that purpose (/tmp/download in my example).

You can put DT=`date +%Y%m%d%H%M` in the while loop if you want the time each file is copied or leave it before the loop for all files to have the same date/time stamp (my preference).

As Volker points out, you have other issues to consider. Is a file busy? You may be able to use fuser to determine that but it could be busy for other reasons besides being written. But since you move it after copying it, then as long as it isn't busy the next time the script runs you should be okay.

Good luck,
Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)