This quick tutorial shows you how to display calendar in Linux with cal command and some common examples of this command.
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 commands 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:
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.
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.
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
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.
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
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.
You can combine -j option with other options as well.
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.