Module 7 · The system, disks and the environment
date and the system clock
Print the time in any format, build timestamps for filenames, set the clock by hand, and understand the difference between the kernel's clock and the hardware clock.
What you will learn
- Format dates with +FORMAT sequences.
- Convert and display other dates with -d and @epoch.
- Set the system clock with date -s and sync it from the RTC with hwclock.
date looks trivial and turns out to be one of the most used commands in scripts. It prints the current time, yes, but it also formats any moment however you like, parses dates you give it, and, as root, sets the clock. Underneath, Linux keeps time as a single number: seconds since 1 January 1970 UTC, the Unix epoch. Everything else is formatting.
Formatting
An argument starting with + is a format string. Each % sequence stands for a field: %Y four-digit year, %m month, %d day, %H %M %S hours, minutes, seconds, %A weekday name, %B month name, %s the epoch seconds. Anything else is copied literally, so quote the format if it contains spaces. -I is a shortcut for the ISO date YYYY-MM-DD, -R for the email-style date, and -u prints UTC instead of local time.
~% date
Sun Oct 4 18:51:18 UTC 2026
~% date +%Y-%m-%d
2026-10-04
~% date '+%A %d %B %Y'
Sunday 04 October 2026
~% date '+%Y%m%d-%H%M%S'
20261004-185118
~% date +%s
1791139878
That %Y%m%d-%H%M%S form is the workhorse of backup scripts: cp data.db data-$(date +%Y%m%d-%H%M%S).db produces names that sort chronologically with a plain ls. Avoid %d/%m/%Y in filenames (slashes are directory separators) and avoid spaces (they need quoting forever after).
Other moments: -d
-d STRING formats a date you supply instead of now. BusyBox accepts a handful of shapes: 2026-01-15, 2026-01-15 10:00, and @SECONDS for an epoch value, which is the way to turn a raw timestamp from a log or a database into something readable. GNU date also understands phrases like *yesterday* or *next friday*; BusyBox does not, so stick to the numeric forms here.
~% date -d 2026-01-15
Thu Jan 15 00:00:00 UTC 2026
~% date -d @0
Thu Jan 1 00:00:00 UTC 1970
~% date -d @1700000000 '+%Y-%m-%d'
2023-11-14
Two clocks
A PC has a hardware clock (the RTC, a battery-backed chip that ticks while the power is off) and the kernel has its own system clock, loaded from the RTC at boot and then kept by the kernel. date reads and sets the system clock; hwclock reads (-r) and sets the hardware one. As root, date -s '2031-03-01 09:00:00' jumps the system clock immediately; the RTC is untouched, so hwclock -s (*hardware to system*) copies the RTC back and undoes the experiment. hwclock -w would go the other way and save your change into the chip.
~% date -s '2031-03-01 09:00:00'
Sat Mar 1 09:00:00 UTC 2031
~% hwclock -r
Sun Oct 4 18:57:32 2026 0.000000 seconds
~% hwclock -s
~% date +%Y
2026
| Sequence | Meaning | Example |
|---|---|---|
%Y-%m-%d | ISO date | 2026-10-04 |
%H:%M:%S | 24-hour time | 18:51:18 |
%A, %B | Weekday and month names | Sunday, October |
%s | Seconds since 1970 | 1791139878 |
%j | Day of the year | 277 |
Commands in this lesson
| Command | What it does |
|---|---|
date | Current date and time. |
date +%Y-%m-%d | Today as YYYY-MM-DD. |
date '+%Y%m%d-%H%M%S' | A timestamp safe for filenames. |
date +%s | Seconds since the Unix epoch. |
date -d @1700000000 | Show the date for an epoch value. |
date -s '2031-03-01 09:00:00' | Set the system clock (root). |
hwclock -r / hwclock -s | Read the hardware clock / copy it to the system clock. |
Quiz
Which format prints 2026-10-04?
- +%d-%m-%Y
- +%Y-%m-%d
- +%y/%m/%d
- +%H-%M-%S
What is `date +%s`?
- The current second (0–59)
- Seconds since 1 January 1970 UTC
- The system uptime
- A summary of the date
After `date -s '2031-03-01 09:00:00'`, which command puts the real time back?
- hwclock -w
- hwclock -s
- date -r
- date -u
Why do backup scripts prefer `%Y%m%d-%H%M%S` over `%d/%m/%Y %H:%M`?
- It is shorter to type
- No slashes or spaces, and names sort in time order
- The other form is not valid
- It uses less disk space
Practice
Write today's date in the form `YYYY-MM-DD` (nothing else) into `/root/lab/l68/today.txt`.
Set the system clock to 1 March 2031 at 09:00:00.
The system clock has been set wrongly to the year 2001. Restore the correct time from the hardware clock.
Open this lesson in the app to do the tasks in a real Linux machine and have them checked.