bash date - ghdrako/doc_snipets GitHub Wiki
- Bash 4.2 added printf %(datefmt)T
- There are two special arguments,
-
-1
, which means “now,” and -
-2
, which means “the time the shell was invoked.”
-
### Set a $today variable using -v
$ printf -v today '%(%F)T' '-1'
$ echo $today
2021-08-13
### Trivial logging (these are the same)
$ printf '%(%Y-%m-%d %H:%M:%S %z)T: %s\n' '-1' 'Your log message here'
$ printf '%(%F %T %z)T: %s\n' '-1' 'Your log message here'
2021-08-13 12:48:33 -0400: Your log message here
### When was 1628873101?
$ printf '%(%F %T)T = %s\n' '1628873101' 'Epoch 1628873101 to human readable'
2021-08-13 12:45:01 = Epoch 1628873101 to human readable
### show the name of the month from two months ago
$ date -d '2 months ago' '+%B'
$ date +%s
1629582882
$ date -d @1629582882 '+%m/%d/%Y:%H:%M:%S'
08/21/2021:21:54:43
date --date="next week Fridey"
date --date="now"
date --date="tomorrow"
date --date="yesterday"
date --date="last week"
date --date="next week"
date --date="last month"
date --date="2015-09-12"
date --date="08/01/2015"
date -r start.sh # -r find the last modified time of the file
vi file-$(date "+%Y-%m-%d_%I:%M:%S").txt
dd
abbreviates to date --date="<given_date>"
.
date "<some_format>" # prints the date in the specified format
-
%Y
is the current year -
%m
is the current month -
%d
is today's date -
%I
is hours -
%M
is minutes -
%S
is seconds