Linux Commands – date
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed wc command in Linux which is used to counts the number of bytes, characters, whitespace-separated words, and newlines in each given FILE or standard input and output the result in standard output.
https://cloudaffaire.com/linux-commands-wc/
In this blog post, we will discuss the date command in Linux. date command displays the current time in the given FORMAT, or set the system date. Invoking date command with no FORMAT argument is equivalent to invoking it with a default format that depends on the ‘LC_TIME’ locale category. In the default C locale, this format is ”+%a %b %e %H:%M:%S %Z %Y”, so the output looks like ‘Thu Mar 3 13:47:51 PST 2005’.
Linux Commands – date:
You can use the date command to print the current DateTime of the system.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
########################### ## Linux Commands | date ## ########################### ## Prerequisites: One Unix/Linux/POSIX-compliant operating system with bash shell ##----- ## date ##----- ## date [option]... [+format] date ## returns Sun, May 24, 2020 7:18:38 PM date '+%d/%m/%Y' ## returns 24/05/2020 |
date command supports different FORMAT arguments to format the output of date command. All the FORMAT arguments start with % and are substituted during runtime. If given an argument that starts with a ‘+’, ‘date’ prints the current date and time or the date and time specified by the –date option in the format defined by that argument. Below is an exhaustive list of all arguments that can be passed to format the output.
Argument | Details |
%% | a literal % |
%a | locale’s abbreviated weekday name (e.g., Sun) |
%A | locale’s full weekday name (e.g., Sunday) |
%b | locale’s abbreviated month name (e.g., Jan) |
%B | locale’s full month name (e.g., January) |
%c | locale’s date and time (e.g., Thu Mar 3 23:05:25 2005) |
%C | century; like %Y, except omit last two digits (e.g., 20) |
%d | day of month (e.g., 01) |
%D | date; same as %m/%d/%y |
%e | day of month, space padded; same as %_d |
%F | full date; same as %Y-%m-%d |
%g | last two digits of year of ISO week number (see %G) |
%G | year of ISO week number (see %V); normally useful only with %V |
%h | same as %b |
%H | hour (00..23) |
%I | hour (01..12) |
%j | day of year (001..366) |
%k | hour, space padded ( 0..23); same as %_H |
%l | hour, space padded ( 1..12); same as %_I |
%m | month (01..12) |
%M | minute (00..59) |
%n | a newline |
%N | nanoseconds (000000000..999999999) |
%p | locale’s equivalent of either AM or PM; blank if not known |
%P | like %p, but lower case |
%r | locale’s 12-hour clock time (e.g., 11:11:04 PM) |
%R | 24-hour hour and minute; same as %H:%M |
%s | seconds since 1970-01-01 00:00:00 UTC |
%S | second (00..60) |
%t | a tab |
%T | time; same as %H:%M:%S |
%u | day of week (1..7); 1 is Monday |
%U | week number of year, with Sunday as first day of week (00..53) |
%V | ISO week number, with Monday as first day of week (01..53) |
%w | day of week (0..6); 0 is Sunday |
%W | week number of year, with Monday as first day of week (00..53) |
%x | locale’s date representation (e.g., 12/31/99) |
%X | locale’s time representation (e.g., 23:13:48) |
%y | last two digits of year (00..99) |
%Y | year |
%z | +hhmm numeric time zone (e.g., -0400) |
%:z | +hh:mm numeric time zone (e.g., -04:00) |
%::z | +hh:mm:ss numeric time zone (e.g., -04:00:00) |
%:::z | numeric time zone with : to necessary precision (e.g., -04, +05:30) |
%Z | alphabetic time zone abbreviation (e.g., EDT) |
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
## date FORMAT options ## %% a literal % date '+%Y%%%m%%%d' ## returns 2020%05%24 ## %a locale's abbreviated weekday name (e.g., Sun) date '+%a' ## returns Sun ## %A locale's full weekday name (e.g., Sunday) date '+%A' ## returns Sunday ## %b locale's abbreviated month name (e.g., Jan) date '+%b' ## returns May ## %B locale's full month name (e.g., January) date '+%B' ## returns May ## %c locale's date and time (e.g., Thu Mar 3 23:05:25 2005) date '+%c' ## returns Sun, May 24, 2020 7:41:45 PM ## %C century; like %Y, except omit last two digits (e.g., 20) date '+%C' ## returns 20 ## %d day of month (e.g., 01) date '+%d' ## returns 24 ## %D date; same as %m/%d/%y date '+%D' ## returns 05/24/20 ## %e day of month, space padded; same as %_d date '+%e' ## returns 24 ## %F full date; same as %Y-%m-%d date '+%F' ## returns 2020-05-24 ## %g last two digits of year of ISO week number date '+%g' ## returns 20 ## %h same as %b date '+%h' ## returns May ## %H hour (00..23) date '+%H' ## returns 19 ## %I hour (01..12) date '+%I' ## returns 07 ## %j day of year (001..366) date '+%j' ## returns 145 ## %k hour, space padded ( 0..23); same as %_H date '+%k' ## returns 19 ## %l hour, space padded ( 1..12); same as %_I date '+%l' ## returns 7 ## %m month (01..12) date '+%m' ## returns 05 ## %M minute (00..59) date '+%M' ## returns 52 ## %n a newline date '+Year=%Y%nMonth=%m' ## returns ## Year=2020 ## Month=05 ## %N nanoseconds (000000000..999999999) date '+%N' ## returns 258026200 ## %p locale's equivalent of either AM or PM; blank if not known date '+%l %p' ## returns 7 PM ## %P like %p, but lower case date '+%l %P' ## returns 7 pm ## %r locale's 12-hour clock time (e.g., 11:11:04 PM) date '+%r' ## returns 7:56:55 PM ## %R 24-hour hour and minute; same as %H:%M date '+%R' ## returns 19:57 ## %s seconds since 1970-01-01 00:00:00 UTC date '+%s' ## 1590330464 ## %S second (00..60) date '+%S' ## returns 10 ## %t a tab date '+%H%t%M%t%S' ## returns 19 59 09 ## %T time; same as %H:%M:%S date '+%T' ## returns 19:59:34 ## %u day of week (1..7); 1 is Monday date '+%u' ## returns 7 ## %U week number of year, with Sunday as first day of week (00..53) date '+%U' ## returns 21 ## %V ISO week number, with Monday as first day of week (01..53) date '+%V' ## returns 21 ## %w day of week (0..6); 0 is Sunday date '+%w' ## returns 0 ## %W week number of year, with Monday as first day of week (00..53) date '+%W' ## returns 20 ## %x locale's date representation (e.g., 12/31/99) date '+%x' ## returns 05/24/2020 ## %X locale's time representation (e.g., 23:13:48) date '+%X' ## returns 8:02:05 PM ## %y last two digits of year (00..99) date '+%y' ## returns 20 ## %Y year date '+%Y' ## returns 2020 ## %z +hhmm numeric time zone (e.g., -0400) date '+%z' ## returns +0530 ## %:z +hh:mm numeric time zone (e.g., -04:00) date '+%:z' ## returns +05:30 ## %::z +hh:mm:ss numeric time zone (e.g., -04:00:00) date '+%::z' ## returns +05:30:00 ## %:::z numeric time zone with : to necessary precision (e.g., -04, +05:30) date '+%:::z' ## returns +05:30 ## %Z alphabetic time zone abbreviation (e.g., EDT) date '+%Z' ## returns IST |
You can use date -d DATESTR or –date=DATESTR to display the date and time specified in DATESTR instead of the current date and time. DATESTR can be in almost any common format. It can contain month names, time zones, am and pm, yesterday, etc. -d option can also be used to convert seconds since the epoch (1970-01-01 UTC) to a date. You can also pass Time Zone (TZ=”RULE”) information using this option.
1 2 3 4 5 6 7 8 |
## date -d DATESTR or --date=DATESTR options date -d '01/01/2020 20:20:20' ## returns Wed, Jan 1, 2020 8:20:20 PM date -d '01/01/2020 20:20:20' +'%Y/%M/%d' ## returns 2020/20/01 date -d 'next tuesday' ## returns Tue, May 26, 2020 12:00:00 AM date -d @915139150 ## returns Fri, Jan 1, 1999 2:49:10 AM date -d '01/01/1999' +'%s' ## returns 915129000 date -d 'TZ="Europe/Paris"' ## returns Europe/Paris time zone current time |
You can use date -I[TIMESPEC] or –iso-8601[=TIMESPEC] options to display the date using the ISO 8601 format, ‘%Y-%m-%d’. The argument TIMESPEC specifies the number of additional terms of the time to include. It can be one of the following:
- auto: Print just the date. This is the default if TIMESPEC is omitted.
- hours: Append the hour of the day to the date.
- minutes: Append the hours and minutes.
- seconds: Append the hours, minutes and seconds.
- ns: Append the hours, minutes, seconds and nanoseconds.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
## date -I[TIMESPEC] or --iso-8601[=TIMESPEC] options date ## returns Mon, May 25, 2020 8:49:45 AM ## auto - Print just the date. This is the default if TIMESPEC is omitted. date -I ## returns 2020-05-25 ## hours - Append the hour of the day to the date. date -Ihours ## returns 2020-05-25T08+05:30 ## minutes - Append the hours and minutes. date -Iminutes ## returns 2020-05-25T08:53+05:30 ## seconds - Append the hours, minutes and seconds. date -Iseconds ## returns 2020-05-25T08:54:23+05:30 ## ns - Append the hours, minutes, seconds and nanoseconds. date -Ins ## returns 2020-05-25T08:54:59,549507700+05:30 |
You can use date -r FILE or –reference=FILE options to display the date and time of the last modification of FILE, instead of the current date and time.
1 2 3 4 5 6 7 |
## date -r FILE or --reference=FILE options touch myfile ## create an empty file ls -l myfile ## -rw-r--r-- 1 user 197121 292 May 25 08:59 myfile date -r myfile ## returns Mon, May 25, 2020 8:59:49 AM |
You can use date -R or –rfc-822 or –rfc-2822 options to display the date and time using the format ‘%a, %d %b %Y %H:%M:%S %z’, evaluated in the C locale so abbreviations are always in English. For example: Fri, 09 Sep 2005 13:51:39 -0700. This format conforms to Internet RFCs 2822
1 2 3 4 5 |
## date -R or --rfc-822 or --rfc-2822 options date ## returns Mon, May 25, 2020 9:03:32 AM date -R ## returns Mon, 25 May 2020 09:03:49 +0530 |
You can use date –rfc-3339=TIMESPEC option to display the date using a format specified by Internet RFC 3339. This is a subset of the ISO 8601 format, except that it also permits applications to use a space rather than a ‘T’ to separate dates from times. Unlike the other standard formats, RFC 3339 format is always suitable as input for the –date and –file options, regardless of the current locale. The argument TIMESPEC specifies how much of the time to include. It can be one of the following:
- date: Print just the full-date, e.g., ‘2005-09-14’. This is equivalent to the format ‘%Y-%m-%d’.
- seconds: Print the full-date and full-time separated by a space, e.g., ‘2005-09-14 00:56:06+05:30’.
- ns: Like seconds, but also print nanoseconds, e.g., ‘2005-09-14 00:56:06.998458565+05:30’.
1 2 3 4 5 6 7 8 9 10 11 12 |
## date --rfc-3339=TIMESPEC option date ## returns Mon, May 25, 2020 9:11:07 AM ## date - Print just the full-date date --rfc-3339=date ## returns 2020-05-25 ## seconds - Print the full-date and full-time separated by a space date --rfc-3339=seconds ## returns 2020-05-25 09:11:18+05:30 ## ns - Like seconds, but also print nanoseconds date --rfc-3339=ns ## returns 2020-05-25 09:11:33.472724700+05:30 |
You can use date -u or –utc or –universal options to display date in UTC format. Coordinated Universal Time (UTC) is often called Greenwich Mean Time (GMT) for historical reasons. Typically, systems ignore leap seconds and thus implement an approximation to UTC rather than true UTC.
1 2 3 4 5 |
## date -u or --utc or --universal options date ## returns Mon, May 25, 2020 9:15:53 AM date -u ## returns Mon, May 25, 2020 3:45:53 AM |
You can use date -f DATEFILE or –file=DATEFILE options to parse each line in DATEFILE as with ‘-d’ and display the resulting date and time. If DATEFILE is ‘-‘, use standard input. This is useful when you have many dates to process, because the system overhead of starting up the date executable many times can be considerable. For example, you can print the last modified date of all files in a particular directory with specific format using this option.
1 2 3 4 5 6 7 |
## date -f DATEFILE or --file=DATEFILE options ## below two example prints last modified date of all files ## inside /etc directory without format and with format sudo find /etc -type f -exec stat {} --printf="%y\n" \; sudo find /etc -type f -exec stat {} --printf="%y\n" \; | date --file=- |
You can use date -s DATESTR or –set=DATESTR options to set the date and time of the system to DATESTR. But since in most of the Linux systems, current date and time is set automatically using ntp, this is no longer a good option to set system date and time and may have an adverse effect.
1 2 3 4 5 |
## date -s DATESTR or --set=DATESTR options ## just for demo, do not execute below command as ## it may have an adverse effect to your system date --set="20020202 20:20" ## sets the current date and time of the system |
Hope you have enjoyed this article. In the next blog post, we will discuss find command in Linux.