Commands

5 Examples of Cal Command in Linux

This quick tutorial shows you how to display calendar in Linux with cal command and some common examples of this command.

Abhishek Prakash
Abhishek Prakash

Unix-like systems provide a handful of tools for dealing with dates and times. Cal is one such command that enables you to view calendar in the command line.

Cal, short for calendar, is a handy command with only a few options to view a certain date, month or year.

Since it doesn’t have tons of options, using cal is pretty simple.

If you just use the cal command, it will print the current month. It also highlights the current day as you can see in the image below:

Cal command in Linux
The current day is highlighted in cal command output

That’s the default behavior but you can do a lot more with this command.

Cal command examples

Let’s see some of the common usage of the cal command in Linux.

1. Print the current year’s calendar

You can print the calendar of the entire current year using the -y option of cal command.

cal -y
View calendar of year in Linux with cal command

You can also specify the year to print the calendar of a particular year.

cal -y <YYYY>

2. Print the calendar of a month in current year

Suppose you want to print the calendar of June, this year. You can do that in several ways.

You can specify the month’s name.

cal -m june

You can also specify the month’s number.

cal -m6
Month view of calendar with cal command in Linux
View a specific month

Note that only first three letters of a month’s name are taken into account. So cal -m jun, cal -m june and cal -m junta, all three will yield the same output.

3. Print the calendar of a particular month and/or year

If you want to print the calendar of a given month an year, you can do so by providing the month name or number and the year.

For example, to print the calendar of June 2017, you can use any of the below three formats.

  • cal june 2017
  • cal 6 2017
  • cal -d 2017-6
Specific month of a year in Linux calendar command

4. Print three months view

You can print the previous month, current month and the next month’s calendar in one go with -3 option.

Three months view with cal command
Previous month, current month and the next month in one view

You can specify the month and the year as well with the -3 option.

cal -3 june 2017

5. Print the calendar with specific number of months before and after a certain month

If you are not satisfied with the three months view, you can specify a custom range of months before and after a certain months.

For example, if you want to print the calendar of two months before and six months after the current month, you can use this command:

cal -A6 -B2

You can combine this with any month of any year.

cal -A1 -B1 june 2015
Specify range of months in calendar in Linux

Bonus Tip: Print the calendar with day numbers

You can print the calendar in day numbers with -j option.

What I mean is that instead of giving you the day of the month, it will display the day in the year.

So the 15th of Feb will be displayed as 46, 16th February as 47 and so on.

Display day number view with cal command in Linux

You can combine -j option with other options as well.

That’s it

That would be most of the usage of the cal command in Linux. There is a similar utility called ncal that gives slightly more options but the cal command is sufficient for my needs.

I hope this quick tutorial helped you in learning the cal command. Bookmark us and subscribe to us for regular Linux tutorials.



Join the conversation.