How to use the command 'cal' (with examples)
- Linux
- December 25, 2023
The ‘cal’ command is a useful utility in Unix-like operating systems that is used to display a calendar. It provides various options to customize the calendar display and can be used to view calendars for different months and years.
Use case 1: Display a calendar for the current month
Code:
cal
Motivation: This use case is helpful when you quickly want to check the calendar for the current month. It provides a visual representation of the days in the month, with the current day highlighted.
Explanation: The command ‘cal’ without any arguments displays the calendar for the current month. It uses the default settings, where Sunday is considered the first day of the week and the current day is highlighted.
Example output:
October 2022
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
Use case 2: Display previous, current and next month
Code:
cal -3
Motivation: This use case is useful when you want to view the calendars for the previous month, current month, and the next month in one glance. It provides a quick overview of the calendar for multiple months.
Explanation: The ‘-3’ option tells the ‘cal’ command to display three months: the previous month, current month, and next month. Each month is displayed individually.
Example output:
September 2022 October 2022 November 2022
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
30 31
Use case 3: Use Monday as the first day of the week
Code:
cal --monday
Motivation: This use case is helpful if you prefer to have Monday as the first day of the week instead of the default Sunday. It allows you to customize the calendar display according to your preference.
Explanation: The ‘–monday’ option tells the ‘cal’ command to consider Monday as the first day of the week while displaying the calendar. This shifts the days and columns in the calendar accordingly.
Example output:
October 2022
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
Use case 4: Display a calendar for a specific year (4 digits)
Code:
cal 2023
Motivation: This use case is helpful when you want to view the calendar for a specific year. It allows you to check the dates and days of the week for that particular year.
Explanation: By providing a specific year as an argument to the ‘cal’ command, the calendar for that year is displayed. The year should be in 4-digit format (e.g., 2023).
Example output:
2023
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 6 7 1 2 3 4 1 2 3 4
8 9 10 11 12 13 14 5 6 7 8 9 10 11 5 6 7 8 9 10 11
15 16 17 18 19 20 21 12 13 14 15 16 17 18 12 13 14 15 16 17 18
22 23 24 25 26 27 28 19 20 21 22 23 24 25 19 20 21 22 23 24 25
29 30 31 26 27 28 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 1 2 3 4 5 6 1 2 3
2 3 4 5 6 7 8 7 8 9 10 11 12 13 4 5 6 7 8 9 10
9 10 11 12 13 14 15 14 15 16 17 18 19 20 11 12 13 14 15 16 17
16 17 18 19 20 21 22 21 22 23 24 25 26 27 18 19 20 21 22 23 24
23 24 25 26 27 28 29 28 29 30 31 25 26 27 28 29 30
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 1 2 3 4 5 1 2
2 3 4 5 6 7 8 6