Executing script or command on the last day of a month

Sometime it is necessary to run a command or script on the last day of a month. There is old good hack (I found this somewhere on Usenet) that will decide whether tomorrow is the first day of the next month.

First find out your time zone (such as EDT, PST, IST, GMT etc) using date command:
$ dateOutput:

Wed Jul 12 11:13:41 MST 2006

MST is my time zone. Now type the following command:
$ TZ=MST-24 date +%dOutput:


As you see, output is 13 i.e. next day. Let us assume if today is 31st-july-2006 and you type the following command:$ TZ=MST-24 date +%dOutput:


Number one (1) indicates that tomorrow is the first day of the next month. Based upon this login you can easily write a shell script:
#! /bin/bash
TOM=$(TZ=MST-24 date +%d)
if [ $TOM -eq 1 ]; then
# commands or script on the last day of a month
# ...

Please note that TZ is the Linux (UNIX) environment variable containing the current time zone identifier (EDT, IST etc). For more information consults the man pages of ctime, date and info pages (info coreutils date) examples section.

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

🐧 6 comments so far... add one
CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
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 jobs killall kill pidof pstree pwdx time
Searchinggrep 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
6 comments… add one
  • Milton Oct 12, 2006 @ 17:45

    Great hack! Another way to actually get the value of the last day of the month is:
    “yest +0 ‘%L'” (See sourceforge.net/projects/yest)

    The utility also solves other date/time calculation problems.

    Milton W.

  • 🐧 nixCraft Oct 12, 2006 @ 17:55


    Thanks for pointing out yest tool.

    Appreciate your post.

  • Heurist Mar 21, 2008 @ 14:40

    date -d tomorrow +%d will also output tomorrow’s date without needing to work with the TZ environment.

    • petrohs Mar 12, 2012 @ 20:35

      only GNU date 🙁

  • buckeye Aug 6, 2009 @ 5:06

    i have some questions, can you alter the date and time on an email? an ex-cop/ex blackwater agent is stalking me and sending me emails through yahoo! his and my email accounts are with yahoo! the free account. Anyway, he sent me several treatening emails but the date and time on the emails were different from the actual date and time i recieved them. can the sender manipulate an email date and time stamp before they send it? if so how is he doing this? he was arrested and indicted by the grand jury on stalking and violation of protection order. he is out on bail and I am still getting weir emails from him but the dates and time stamps are wrong on these emails, since he seem to be manipulating the dates and they reflect a past date (prior to PO being filed, the police can’t do anything since the emails do not show current date sent.

    I am scared and confused by all of this, my knowledge of software is very limited and don’t know anyone that has better knowledge on programing. please forgive my spelling, he assaulted me and I had surgery a few days ago, he tore my rotated cuff and injured my neck.

    if anyone can help me out, i will greatly appreciate it.

  • Philippe Apr 1, 2010 @ 22:10

    why don’t you take a useful comment into account ?
    The “date -d tomorrow +%d” tip is preferable, because it is applicable on any Time Zone.

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