
the date
The command is in the bash shell, which is the default shell on most Linux distributions and even macOS. This tutorial shows you how to master date
on the command line and how you can use it in shell scripts to do more than just print the time.
Run the date
command to view this information. Print the current date and time for your time zone:
date
The default format seems a bit ridiculous. Why is the year not printed after the month and day, instead of being tagged at the end, behind the time zone? Do not worry: if what you want is to control the output format, date
hands it over with. There is more of 40 options you can go to date
to instruct you to format your output exactly how you would like.
To use any of the options, scribe date
, a space, a plus sign +
, and the option even the leading percent sign. the %c
(data and time in locale format) causes the date and time to be printed in the standard format associated with your locale. Your locale is determined by the geographic and cultural information that you provided when you installed your operating system.. Place rules things like the currency symbol, the paper size, time zone and other cultural norms.
date +%c
The year now appears in a more natural position in production.
You can pass various options to date
right away. A sequence of options is called a format string. To see the name of the day (%A
), the day of the month (%d
) and the name of the month (%B
), use this command:
date +%A%d%B
That worked, but it's ugly. No problem, we can include spaces as long as you wrap the entire format string in quotes. Note that the +
will outside the quotation marks.
date +"%A %d %B"
You can add text to the format string, So:
date +"Today is: %A %d %B"
Scroll up and down through the date
man page searching for the option you want soon becomes tedious. We have divided the options into groups to help you find your way more easily.
Options to display the date and time
- %C: Print the date and time in the format of your regional settings, including time zone.
Options for displaying the date
- %D: Print the date in mm format / dd / aa.
- %F: Print the date in yyyy-mm-dd format.
- %X: Print the date in your local format.
Options to display the day
- %a: Print the name of the day, abbreviated as Mon, Mar, Wed, etc.
- %A: Print the full name of the day, Monday Tuesday, Wednesday, etc.
- % u: Print the number of the day of the week, where monday = 1, Tuesday = 2, Wednesday = 3, etc.
- % w: Print the number of the day of the week, where Sunday = 0, Monday = 1, Tuesday = 2, etc.
- %D: Print the day of the month, with a leading zero (01, 02… 09) if required.
- %me: Print the day of the month, with a leading space (‘1’, ‘2’… ‘9’) if required. Please note that the apostrophes are not printed.
- % j: Print the day of the year, with up to two leading zeros, if required.
Options to display the week
- % U: Print the week number of the year, considering Sunday as the first day of the week. For instance, the third week of the year, the twentieth week of the year, etc.
- % V: Prints the ISO week number of the year, considering Monday as the first day of the week.
- % W: Week number of the year, considering Monday as the first day of the week.
Options to display the month
- %B O % h: Print the name of the month abbreviated as January, February, March, etc.
- %B: print the full name of the month, January, February, March, etc.
- %metro: Print the number of the month, with a leading zero if necessary 01, 02, 03… 12.
Options to display the year
- %C: Print the century without the year. On 2019 would print 20.
- % and: Print the year as two digits. on 2019 will print 19.
- % AND: Print the year with four digits.
Options to display the time
- % T: Print the time as HH: MM: SS.
- % R: Print the hour and minute as HH: MM without seconds, using the clock 24 hours.
- % r: Print the time based on your location, using the clock 12 hours and an am or pm indicator.
- %X: Print the time according to your location, using the clock 24 hours. Presumably. Note that during testing this option behaved exactly as
%r
does, as it's shown in the following. On a Linux machine configured for the UK locale and set to GMT, printed the time, using the clock 24 hours without indicator AM or PM, as expected.
Options to display the time
- % H: Print the time 00, 01, 02… 23.
- %I: Print the time using the clock 12 hours, 00, 01, 02… 12, with a leading zero if necessary.
Options to display minutes
- %METRO: print the minute, 01, 02, 03… 59, with a leading zero if necessary.
Options to show seconds
- %s: Prints the number of seconds since 1970-01-01 00:00:00, the beginning of the Unix era.
- %S: Print the seconds, 01, 02, 03… 59, with a leading zero if necessary.
- %NORTH: Print the nanoseconds.
Options for displaying time zone information
- % With: Print the time difference between your time zone and UTC.
- %: With: Print the time difference between your time zone and UTC, with a: between hours and minutes. Note the
:
Between the%
sign andz
. - % :: With: Print the time difference between your time zone and UTC, with a: between hours, the minutes and the seconds. Note the
::
Between the%
sign andz
. - % WITH: Print the alphabetical name of the time zone.
Format-related options
- %pag: Print the AM or PM indicator in uppercase.
- %PAG: Print the am or pm indicator in lowercase. Note the peculiarity of these two options. A lowercase
p
outputs in uppercase, a capital letterP
outputs lowercase. - % t: Print a tab.
- %North: Print a new line.
Options to modify other options
These modifiers can be inserted between the %
and the option letter of other options to modify their display. For instance, %-S
would remove the leading zero for single digit seconds values.
- –: A single dash prevents zero padding in single digit values.
- _: a simple underscore adds leading spaces for single digit values.
- 0: Provides leading zeros for single digit values.
- ^: Use capital letters, if possible (not all options respect this modifier).
- #: use the opposite case to the default for the, if possible (not all options respect this modifier).
Two more ingenious tricks
To get the last time to modify a file, Use the -r
(reference) option. Note that this uses a -
(screenplay) How is Windows S mode different %
sign, and does not require a +
sign. try this command in your home folder:
date -r .bashrc
TZ settings allow you to change your time zone for the duration of a single command.
TZ=GMT date +%c
Using the date in scripts
Enabling a Bash shell script to print the time and date is trivial. create a text file with the following content and save it as gd.sh
.
#!/bin/bash TODAY=$(date +"Today is %A, %d of %B") TIMENOW=$(date +"The local time is %r") TIME_UK=$(TZ=BST date +"The time in the UK is %r") echo $TODAY echo $TIMENOW echo $TIME_UK
Escriba el siguiente comando para establecer los permisos de ejecución y hacer que el script sea ejecutable.
chmod +x gd.sh
Run the script with this command:
./gd.sh
We can use the date command to provide a timestamp. The script shown will create a directory with the timestamp as its name. Then it will copy all text files from current folder to it. By running this script periodically, we can take a snapshot of our text files. Over time, we will create a series of folders with different versions of our text files.
Tenga en cuenta que este no es un sistema de copia de seguridad sólido, es solo para fines ilustrativos.
create a text file with the following content and save it as snapshot.sh.
#!/bin/bash # obtain the date and time date_stamp=$(date +"%F-%H-%M-%S") # make a directory with that name mkdir "$date_stamp" # copy the files from the current folder into it cp *.txt "$date_stamp" # all done, report back and exit echo "Text files copied to directory: "$date_stamp
Escriba el siguiente comando para establecer los permisos de ejecución y hacer que el script sea ejecutable.
chmod +x snapshot.sh
Run the script with this command:
./snapshot.sh
Verá que se ha creado un directorio. Su nombre es la fecha y hora en que se ejecutó el script. Inside that directory are copies of the text files.
With a little thought and creativity, even the humble date
The command can be used productively.
setTimeout(function(){
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq = n;n.push=n;n.loaded=!0;n.version=’2.0′;
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s) } (window, document,’script’,
‘https://connect.facebook.net/en_US/fbevents.js’);
fbq(‘init’, ‘335401813750447’);
fbq(‘track’, ‘PageView’);
},3000);