RSS Subscription
Linux Howtos & Tutorials

Enter your email:

Delivered by


NOTE:New tutorials are from LinuxCareer.com

Poll

Do you own or wish to have iPhone?
 


Linux eBooks FREE Download
A guide to programming Linux kernel modules
Introduction to Linux - A Hands on Guide
A Newbie's Getting Started Guide to Linux

Linux from Scratch - Create Your Own Linux System - Free eBook

Linux: The Hacking Solution (v.3.0)

SQLite 3 with PHP Essential Training – Free Video Training Tutorials

This guide will introduce you to the world of GNU/Linux

The GNU/Linux Advanced Administration

A Complete Beginner's Manual for Ubuntu 10.04 (Lucid Lynx)

Advanced Bash-Scripting Guide

Set up, maintain, and secure a small office email server

Partner Linux Sites:
How-To.LinuxCareer.com
Jobs.LinuxCareer.com
TuxMachines
Monsterb
LinuxBloggers
AdamsInfo
LinuxScrew
All For Linux

Addition and subtraction arithmetics with Linux date command

This article features some simple examples on how to add or subtract a time from a current date. At first we use a native date sytax to do this trick and later we will see how this can be done manually by converting date to an epoch time. Let's start with a simple backup script based on the date Linux command:

#!/bin/bash

tar cjf linuxconfig_$(date +%H%M-%d%m%Y).tar.bz2 ~/public_html

Every time the script is executed it will create a file with a current date included in the file name. The backup problem is solved. However, we do not usually want to keep all backup files indefinitely or until they consume all available free space. This is where the subtraction arithmetics with Linux date command comes handy. Let's see couple examples how to subtract time from a current date using date string: Subtract 10 years from a current date:

$ date
Mon Mar  7 09:26:13 EST 2011
$ date --date="10 years ago" +%H%M-%d%m%Y
0926-07032001

Subtract 3 months from a current date:

$ date
Mon Mar  7 09:29:39 EST 2011
$ date --date="3 months ago" +%H%M-%d%m%Y
0929-07122010

Subtract 255 days from a current date:

$ date
Mon Mar  7 09:31:13 EST 2011
$ date --date="255 days ago" +%H%M-%d%m%Y
0831-25062010

Subtract 32 weeks from a current date:

date; date --date="32 weeks ago" +%H%M-%d%m%Y
Mon Mar  7 10:05:34 EST 2011
0905-26072010

Subtract hours minutes from a current date:

$ date; date --date="5 hours ago" +%H%M-%d%m%Y
Mon Mar  7 09:54:42 EST 2011
0454-07032011

and as you have already guessed we follow the same format to subtract minutes from current date:

$ date; date --date="5 minutes ago" +%H%M-%d%m%Y
Mon Mar  7 09:32:48 EST 2011
0927-07032011

We can now improve our simple backup script to keep only files which are not older than 6 months:

#!/bin/bash

tar cjf linuxconfig_$(date +%H%M-%d%m%Y).tar.bz2 ~/public_html
rm linuxconfig_$(date --date="6 months ago" +%H%M-%d%m%Y).tar.bz2

Going into the future with date command is as easy as going into the past. All what needs to be done is to add "-" ( minus ) sign in front of every date string. For example you can ask date command to add 12 hours to a current date and time:

date; date --date="-12 hours ago" +%H%M-%d%m%Y
Mon Mar  7 10:02:17 EST 2011
2202-07032011

On some Unix systems the date syntax described above may not be available. In this case here as a simple example on how to the do all above using epoch time. epoch time is simply a number of seconds since "Jan 1, 1970 00:00:00". Therefore, epoch time "1" using universal time is:

$ date -ud@1
Thu Jan  1 00:00:01 UTC 1970

Lets see how we can subtract 2 weeks 3 days and 23 seconds from a current date sing epoch time. Please not that we use only universal time for a following calculations. In the first step we need to convert 2 weeks 3 days and 23 seconds into seconds. Use a following table to guide you through this process:

1 year ( 365 ) 31,536,000 seconds
1 week 604 800 seconds
1 day 86 400 seconds
1 hour 3600 seconds
1 minute 60 seconds
1 second 1 second
2 weeks 3 days and 23 seconds = 2 x 604 800 + 3 x 86 400 + 23
2 weeks 3 days and 23 seconds = 1 209 600 + 259 200 + 23
2 weeks 3 days and 23 seconds = 1 468 823 seconds

Now, that we have number of seconds lets subtract this number from a current epoch time:

$ date;echo `date --universal +%s` - 1468823 | bc 
Mon Mar  7 10:43:14 EST 2011
1297986171

All what has left is to convert output echo time to universal human readable date format:

$ date -ud@1297986171
Thu Feb 17 23:42:51 UTC 2011

To add 2 weeks 3 days and 23 seconds use the same process but use addition instead of subtraction:

$ date;echo `date --universal +%s` + 1468823 | bc
Mon Mar  7 11:02:29 EST 2011
1300924972

Convert epoch time:

$ date -ud@1300924972
Thu Mar 24 00:02:52 UTC 2011

Share this linux post:

Submit Addition and subtraction arithmetics with Linux date command in Delicious Submit Addition and subtraction arithmetics with Linux date command in Digg Submit Addition and subtraction arithmetics with Linux date command in FaceBook Submit Addition and subtraction arithmetics with Linux date command in Google Bookmarks Submit Addition and subtraction arithmetics with Linux date command in Stumbleupon Submit Addition and subtraction arithmetics with Linux date command in Technorati Submit Addition and subtraction arithmetics with Linux date command in Twitter
 
Comments for this page are closed !!!
Please visit our new Linux Forum for additional help or discussion.


Linux eBooks FREE Download