Skip to content

Latest commit

 

History

History
212 lines (164 loc) · 4.44 KB

File metadata and controls

212 lines (164 loc) · 4.44 KB

date Command in Linux

The date command is used to display the current system date and time.

Options of date Command

1) Display Date and Time in Different Formats

date +%D

  • Displays the date only in the format: mm/dd/yy.

date +%T

  • Displays the time only in the format: hh:mm:ss.

date +%d

  • Displays the day value.

date +%m

  • Displays the month value.

date +%y

  • Displays the year value in yy format.

date +%Y

  • Displays the year value in yyyy format.

date +%H

  • Displays the hour value (in 24-hour format).

date +%M

  • Displays the minute value.

date +%S

  • Displays the second value.

Examples

Example 1: Display the current system date in dd-mm-yyyy format

date +%d-%m-%Y

Example 2: Create an empty file where the file name contains the current system date

touch "durgajobs$(date +%d%m%Y).log"

Example Output:

durgajobs31102019.log
durgajobs01112019.log
durgajobs02112019.log
durgajobs03112019.log
durgajobs04112019.log

Example 3: Create an empty file where the file name contains the current system date and time

touch "durgajobs$(date +%d%m%Y%H%M%S).log"

Example Output:

durgajobs31102019205834.log

Note: If the file name contains date and time, then that file is called a timestamped file.


cal Command in UNIX/Linux

The cal command is used to display a calendar.

Usage of cal Command

1) Display the current month’s calendar

cal

2) Display the entire calendar for a specific year

cal 2020

3) Display a specific month’s calendar for a given year

cal 08 2019

4) Display the calendar for a specific year (e.g., the 1st year)

cal 1

5) Display the calendar for a large year value (e.g., the 9999th year)

cal 9999

6) Error for exceeding supported range

cal 10000

Output:

cal: year '10000' not in range 1..9999

Note: The cal command supports years from 1 to 9999 only.


Case Study: Automating Log File Management Using date and cal Commands

Scenario

A system administrator needs to manage log files efficiently by:

  1. Creating daily log files with timestamps.
  2. Deleting old log files automatically.
  3. Verifying system date and time before scheduling tasks.
  4. Checking calendar details for scheduling maintenance tasks.

Solution Using date and cal Commands

Step 1: Create a Daily Log File with Timestamp

Every day, a new log file should be created with the current date and time for tracking purposes.

touch "syslog_$(date +%d%m%Y_%H%M%S).log"

Example Output:

syslog_05022025_153045.log

Step 2: Remove Log Files Older than 7 Days

Since logs are created daily, old logs (older than 7 days) should be deleted to free up space.

find /var/logs -name "syslog_*.log" -mtime +7 -exec rm {} \;

Step 3: Verify System Date and Time

Before scheduling automated scripts, the admin needs to ensure the system clock is accurate.

date

Example Output:

Tue Feb 5 15:30:45 UTC 2025

To display only the date in dd-mm-yyyy format:

date +%d-%m-%Y

To display only the time in hh:mm:ss format:

date +%T

Step 4: Extract Specific Date Components for Reporting

To generate reports by day, month, or year, extract the required values.

echo "Day: $(date +%d), Month: $(date +%m), Year: $(date +%Y)"

Example Output:

Day: 05, Month: 02, Year: 2025

Step 5: Display the Current Month’s Calendar

Before scheduling maintenance, check the current month’s calendar.

cal

Example Output:

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

To check the entire year’s calendar:

cal 2025

Step 6: Display Calendar for a Specific Month and Year

To schedule a system update for August 2025, display its calendar.

cal 08 2025

Conclusion

Using date and cal commands, the system admin efficiently manages logs, automates cleanup, verifies system time, and schedules tasks. These commands simplify administrative tasks and improve system efficiency. 🚀