How to Use the Command 'cal' (with Examples)
- Linux
- December 17, 2024
The ‘cal’ command is a utility in Unix and Unix-like operating systems that prints a simple calendar to the terminal. It offers various options to display current, past, or future months, as well as entire years. This command is particularly useful for system administrators, programmers, and users who quickly want to check precise calendar dates without leaving the command line interface. Through a range of options, users can customize their calendar view by displaying specific months, years, or adjusting the week start day.
Display a Calendar for the Current Month
Code:
cal
Motivation:
This command is perfect when you need a quick glance at the current month’s layout. Perhaps you’re scheduling tasks or events and need a reminder of how days align in the current month. It also helps to confirm today’s date effortlessly from the command line.
Explanation:
The cal
command without any additional arguments defaults to displaying the calendar for the current month. It automatically highlights the current day, enhancing usability by quickly directing your attention to today’s date.
Example Output:
October 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
Display 3 Months Spanning the Date
Code:
cal -3
Motivation:
When you need a broader view of the calendar, looking at previous or upcoming months, this command is beneficial. It’s particularly useful when planning events or tasks that straddle the current month, enabling a seamless understanding of past, present, and near-future dates.
Explanation:
The -3
flag tells the cal
command to display three consecutive months — the current month, the preceding one, and the following one. This option expands the default calendar output for more comprehensive date planning.
Example Output:
September 2023 October 2023 November 2023
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 7 1 2 3 4
3 4 5 6 7 8 9 8 9 10 11 12 13 14 5 6 7 8 9 10 11
10 11 12 13 14 15 16 15 16 17 18 19 20 21 12 13 14 15 16 17 18
17 18 19 20 21 22 23 22 23 24 25 26 27 28 19 20 21 22 23 24 25
24 25 26 27 28 29 30 29 30 31 26 27 28 29 30
Display the Whole Calendar for the Current Year
Code:
cal --year
Motivation:
This option is advantageous when you need a high-level overview of the entire year. It allows you to evaluate months at a glance, useful in annual planning, setting deadlines across months, or simply reviewing the year layout.
Explanation:
The --year
flag directs the cal
command to print the complete calendar for the current year. This extended view includes all months from January through December, simplifying the process of year-round scheduling.
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
... (and so on for all months of the year)
Display the Next Twelve Months
Code:
cal --twelve
Motivation:
Ideal for forward planning, this command shows the calendars for the upcoming twelve months, allowing you to plan long-term projects, vacations, or events. It provides insight into the alignment of days across numerous months.
Explanation:
The --twelve
flag instructs cal
to exhibit a calendar starting with the current month and extending through the next eleven months. The outcome is a comprehensive view of the year ahead, suitable for strategic planning.
Example Output:
October 2023 November 2023 ...
September 2024 October 2024
(Note: The output will include the complete months layout for a year starting from the current month.)
Use Monday as the First Day of the Week
Code:
cal --monday
Motivation:
This variation is favored by users in regions or contexts where the week is considered to start on Monday, such as much of Europe. Adjusting the first day of the week enhances usability and consistency in planning according to local customs.
Explanation:
The --monday
option modifies the calendar so that it starts the week on Monday instead of the default Sunday. This reflects international standards used outside of the United States.
Example Output:
October 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
Display a Calendar for a Specific Year (4 digits)
Code:
cal 2025
Motivation:
When needing to explore or plan for a future year or examine historical dates, this command shines. It’s invaluable for project managers or individuals tasked with setting timelines well into the future or past.
Explanation:
The cal
command followed by a four-digit number specifies which year’s calendar to display. This facilitates viewing any desired year’s complete layout easily.
Example Output:
2025
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 3 4 5 6 7 1
6 7 8 9 10 11 12 8 9 10 11 12 13 14 2 3 4 5 6 7 8
... (and so on for all months of 2025)
Display a Calendar for a Specific Month and Year
Code:
cal 4 2023
Motivation:
This command is best for pinpointing specific calendar data for concrete planning. Whether reviewing past events or preparing for upcoming engagements, it allows you to focus on one month in any specified year.
Explanation:
In this use case, the cal
command takes two arguments: the month and the year. The month is specified as a numerical value (1 for January, 2 for February, and so on), followed by the four-digit year.
Example Output:
April 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
Conclusion
The cal
command in Unix-like systems provides an efficient and straightforward way to view calendar information from the command line. With its rich set of options, users can easily tailor calendar outputs to their specific needs, whether they require a quick view of the current month or long-term, multi-month planning. Through its versatility and simple syntax, cal
remains a stalwart tool in every command line user’s arsenal.