LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-11-2004, 09:44 AM   #1
the_rhino
Member
 
Registered: Sep 2004
Distribution: PCLinuxOS
Posts: 59

Rep: Reputation: 15
putting a math calculation in a cron job


I have a cron job

Getting a script called "backup" and it is cranking out a new directory and moving some files for me everyday like this

SHELL=/bin/bash
today=$(date +%m%d%y)
mkdir /home/user_name/serverbackup/serverbackup$today
mv /home/user_name/serverbackup/*.tar.gz --target-directory=/home/user_name/serverbackup/serverbackup$today

It works great, now I want to add another command to the script to remove the directory that is 8 days old, so, that I have the current seven days directories.

I have looked at man pages date and expr they don't help.

I am working my way through Rute and it does not give me anything to work with yet.

I haven't found anything useful on google or ldp.org

I do not know the syntax to make this work. I know that some how I need to have the script calculate the date that is 8 days from the current date so that I would end up with it looking for a directory like this:

today = serverbackup101104

8 days back from today's date = serverbackup100404

then the script would remove serverbackup100404 and the contents of that directory.

I suppose this part of the script is going to be a little more complex than the part I am using now.

Will anyone point me in the right direction so I can learn how to do this?

Thanks for the help,
The Rhino
 
Old 10-11-2004, 11:57 AM   #2
DaHammer
Member
 
Registered: Oct 2003
Location: Planet Earth
Distribution: Slackware, LFS
Posts: 561

Rep: Reputation: 30
Assuming your directory names always start with "serverbackup" followed by the "mmddyy", then this should work:
Code:
#!/bin/sh
# cleanup.sh

cleanup()
{
  for file in `ls`;
    do
      oldday=`echo $file | cut -c 15,16`;
      namechk=`echo $file | cut -c 1,2,3,4,5,6,7,8,9,10,11,12`;
      if test -d $file && test "$namechk" == "serverbackup"; then
        diff=`expr $thisday - $oldday`
        if test $diff -gt 7; then
          echo "$file: $diff days old - Removing"
          #rm -rf $file
        else
          echo "$file: $diff days old"
        fi;
      fi;
  done;
}

today=`date +%m%d%y`
thisday=`echo $today | cut -c 3,4`
cleanup
Given a directory with the following contents:
Code:
cleanup.sh*          serverbackup100404/  serverbackup100704/  serverbackup101004/  testfile
serverbackup100204/  serverbackup100504/  serverbackup100804/  serverbackup101104/  testfile.txt
serverbackup100304/  serverbackup100604/  serverbackup100904/  testdir/
the output would be:
Code:
serverbackup100204: 9 days old - Removing
serverbackup100304: 8 days old - Removing
serverbackup100404: 7 days old
serverbackup100504: 6 days old
serverbackup100604: 5 days old
serverbackup100704: 4 days old
serverbackup100804: 3 days old
serverbackup100904: 2 days old
serverbackup101004: 1 days old
serverbackup101104: 0 days old
Notice I left the "rm -rf $file" commented. You should throughly test this before actually enabling that. Placing a dynamic "rm" in a shell script makes me cringe with thoughts of $file somehow ending up being equal to "/" and then getting executed as root. There is somewhat of a sanity check in place though, since it makes sure the directroy starts with "serverbackup" before it would remove it, but you may still want to include a few more safety nets. Anyway, you should at least get an idea of how you can calculate the difference in the dates on end of the file name.

Edit:
Oops, that won't work when the month and/or year changes, sorry.

Last edited by DaHammer; 10-11-2004 at 12:17 PM.
 
Old 10-11-2004, 12:37 PM   #3
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
The find utility has time calculation features like modification time ( mtime ) and access time ( atime ) so you can search for files or directories which are older than or newer than the present time. Be careful when using the rm command, especially if you use it with -rf

#Delete old directories with the following command
find /home/user_name/serverbackup/* -type d -mtime +192 -exec rm -rf {} \;

#Delete old text files with the following command
find /home/user_name/serverbackup -type f -name '*.txt' -mtime +192 -exec rm {} \;
 
Old 10-11-2004, 01:46 PM   #4
DaHammer
Member
 
Registered: Oct 2003
Location: Planet Earth
Distribution: Slackware, LFS
Posts: 561

Rep: Reputation: 30
Here is a correction to what I did above, still using the directory name itself. You'll need to switch your date format to yymmdd though for it to work.

Code:
#!/bin/sh

cleanup()
{
  for file in `ls`;
    do
      namechk=`echo $file | cut -c 1,2,3,4,5,6,7,8,9,10,11,12`;
      filedate=`echo $file | cut -c 13,14,15,16,17,18`;
      filesecs=`date -d "$filedate" +%s` # Get seconds since 00:00:00 1970-01-01 UTC
      if test -d $file && test "$namechk" == "serverbackup"; then
        diff=`expr $tsecs - $filesecs`
        days=`expr $diff / 86400`;
        if test $diff -gt 691200; then
          echo "$file: $days days old - Removing"
          #rm -rf $file
        else
          echo "$file: $days days old"
        fi;
      fi;
  done;
}

today=`date +%y%m%d`
tsecs=`date +%s` # Get seconds since 00:00:00 1970-01-01 UTC
cleanup
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cron job? dr_zayus69 Linux - Newbie 14 12-01-2004 06:16 PM
improve math calculation... java os2 Programming 1 10-21-2004 06:17 PM
cron job sanjith11 Linux - General 5 07-14-2004 01:06 PM
c math calculation alaios Programming 3 06-01-2004 01:46 AM
Cron job T-Rex Linux - Newbie 1 09-26-2001 11:28 AM


All times are GMT -5. The time now is 10:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration