cal - Linux


Overview

The cal command in Linux displays a calendar in the terminal, showing the current month by default. It can also display any specified month or year, providing a quick and easy way to reference dates. Generally used in scheduling, planning, and scripting to fetch date-related information.

Syntax

cal [options] [[month] year]
  • month: Numerically specifies the month to display (1-12).
  • year: Specifies the year to display. It should be a four-digit year.

Options/Flags

  • -1 : Display single month output (default setting).
  • -3 : Display previous, current, and next month in a row.
  • -m : Start weeks on Monday. By default, weeks start on Sunday.
  • -y : Display a calendar for the entire year.
  • -s : Set Sunday as the first day of the week (default in the U.S.).
  • -M : Set Monday as the first day of the week (common in most European countries).

Examples

  1. Display the current month’s calendar:
    cal
    
  2. Display the calendar for a specific month and year (October 2023):
    cal 10 2023
    
  3. Display the calendar for the entire year of 2023:
    cal -y 2023
    
  4. Display calendar for previous, current and next month:
    cal -3
    
  5. Display calendar starting with Monday as the first day of the week for March 2023:
    cal -m 3 2023
    

Common Issues

  • Incorrect month or year: If an invalid month (e.g., 13) or year (e.g., 999) is entered, cal will fail to display a calendar. Ensure that the month is between 1 and 12 and the year is a four-digit number.
  • Configuration locale: The start day of the week can be influenced by system locale settings. Use -s or -m to explicitly set Sunday or Monday as the start day.

Integration

cal can be combined with other commands to manage dates and automate tasks. For example, using cal with grep to find all Fridays in the current month:

cal | grep "Fri"

This can be expanded in scripts to automate reminders or tasks that need scheduling based on specific days.

  • date: Displays or sets the system date and time.
  • ncal: Displays a calendar and the date of Easter; similar to cal but provides a different layout and more options.

Further reading and detailed documentation can be accessed by using the man cal command or visiting the online Linux manual pages.