The date command is used to display the current system date and time.
- Displays the date only in the format:
mm/dd/yy.
- Displays the time only in the format:
hh:mm:ss.
- Displays the day value.
- Displays the month value.
- Displays the year value in
yyformat.
- Displays the year value in
yyyyformat.
- Displays the hour value (in 24-hour format).
- Displays the minute value.
- Displays the second value.
date +%d-%m-%Ytouch "durgajobs$(date +%d%m%Y).log"Example Output:
durgajobs31102019.log
durgajobs01112019.log
durgajobs02112019.log
durgajobs03112019.log
durgajobs04112019.log
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.
The cal command is used to display a calendar.
calcal 2020cal 08 2019cal 1cal 9999cal 10000Output:
cal: year '10000' not in range 1..9999
Note: The cal command supports years from 1 to 9999 only.
A system administrator needs to manage log files efficiently by:
- Creating daily log files with timestamps.
- Deleting old log files automatically.
- Verifying system date and time before scheduling tasks.
- Checking calendar details for scheduling maintenance tasks.
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
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 {} \;Before scheduling automated scripts, the admin needs to ensure the system clock is accurate.
dateExample Output:
Tue Feb 5 15:30:45 UTC 2025
To display only the date in dd-mm-yyyy format:
date +%d-%m-%YTo display only the time in hh:mm:ss format:
date +%TTo 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
Before scheduling maintenance, check the current month’s calendar.
calExample 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 2025To schedule a system update for August 2025, display its calendar.
cal 08 2025Using 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. 🚀