Linux date CommandOverviewIn Linux, the date command is used for displaying the time and date of the system. Also, it is used for setting the time and date of the system. The date command by default shows the date within the time zone over which Linux/Unix operating system is constructed. We should the root (super) user to modify the time and date. Syntax of the date command: Options in the date commandThe following are some important options in the date command: 1. date (no option): The date command shows the current time and date with no options including the year, the time zone name, the time isolated by colons, day of the month, abbreviated month name, and the abbreviated day name. The command is as follows: Output: The output will be as follows: Note: Unix system is constructed in neutral daylight time.2. -d or --date Option: This command shows the provided date string within the date format. But, it will not impact the actual time and date value of the system. Instead, it used the time and date given in the string form. Syntax: The command is as follows: Output: The output will be as follows: 3. -u Option: It shows the time in UTC (Coordinated Universal Time)/GMT (Greenwich Mean Time) time zone. The command is as follows: Output: The output will be as follows: 4. --date option to show past dates
The command is as follows: Output: The output will be as follows:
The command is as follows: Output: The output will be as follows:
The command is as follows: Output: The output will be as follows:
The command is as follows: Output: The output will be as follows:
The command is as follows: Output: The output will be as follows: 5. --date option to show the future date
The command is as follows: Output: The output will be as follows:
The command is as follows: Output: The output will be as follows:
The command is as follows: Output: The output will be as follows:
The command is as follows: Output: The output will be as follows: 6. --set or -s Option: It is used for setting the time and date of the system. Syntax: Command: Output: The output will be as follows: Command: Output: The output will be as follows: 7. -f or --file Option: It is used for displaying the date string available at all file lines in the time and date format. This option is the same as an option, i.e., --date. Although, a single difference between these options is that in --date option we can only provide a single date string but we can provide more than one date string at all the lines in a file. Syntax: The command is as follows: Output: The output will be as follows: 8. -r Option: It is used for displaying the modified timestamp (last) of a datefile. Syntax: Also, we can change the timestamp of any datefile by applying the touch command. Syntax: 9. Format specifier list used using the date command Syntax: Examples:Some of the examples of the date command are as follows:
Output:
Output:
Output:
Output:
Output: Formatting CharactersThere are the most widely used formatting characters of the date command: %Y: Year (e.g., 2021) %D: Show date as mm/dd/yy %m: Month (02-14) %B: Long month name (e.g., December) %b: Shirt month name (e.g., Dec) %d: Day of month (e.g., 02) %j: Day of year (002-366) %u: Day of week ((2-7) %A: Full weekday name (e.g., Saturday) %a: Short weekday name (e.g., Sat) %H: Hour (00:24) %I: Hour (02-12) %S: Second (00-60) %M: Minute (00-59) Note: We can execute the man date (man command) or date --help in our terminal to see every formatting option.Next TopicLinux hosts (file) |