Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete the files based on the date's

Status
Not open for further replies.

kandir

Programmer
Feb 17, 2010
4
IN
I have to write one script which will delete the files in the below passion.

If today is 17-Feb-2010 then the script delete only 17-JAN-2010 files from the directory.

Could you please help me, How will I delete the files when the year is leap year, if today is 30th Mar 2010 then how will handle such scenarios.

I have written simple script for getting the yesterday's date but I do not understand how to develop the script based on the above requirement.


bash-3.00$ cat calc_date_sample.sh

v_todaysdate=`date +%d`
v_yesterday=$((`date +%e` -1))
v_month=`date +%m`
v_year=`date +%Y`

if [ $v_yesterday -eq "0" ]
then
v_month=$((v_month-1))
if [ $v_month -eq "0" ]
then
v_month=12
v_year=$((v_year-1))
fi
set `cal $v_month $v_year`
shift $(($# - 1))
v_yesterday=$1
fi
echo "Previous Date : - " $v_yesterday-$v_month-$v_year
echo
echo "TodaysDate : - " $v_todaysdate-$v_month-$v_year

bash-3.00$
bash-3.00$

Quote:
bash-3.00$ ./calc_date_sample.sh
Previous Date : - 16-02-2010

TodaysDate : - 17-02-2010
bash-3.00$
bash-3.00$

 
If you are using Linux, you can write something like this to get date one month later

Code:
 date --date="-1 month" +%Y-%m-%d
 2010-01-17
 
Code:
find /path/to/startingDir -mtime 30 -delete
This will delete all files in the directory and subdirectories and the directories themselves, if exactly 30 days old.

This will not be the file of the same date from last month, and therefore not get troubles in not deleting files from 31.Jan. or searching for files from 31.Feb. but maybe it's ok for you.

Or maybe you prefer -mtime 31 or -mtime -28.

The command would work on linux with gnu-find. Maybe your's'll differ.

don't visit my homepage:
 
Thank you very much for your help on this.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top