The date command on Linux can be used to see the current date and time, but we can also use addition and subtraction arithmetic with the command to extend its functionality. For example, instead of seeing the current date, we can see the date and time from five days ago, five years in the future, etc. The possibilities here are endless.

This becomes useful in many situations. One such example would be when you're creating backups, you can use the date command to assign files a dated name, or even to delete older backups by using some substraction arithmetic. We'll cover this specific example below so you can see it in action, but as you can imagine, there are many more cases where it would be handy.

In this guide, you'll see various date command examples involving addition and subtraction. Feel free to use these commands on your own system or in your own scripts to get acquainted with the command.

In this tutorial you will learn:
  • date command arithmetic and subtraction examples
Addition and subtraction examples with date command on Linux
Addition and subtraction examples with date command on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software date
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ - requires given linux commands to be executed as a regular non-privileged user

date arithmetic and subtraction examples


SUBSCRIBE TO NEWSLETTER
Subscribe to our NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


  1. Let's start with a simple backup command based on the date Linux command:
    $ tar cjf linuxconfig_$(date +%H%M-%d%m%Y).tar.bz2 ~/public_html
    
    Every time the command is executed it will create a file with the current date included in the file name.
  2. 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 becomes handy. Let's see a couple examples of how to subtract time from a current date using date string. Subtract 10 years from a current date:
    $ date
    Tue Jul 13 21:12:52 EDT 2021
    $ date --date="10 years ago" +%H%M-%d%m%Y
    2112-13072011
    
  3. Subtract 3 months from the current date:
    $ date
    Tue Jul 13 21:13:47 EDT 2021
    $ date --date="3 months ago" +%H%M-%d%m%Y
    2113-13042021
    
  4. Subtract 255 days from the current date:
    $ date
    Tue Jul 13 21:14:17 EDT 2021
    $ date --date="255 days ago" +%H%M-%d%m%Y
    2114-31102020
    
  5. Subtract 32 weeks from the current date:


    $ date; date --date="32 weeks ago" +%H%M-%d%m%Y
    Tue Jul 13 21:15:10 EDT 2021
    2015-01122020
    
  6. Subtract hours and minutes from the current date:
    $ date; date --date="5 hours ago" +%H%M-%d%m%Y
    Tue Jul 13 21:16:01 EDT 2021
    1616-13072021
    
  7. As you can probably guess, we follow the same format to subtract minutes from current date:
    $ date; date --date="5 minutes ago" +%H%M-%d%m%Y
    Tue Jul 13 21:16:45 EDT 2021
    2111-13072021
    
  8. 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
    
  9. 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
    Tue Jul 13 21:17:56 EDT 2021
    0917-14072021
    


  10. 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
    
  11. Lets see how we can subtract 2 weeks, 3 days, and 23 seconds from a current date using epoch time. Since this would be 1,468,823 seconds ago, the calculation would look like this:
    $ date;echo `date --universal +%s` - 1468823 | bc
    Tue Jul 13 21:19:37 EDT 2021
    1624756754
    
  12. The only thing left to do is convert output. Echo time to universal human readable date format:
    $ date -ud@1624756754
    Sun Jun 27 01:19:14 UTC 2021
    
  13. 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
    Tue Jul 13 21:22:13 EDT 2021
    1627694556
    
  14. Convert epoch time:
    $ date -ud@1627694556
    Sat Jul 31 01:22:36 UTC 2021
    

SUBSCRIBE TO NEWSLETTER
Subscribe to our NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


Closing Thoughts

In this guide, we saw how to use addition and subtraction arithmetic with the date command on Linux. This has many applications, one of which would be timestamped file names, as we saw in the examples above. Whether you choose to use the epoch time or normal time with date, Linux allows you to quickly calculate arbitrary date values and assign those variables to file names or any other use that you may have.

FIND LATEST LINUX JOBS on LinuxCareers.com
Submit your RESUME, create a JOB ALERT.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.