Removing leading zeros from date output

awkdatesed

I made an alias of the date command to display date in the following format:

2013.06.14.12.10.02

using this command:

alias date = date +"%Y.%m.%d.%H.%M.%S"

Everything works great, except I want to remove the leading zeroes from the output.

There is no way to make it happen by changing the format. I think it can be done only by piping the output to other commands like sed and awk.

The OS I am running is Ubuntu 12.04.2 LTS.

Best Answer

As per the GNU date manpage:

   By default, date  pads  numeric  fields  with  zeroes.   The  following
   optional flags may follow '%':

   -      (hyphen) do not pad the field

Therefore you can do

alias date="date '+%Y.%-m.%-d.%-H.%-M.%-S'"

and receive

2013.6.14.3.19.31
Related Question