Operating System - HP-UX
1826056 Members
4181 Online
109690 Solutions
New Discussion

Move files older than 3 hours to archive directory

 
SHABU KHAN
Trusted Contributor

Move files older than 3 hours to archive directory

Hi All,

I was wondering if there is an easy way to do this ... I need to move log files older than 3 hours to the archive directory, for now I've a written a script (runs every 4 hours in cron) to do that and works okay, but it is not very efficient.

Any help would be greatly appreciated.

Thanks,
Shabu
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor

Re: Move files older than 3 hours to archive directory

Hi:

Why do you not consider your approach efficient? Inherently, this has little overhead.

Regards!

...JRF...
Helen French
Honored Contributor

Re: Move files older than 3 hours to archive directory

Just a thought:

You may consider using '-newer' option with find command. Touch a file and find the more recently modified files.

# man find (for details)
Life is a promise, fulfill it!
MANOJ SRIVASTAVA
Honored Contributor

Re: Move files older than 3 hours to archive directory

Hi Shabu


It is good taht you already have a wokring solution . May be run it every 3 hrs instead of 4 hrs , use find with options or

ls -l | awk '{if ($9 > "x" ) print $NF }' print the o/p in a file and use that file as the input to move the files .

You can just increment the value of x by whenever you want to run it .


Manoj Srivastava
SHABU KHAN
Trusted Contributor

Re: Move files older than 3 hours to archive directory

JRF,
I can live with the current script but just curious if you guys had a similar requirement ..

Shiju,
I am aware of the newer file option with find ... and thought about using it, but settled for a different logic ...

Manoj,
I'll think about your option ...

Thanks Guys !
-Shabu
Gregor Weertman
New Member

Re: Move files older than 3 hours to archive directory

Hello Shabu,

I made 2 script.
the first calculates the
time to minutes.
the second calculates it to
time again so it can be used
to by the touch command.

calculate the time in minutes
minus 180,
calculated this back to time,
touch a file with this time,
move all files that are not
newer then this file.

I use them for 40 minutes.
set $(min2date.sh $(expr $(date2min.sh) - 40))

touch -t $1$2$3$4 tempfile

the scripts do NOT work at the start of a new year.

The scripts are attached to
this message.

when you want, try it.

Regards Gregor
Keep it simple, it really works.
Frank Slootweg
Honored Contributor

Re: Move files older than 3 hours to archive directory

Since you did not post your script, it is hard to guess what is inefficient about it or/and what can be improved.

In general, it is important to try not to use loops, or, if you have to use them, only/mainly use built-in shell commands in the loop, i.e. no external ones, i.e. not from /usr/bin, etc..

Apparently you are already aware of find(1) and its "-newer" option. I advise to use that and pipe the list of files to be mv(1)-ed to xargs(1), so xargs(1) can invoke mv(1) multiple files to be moved, instead of just a single one.