Overview of date
The `date` command is a core tool that interacts with the system clock to get or set time information. By default, it displays the current date and time to standard output, and you can format the output as desired using various format specifiers.
Key Roles of date
The `date` command is primarily used for the following purposes:
Main Use Cases
- Check Current Time: Quickly check the system's current date and time.
- Logs and File Names: Improve manageability by including a timestamp in the names of log files or backup files.
- Set System Time: (With administrator privileges) Change the system's date and time.
- Time-based Scripts: Used in shell scripts that need to run at a specific time or operate based on time information.
- Time Zone Information: Check the system's current time zone information.
Key date Command Options and Format Specifiers
The `date` command, along with options like `-d` and `-s`, can use a wide variety of format specifiers to output or set the date and time in the desired format.
1. Basic Output and Setting
2. Format Specifiers
Generated command:
Try combining the commands.
Description:
`date` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Explore various examples of the `date` command to learn how to display and use date and time information in your desired format.
Output Current Date and Time in Default Format
date
Outputs the current system date and time according to the default locale settings.
Output in YYYYMMDD_HHMMSS Format
date +%Y%m%d_%H%M%S
Outputs the date and time in a format often used for log file or backup file names.
Output Yesterday's Date
date -d "yesterday"
Outputs the date for 'yesterday' relative to the current date.
Output Next Monday's Date
date -d "next Monday"
Outputs the date for 'next Monday' relative to the current date.
Convert a Specific Unix Timestamp to a Date
date -d @1678886400
Converts a given Unix timestamp (`1678886400` is 2023-03-15 00:00:00 UTC) into a human-readable format.
Back up a File with the Current Date in the Name
cp config.conf config.conf.$(date +%Y%m%d)
Backs up the current `config.conf` file by including the current date in the new file name.