How to use the cal command in Linux?

In this tutorial, we are going to discuss how to use the cal command in Linux or Unix systems. We will also discuss the different ways to use this command with different options. So let’s get started!

Recommended read: The nix package manager in Linux


What is the cal command in Linux?

While working on the Linux or Unix systems most of the time we are on the terminal. In case we want to get an immediate overview of the calendar on the terminal itself then the cal command is the best option available for us. It is a command-line utility that is used to print the calendar in different formats on the terminal screen.

Installing ncal

To use cal first, we have to install the ncal utility on our Linux/Unix system. Some Linux/Unix distributions have this installed by default. In that case, we can directly start using cal with its full potential. Following is the Linux command to install the cal command-line utility on the system:

ubuntu:~$ sudo apt install ncal

Output:

Output Install Ncal

cal vs ncal command in Linux

In Linux/Unix both cal and ncal are used to print a formatted calendar. ncal stands for new cal i.e new calendar.

ncal provides all the functions of cal with an additional feature of printing the calendar vertically i.e the weekdays will be shown in a column, not in a row.

If a system has ncal installed on it then cal acts as a symbolic link to ncal.

By default both cal and ncal prints the calendar of the current month but the only difference in the outputs is that cal will show the weekdays in a row whereas ncal will show the weekdays in a column

Let’s try running cal and ncal on our Linux terminal one by one and observe the differences in the outputs.

ubuntu:~$ cal

Output:

Output cal
ubuntu:~$ ncal

Output:

Output ncal

Syntax of the cal command

The most common syntax of cal or ncal in Linux is:

  • cal [[month] year]
  • ncal [[month] year]

Although the [[month] year] and [year] are optional. By default without these options, cal and ncal both display the current month’s calendar with the current date highlighted. The values for these options can be provided as follows:

  • [month]: Either MM (01 to 12) or Mon (Jan, Feb, Mar…..Nov, Dec) or Month (January, February, March…..November, December)
  • [year]: YYYY like ……2010, 2011, 2012……,2021

Let’s use this on the terminal and observe the output:

ubuntu:~$ cal 07 2021
ubuntu:~$ cal Nov 2021
ubuntu:~$ cal May 2021

Output:

Output Cal Mm Yyyy
Output Cal Mon Yyyy
Output Month Yyyy
ubuntu:~$ ncal 07 2021
ubuntu:~$ ncal Nov 2021
ubuntu:~$ ncal May 2021

Output:

Output Ncal Mm Yyyy
Output Ncal Mon Yyyy
Output Ncal Month Yyyy

Options used with the cal command

There is a long list of options that can be used with cal or ncal in Linux, but we will discuss only the useful and interesting ones. Following is the list of some useful and interesting options with the details which we can use with the cal or ncal command:

  • -y: This is available for both cal and ncal. It prints the calendar of the entire current year with the current date highlighted.
ubuntu:~$ cal -y
ubuntu:~$ ncal -y

Output:

Output Cal Y
Output Ncal Y
  • -3: This is also available for both cal and ncal. It prints the calendar of the previous month, current month, and forthcoming month of the current year.
ubuntu:~$ cal -3
ubuntu:~$ ncal -3

Output:

Output Cal 3
Output Ncal 3 1
  • -j: This is also available for both cal and ncal. It prints the calendar of the current month but in the *Julian calendar format not in the default **Gregorian calendar format with the current day highlighted.
    • **Gregorian calendar: Every month starts with the date as 1.
    • *Julian calendar: The date does not reset to 1 after a month ends. So after January 31st, Feb starts as day 32 instead of Feb 1.
ubuntu:~$ cal -j
ubuntu:~$ ncal -j

Output:

Output Cal J
Output Ncal J
  • -j -3 or -y -j: We can also combine these options and use them with the cal or ncal command to get the desired output. The order of these options does not affect the output.
ubuntu:~$ cal -j -3
ubuntu:~$ ncal -y -j

Output:

Output Cal J 3
Output of cal -j -3
Output Ncal Y J
Output of ncal -y -j

Conclusion

In this tutorial, we have learned:

  • what the cal command in Linux is
  • the difference between the cal and ncal commands
  • how to install the command-line utility the cal and ncal
  • the syntax of the cal and ncal commands
  • few useful and interesting options which can be provided with the cal and ncal commands.

Hope you have understood the things discussed here and ready to experiment with these commands on your Linux/Unix system.