UNIX / Linux: Increment The Date

I want to increment the date under UNIX or Linux operating system while writing shell scripts. For example Apr/27/2011 should be incremented as Apr/28/2011 and so on. How do I increment a date in UNIX or Linux shell?

You can display time and date described by format (also known as relative date format) under GNU/date utility which is part of Linux and UNIX like operating systems. The default format is to display current date and time. To display tomorrows date, enter:
$ date
$ date --date="-1 days ago"

Sample outputs:

Wed Apr 27 02:29:59 IST 2011
Thu Apr 28 02:29:34 IST 2011

You can use bash for loop as follows:

#!/bin/bash
for i in {1..10}
do
   date --date="-$i days ago"
done

Sample outputs:

Thu Apr 28 02:33:02 IST 2011
Fri Apr 29 02:33:02 IST 2011
Sat Apr 30 02:33:02 IST 2011
Sun May  1 02:33:02 IST 2011
Mon May  2 02:33:02 IST 2011
Tue May  3 02:33:02 IST 2011
Wed May  4 02:33:02 IST 2011
Thu May  5 02:33:02 IST 2011
Fri May  6 02:33:02 IST 2011
Sat May  7 02:33:02 IST 2011

Change Date Format To DD/MM/YY

Use the following syntax:
$ date +"%d/%m/%y" --date="-5 days ago"
Here is update code:

#!/bin/bash
# Increment the date in dd/mm/yy format
for i in {1..10}
do
   date +"%d/%m/%y" --date="-$i days ago"
done

Sample outputs:

28/04/11
29/04/11
30/04/11
01/05/11
02/05/11
03/05/11
04/05/11
05/05/11
06/05/11
07/05/11

You can use the same technique in backup scripts to adjust date and time.

See also:


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 3 comments so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
3 comments… add one
  • ara Dec 28, 2011 @ 11:13

    date –date=”1 days ago” is a solution i was looking for!
    nice one, thanks a lot!!

  • giovani Mar 27, 2012 @ 19:13

    Awesome tip!

  • sai sandeep Aug 21, 2013 @ 12:40

    I am trying to use the above command for incrementing the current date by 1 day but its not working..can you please help me in this.
    I am executing this command in Korn shell.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum