1. Overview
A CLI calendar in the terminal provides a distraction-free option for command-line users. It’s beneficial for those who work extensively in the command-line environment, offering integration with workflow automation and scripts running on remote servers.
In this tutorial, we’ll learn how to show a calendar in a Linux terminal using the cal command.
2. cal Command
The cal command is a simple and commonly available command for displaying calendars in the terminal. Let’s learn about its features in detail.
2.1. Basics
The default behavior of the cal calendar is to assume the context for the current month. By making this assumption, it doesn’t require any other argument when the user wants information from the current context.
Let’s see the default behavior in action:
$ cal
July 2023
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
We notice that the output contains a calendar view for the current month.
Furthermore, we can use the cal command to show any month by specifying it explicitly:
$ cal 03 2050
March 2050
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Fantastic! We’ve got the basics covered.
2.2. -3 vs. -1 Flag
When viewing a calendar, an everyday use case is to check the previous and next months. So, we can use the -3 flag for this purpose:
$ cal -3
June 2023 July 2023 August 2023
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 1 1 2 3 4 5
4 5 6 7 8 9 10 2 3 4 5 6 7 8 6 7 8 9 10 11 12
11 12 13 14 15 16 17 9 10 11 12 13 14 15 13 14 15 16 17 18 19
18 19 20 21 22 23 24 16 17 18 19 20 21 22 20 21 22 23 24 25 26
25 26 27 28 29 30 23 24 25 26 27 28 29 27 28 29 30 31
30 31
We must note that the current month gets the middle position in this view.
Alternatively, we can use the cal command to show us a single month in the view by using the -1 flag:
$ cal -1
July 2023
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
It’s interesting to note that this is a default behavior.
Lastly, we must remember that -3 and -1 are special numeric flags, and cal doesn’t support numeric flags generically. So, using any other numeric flag, such as -2, won’t work.
2.3. -A and -B Flags
While the -3 flag is more inclined towards an everyday use case, the cal command offers the -A (after) and -B (before) flags for more customization for controlling the number of months in the view.
Now, let’s see how we can use the -A flag to show the current month along with an additional month after it:
$ cal -A1
July 2023 August 2023
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 1 2 3 4 5
2 3 4 5 6 7 8 6 7 8 9 10 11 12
9 10 11 12 13 14 15 13 14 15 16 17 18 19
16 17 18 19 20 21 22 20 21 22 23 24 25 26
23 24 25 26 27 28 29 27 28 29 30 31
30 31
Similarly, we can use the -B flag to show the current month along with an additional month before it:
$ cal -B1
June 2023 July 2023
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 1
4 5 6 7 8 9 10 2 3 4 5 6 7 8
11 12 13 14 15 16 17 9 10 11 12 13 14 15
18 19 20 21 22 23 24 16 17 18 19 20 21 22
25 26 27 28 29 30 23 24 25 26 27 28 29
30 31
Lastly, let’s use the -B and -A in conjunction to mimic the behavior of the -3 flag that shows three months together:
$ cal -B1 -A1
June 2023 July 2023 August 2023
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 1 1 2 3 4 5
4 5 6 7 8 9 10 2 3 4 5 6 7 8 6 7 8 9 10 11 12
11 12 13 14 15 16 17 9 10 11 12 13 14 15 13 14 15 16 17 18 19
18 19 20 21 22 23 24 16 17 18 19 20 21 22 20 21 22 23 24 25 26
25 26 27 28 29 30 23 24 25 26 27 28 29 27 28 29 30 31
30 31
2.4. -y Flag
We can use the -y flag to see a year in the calendar view. Let’s use it to show a year from the future:
$ cal -y 2030
2030
January February March
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 1 2 1 2
6 7 8 9 10 11 12 3 4 5 6 7 8 9 3 4 5 6 7 8 9
13 14 15 16 17 18 19 10 11 12 13 14 15 16 10 11 12 13 14 15 16
20 21 22 23 24 25 26 17 18 19 20 21 22 23 17 18 19 20 21 22 23
27 28 29 30 31 24 25 26 27 28 24 25 26 27 28 29 30
31
April May June
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 1 2 3 4 1
7 8 9 10 11 12 13 5 6 7 8 9 10 11 2 3 4 5 6 7 8
14 15 16 17 18 19 20 12 13 14 15 16 17 18 9 10 11 12 13 14 15
21 22 23 24 25 26 27 19 20 21 22 23 24 25 16 17 18 19 20 21 22
28 29 30 26 27 28 29 30 31 23 24 25 26 27 28 29
30
July August September
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 1 2 3 1 2 3 4 5 6 7
7 8 9 10 11 12 13 4 5 6 7 8 9 10 8 9 10 11 12 13 14
14 15 16 17 18 19 20 11 12 13 14 15 16 17 15 16 17 18 19 20 21
21 22 23 24 25 26 27 18 19 20 21 22 23 24 22 23 24 25 26 27 28
28 29 30 31 25 26 27 28 29 30 31 29 30
October November December
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 1 2 1 2 3 4 5 6 7
6 7 8 9 10 11 12 3 4 5 6 7 8 9 8 9 10 11 12 13 14
13 14 15 16 17 18 19 10 11 12 13 14 15 16 15 16 17 18 19 20 21
20 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28
27 28 29 30 31 24 25 26 27 28 29 30 29 30 31
Great! This was convenient.
2.5. -m Flag
By default, the week starts on Sunday in the default view. However, we can use the -m flag to show the calendar view with Monday as the starting day:
$ cal -m
July 2023
Mo Tu We Th Fr Sa Su
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
With the -m flag, the cal command lets users view the calendar based on personal, business, or regional preferences. Let’s go ahead and learn about the -j flag, which comes in a similar category.
2.6. -j Flag
By default, the cal command uses the Gregorian calendar system. However, if we’ve got a non-standard use case to view the calendar using the Julian calendar system, then we can use the -j flag:
$ cal -j 02 1988
February 1988
Su Mo Tu We Th Fr Sa
32 33 34 35 36 37
38 39 40 41 42 43 44
45 46 47 48 49 50 51
52 53 54 55 56 57 58
59 60
We can see that the day in February goes from 32 to 60 because the day doesn’t reset after every month in the Julian calendar system.
3. ncal Command
The ncal command is an alternative to the traditional cal command and supports a few more features than those supported by the cal command.
The foremost difference between the cal and ncal commands is that the latter shows the day of the week as rows instead of columns:
$ ncal
July 2023
Su 2 9 16 23 30
Mo 3 10 17 24 31
Tu 4 11 18 25
We 5 12 19 26
Th 6 13 20 27
Fr 7 14 21 28
Sa 1 8 15 22 29
Further, the ncal command supports a few more flags besides those offered by the cal command.
Amongst the commonly used flags, let’s see the -w flag that shows the week of the year at the bottom:
$ ncal -w
July 2023
Su 2 9 16 23 30
Mo 3 10 17 24 31
Tu 4 11 18 25
We 5 12 19 26
Th 6 13 20 27
Fr 7 14 21 28
Sa 1 8 15 22 29
26 27 28 29 30 31
4. Conclusion
In this article, we learned about Linux’s cal and ncal commands to show the calendar in the terminal. Furthermore, we explored flags such as -3, -y, -A, -B, -m, and -j for customizing the calendar view.