1832869 Members
8115 Online
110048 Solutions
New Discussion

date script

 
Dottie Hamilton
Contributor

date script

I am trying to write a script that will delete a file with the extention of the prior days date and everything that I have tried doesn't work. I don't want to delete the file with the current date as its extention.
What am I missing.

thanks
8 REPLIES 8
Steve Steel
Honored Contributor

Re: date script

Hi


Example please of extension used
for previous and todays date plus what you tried

You can always touch a file with an mtime of today at 06:00 and then find files older
and delete them.Then remove touch file.


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Alexander M. Ermes
Honored Contributor

Re: date script

Hi there.
Try this find statement :

find /u004/ftp/cash/if/in/cop/archive/ -name icstc_v100_a??????_0??.??-??-??
-exec rm {} \;
You can replace the last question marks by a parameter, that you can enter.
Rgds
Alexander M. Ermes
.. and all these memories are going to vanish like tears in the rain! final words from Rutger Hauer in "Blade Runner"
Justo Exposito
Esteemed Contributor

Re: date script

Hi Dottie,

I suppouse that you have files with names like this:

filename.yyyymmdd

If it is you can do a script that keep the today date and rm all the files with an extension minor than today.

Something like this:

cd your directory
date=$(date +%Y +%b +%d)
for i in `ls`
do
extension=$(echo $i | cut -d. -f2)
if [ $extension -lt $date ]
then
file="\*"+"\."+${extension}
rm ${file}
fi
done

Regards,

Justo.
Help is a Beatiful word
Steve Steel
Honored Contributor

Re: date script

Hi

or

today=todays_extension
find /your_dir -type f|grep -v $today|xargs rm

Will do it if you want to remove all but todays files.

or
find /yourdir -type f -mtime +1|xargs rm

kill more than 1 day old

Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
A. Clay Stephenson
Acclaimed Contributor

Re: date script

Hi Dottie:

I've attached a date script called caljd.sh; this will do almost any date problem; in this case it's overkill but this is my date hammer and every date problem is a nail.

FILENAME=myfile
SUFFIX=$(caljd.sh -y -s $(caljd.sh -p 1))
FULLNAME="${FILENAME}.${SUFFIX}"
This will result in a filename for the previous day of "myfile.20020123".

The inner caljd.sh uses the -p 1 argument to find the julian day 1 day previous. The outer call the converts that julian day into YYYY MM DD (the -y argument) and the -s removes the spaces to yield "YYYYMMDD". Invoke caljd.sh -u for full usage. With this you can skip weekends or holidays, leap years don't matter, or change the number of days previous.

If you start using this script, I think you will use nothing else for a date calculation within a script.

Regards, Clay
If it ain't broke, I can fix that.
Steven Sim Kok Leong
Honored Contributor

Re: date script

Hi,

If you have the date in the format: filename.yyyymmdd. You can write your script to make use of the fact that the files are sorted numerically in ls.

This script will remove all filename.* logs except the latest log (ie. today's log):
==========================================
#!/sbin/sh

current=`ls filename.*|tail -1`

for file in filename.*
do
if echo $file | grep $current
then
: # today's log, do nothing
else
rm $file # remove this log
fi
done
==========================================

Hope this helps. Regards.
Dottie Hamilton
Contributor

Re: date script

This is what I am trying to use; what have I got wrong?#!/bin/sh

cd /hbo/audittest
find . -type f -mtime -1 -exec rm -f {} \;

thanks for everyones help

A. Clay Stephenson
Acclaimed Contributor

Re: date script

Hi Dottie:

In that case, you need to change your -1 to +1 (older than 1 day). I a little surprised because this stuff has nothing to do with suffixes as your original post indicated.

If it ain't broke, I can fix that.