date
Command in Linux
Summary
The date
command is a versatile tool for displaying and setting the system date and time in Linux. It provides a wide range of formatting options to customize the output.
Introduction
The date
command is a fundamental utility in Linux used to display the current date and time. It can also be used to set the system clock (requires appropriate permissions). Its primary function is displaying the date and time, but it's also used for date arithmetic and formatting for scripting purposes. The output can be customized to suit various needs, such as logging timestamps or generating specific date formats for reports.
Use case and Examples
Displaying the current date and time
This command, without any options, will display the current date and time in a default format.Formatting the date
This example uses the+%Y-%m-%d
format string to display the date in the YYYY-MM-DD
format. Displaying the number of seconds since the Epoch
This command shows the number of seconds that have elapsed since January 1, 1970, 00:00:00 UTC.Setting the date and time (requires root privileges)
This command sets the system date and time to March 1st, 2024, at 10:00:00 AM. Use with caution as incorrect setting can cause issues.Display date in RFC3339 format
This command displays date and time in RFC3339 format with seconds.Commonly used flags
Flag | Description | Example |
---|---|---|
+%FORMAT | Specifies the output format using format specifiers. | date +%m/%d/%Y (Displays date as MM/DD/YYYY) |
-d, --date STRING | Display date described by STRING, not 'now' | date -d "yesterday" |
-s, --set STRING | Set the date and time (requires root privileges) | sudo date -s "12:34:56" |
-u, --utc, --universal | Print or set Coordinated Universal Time (UTC). | date -u |
--rfc-3339=TIMESPEC | Output date and time in RFC 3339 format. TIMESPEC can be 'date', 'seconds', or 'ns'. | date --rfc-3339=seconds |