Script to delete files based on timestamp

Post questions here relative to DataStage Server Edition for such areas as Server job design, DS Basic, Routines, Job Sequences, etc.

Moderators: chulett, rschirm, roy

Post Reply
Jay
Participant
Posts: 105
Joined: Tue Nov 11, 2003 8:28 pm

Script to delete files based on timestamp

Post by Jay »

Hi All,

Here is my requirement.

I have to delete older files based on timestamp.

I have not started developing yet.

I need some ideas to achieve this.

My idea:

list files : ls -ltr
get file names : cut -c 55-
get last file: tail -1

then give the rm command.

The problem is there might be two or more files with the same timestamp.

I am stuck as to how to delete these two or more files.

If you have any ideas, please let me know.

Thanks
Jay
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Investigate the UNIX find command. For example:

Code: Select all

find . -atime +30 -exec rm {} \;
Read the man pages for find to garner more information.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Jay
Participant
Posts: 105
Joined: Tue Nov 11, 2003 8:28 pm

Post by Jay »

Thanks, Ray.

I googled and found out that i have to use the "find" command where we can specify the number of days.

Thanks
Jay
Jay
Participant
Posts: 105
Joined: Tue Nov 11, 2003 8:28 pm

Post by Jay »

Hi Ray,

When i embed the find command in the ftp script, it complains Invalid command. FTP cannot recognize "find".

#Build and run the delete file script
echo ===================
echo About to delete files from $tdir
echo "open $server
user $user $pwd
cd $tdir
prompt off
find . -atime +0 -exec rm -f {} \;
quit "


Is there any other way to use the find command, apart from the FTP process ?

Thanks in advance
Jay
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

You didn't mention FTP in your original post! The solution I gave was one that also didn't assume being embedded in FTP script.

I would have to give it a great deal of thought. In the meantime, try an FTP stage, and run find as a "before" command. You may be able to get that to work.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

Jay wrote:Is there any other way to use the find command, apart from the FTP process ?
Of course. I'm curious why you feel the need to do this in an ftp session. Is this the only access you have to a remote system? Is it at least a UNIX system?

Perhaps if you explained your need in greater detail people could give you more targeted answers.
-craig

"You can never have too many knives" -- Logan Nine Fingers
Jay
Participant
Posts: 105
Joined: Tue Nov 11, 2003 8:28 pm

Post by Jay »

Hi All,

Thanks for the replies...sorry , i was not clear enough...

here is my scenario.

Datastage runs on Box1, Solaris 5.8
Archiving is to be done on Box2, AIX. There are folders here where we will be keeping our archived files.

My requirement is to delete older files depending on the interface.
For Interface 1, i need to keep 3 files (weekly process), 3 weeks data.
For Interface 2, i need to keep 14 files ( daily process), 2 weeks data.

Each archived file will have timestamp attached to it.

Please let me know in which direction i should proceed.

Meanwhile i will try what Ray suggested.

Thanks in advance
Jay
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

You could try the find command (with exec of rm) through remote shell. Your UNIX administrator will have a heart attack when you ask for authority to do this, but you merely need to convince her or him that your UNIX skills are sufficiently up to scratch!

It may be better to create such a shell script on the remote machine, so that only that command can be executed, and remotely execute the shell script.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Jay
Participant
Posts: 105
Joined: Tue Nov 11, 2003 8:28 pm

Post by Jay »

Thanks, Ray.

We came up with a solution.

I wrote a shell script to delete files. We placed it on the remote server and called it via the scheduler.

Thanks all for your suggestions,
Jay
Post Reply