Display current date and time without punctuation
Here you go:
date +%Y%m%d%H%M%S
As man date
says near the top, you can use the date
command like this:
date [OPTION]... [+FORMAT]
That is, you can give it a format parameter, starting with a +
.
You can probably guess the meaning of the formatting symbols I used:
-
%Y
is for year -
%m
is for month -
%d
is for day - ... and so on
You can find this, and other formatting symbols in man date
.
A simple example in shell script
#!/bin/bash
current_date_time="`date +%Y%m%d%H%M%S`";
echo $current_date_time;
With out punctuation format :- +%Y%m%d%H%M%S
With punctuation :- +%Y-%m-%d %H:%M:%S
If you're using Bash you could also use one of the following commands:
printf '%(%Y%m%d%H%M%S)T' # prints the current time
printf '%(%Y%m%d%H%M%S)T' -1 # same as above
printf '%(%Y%m%d%H%M%S)T' -2 # prints the time the shell was invoked
You can use the Option -v varname
to store the result in $varname
instead of printing it to stdout:
printf -v varname '%(%Y%m%d%H%M%S)T'
While the date command will always be executed in a subshell (i.e. in a separate process) printf is a builtin command and will therefore be faster.