cal & ncal – Linux Commands To Display Calendar Information

By | 18/07/2013

Q11. I want to display a specified number of months before and after a certain month. How can this be done?
Ans. Suppose you want to display 2 months prior and 2 months after the month of April (for current year). This can be done using -A and -B options.

Here is an example :

$ cal -m4 -B2 -A2
   February 2013           March 2013            April 2013       
Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  
                1  2                  1  2      1  2  3  4  5  6  
 3  4  5  6  7  8  9   3  4  5  6  7  8  9   7  8  9 10 11 12 13  
10 11 12 13 14 15 16  10 11 12 13 14 15 16  14 15 16 17 18 19 20  
17 18 19 20 21 22 23  17 18 19 20 21 22 23  21 22 23 24 25 26 27  
24 25 26 27 28        24 25 26 27 28 29 30  28 29 30              
                      31                                          

      May 2013             June 2013        
Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  
          1  2  3  4                     1  
 5  6  7  8  9 10 11   2  3  4  5  6  7  8  
12 13 14 15 16 17 18   9 10 11 12 13 14 15  
19 20 21 22 23 24 25  16 17 18 19 20 21 22  
26 27 28 29 30 31     23 24 25 26 27 28 29  
                      30

So you can see that along with the month of April, previous 2 months (Feb and March) and next 2 months (May and June) were displayed in output.

If you want to display some 2 months before and 2 months after year 2012, this can be done as :

$ cal -y 2012 -B2 -A2
   November 2011         December 2011          January 2012      
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   1  2  3  4  5  6  7  
 6  7  8  9 10 11 12   4  5  6  7  8  9 10   8  9 10 11 12 13 14  
13 14 15 16 17 18 19  11 12 13 14 15 16 17  15 16 17 18 19 20 21  
20 21 22 23 24 25 26  18 19 20 21 22 23 24  22 23 24 25 26 27 28  
27 28 29 30           25 26 27 28 29 30 31  29 30 31              

   February 2012           March 2012            April 2012       
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               1  2  3   1  2  3  4  5  6  7  
 5  6  7  8  9 10 11   4  5  6  7  8  9 10   8  9 10 11 12 13 14  
12 13 14 15 16 17 18  11 12 13 14 15 16 17  15 16 17 18 19 20 21  
19 20 21 22 23 24 25  18 19 20 21 22 23 24  22 23 24 25 26 27 28  
26 27 28 29           25 26 27 28 29 30 31  29 30                 

      May 2012             June 2012             July 2012        
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              

    August 2012          September 2012         October 2012      
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                     1      1  2  3  4  5  6  
 5  6  7  8  9 10 11   2  3  4  5  6  7  8   7  8  9 10 11 12 13  
12 13 14 15 16 17 18   9 10 11 12 13 14 15  14 15 16 17 18 19 20  
19 20 21 22 23 24 25  16 17 18 19 20 21 22  21 22 23 24 25 26 27  
26 27 28 29 30 31     23 24 25 26 27 28 29  28 29 30 31           
                      30                                          

   November 2012         December 2012          January 2013      
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                                       

   February 2013      
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

So we can see that last 2 months of 2011 and beginning 2 months of 2013 were displayed along with all the months of year 2012.

Q12. How to direct ncal to produce cal like output and vice versa?
Ans. ncal can be made to produce output like cal by using -C option.

Here is an example :

$ ncal
    July 2013         
Su     7 14 21 28   
Mo  1  8 15 22 29   
Tu  2  9 16 23 30   
We  3 10 17 24 31   
Th  4 11 18 25      
Fr  5 12 19 26      
Sa  6 13 20 27      

$ cal
     July 2013        
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           

$ ncal -C
     July 2013        
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

So you can see that with -C option ncal produces outputs like cal utility.

Similarly, cal can be used to produce output like ncal by using -N option.

Here is an example :

$ cal
     July 2013        
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           

$ ncal
    July 2013         
Su     7 14 21 28   
Mo  1  8 15 22 29   
Tu  2  9 16 23 30   
We  3 10 17 24 31   
Th  4 11 18 25      
Fr  5 12 19 26      
Sa  6 13 20 27      

$ cal -N
    July 2013         
Su     7 14 21 28   
Mo  1  8 15 22 29   
Tu  2  9 16 23 30   
We  3 10 17 24 31   
Th  4 11 18 25      
Fr  5 12 19 26      
Sa  6 13 20 27

Observe that by using -N option with cal, the utility produces output like ncal.

Q13. How to make Monday as starting day of the week?
Ans. To do this, use -M option with ncal utilty.

Here is an example :

$ ncal -M
    July 2013         
Mo  1  8 15 22 29   
Tu  2  9 16 23 30   
We  3 10 17 24 31   
Th  4 11 18 25      
Fr  5 12 19 26      
Sa  6 13 20 27      
Su  7 14 21 28

Observe that week now starts from Monday. Also, you can use -S option to make Sunday as a week starter day.

Q14. How to switch ncal to old style format?
Ans. Use -b option for this.

Here is an example :

$ ncal
    July 2013         
Su     7 14 21 28   
Mo  1  8 15 22 29   
Tu  2  9 16 23 30   
We  3 10 17 24 31   
Th  4 11 18 25      
Fr  5 12 19 26      
Sa  6 13 20 27      

$ ncal -b
     July 2013        
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

So this way ncal produces output in old style.

Some important information related to cal and ncal

Though this information is present in man page of cal/ncal but still I would like to emphasize on this :

A single parameter specifies the year (1–9999) to be displayed; note the year must be fully specified: “cal 89” will not display a calendar for 1989. Two parameters denote the month and year; the month is either a number between 1 and 12, or a full or abbreviated name as specified by the current locale. Month and year default to those of the current system clock and time zone (so “cal -m 8” will display a calendar for the month of August in the current year).

Not all options can be used together. For example, the options -y, -3, and -1 are mutually exclusive. If inconsistent options are given, the later ones take precedence over the earlier ones. A year starts on January 1.

Here is an interesting discussion on cal utility.

Here is a link to man page of cal/ncal.

Leave a Reply

Your email address will not be published. Required fields are marked *