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


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.

  • There are no trailing zeroes in your example (well, unless you count the 0 in the 10 minutes past the hour, but removing that changes meaning of the timestamp). I take it you mean leading zeroes (zeroes at the beginning of each date component), in which case if you are using a GNU userland @j883376's answer will likely be useful. Otherwise, please specify your environment (userland tools are not the same in all Unix-like OSes, and not even all tools might be available on all such OSes).
    – user
    Jun 14, 2013 at 7:25
  • yes, the title was wrong, though the post was correct, sorry for confusion.
    – easl
    Jun 14, 2013 at 7:33
  • No worries. As a general rule, though, it is always good to specify your environment. OS X is different from OpenBSD is different from AIX is different from GNU. By specifying your environment, you don't risk getting answers which won't be of any use to you (like, say, answers suggesting using Linux's /proc when you are trying to solve a problem on OS X).
    – user
    Jun 14, 2013 at 7:40
  • 4
    Just a remark: Be aware that removing leading zeros makes it harder to sort by those dates, if you ever need to.
    – Dubu
    Jul 4, 2014 at 7:18
  • See this post: stackoverflow.com/questions/47880412/… Mar 1, 2018 at 23:12

4 Answers 4


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


Feels silly, but since this question is tagged with /sed, here is a way to do this with sed, as you had mentioned :)

alias date='date +"%Y.%m.%d.%H.%M.%S" | sed "s/^0*//g; s/\.0*/./g"'

Notice the comments below. They have very valid points about removing the *, and removing the g option in the first sed expression. I'd rather not modify what I have written above without testing it, and I don't have time right now. Scott's comments below put common sense into this answer.

  • 1
    At two seconds past noon, date will say 2013., and sed will output 2013.6.14.12..2.  Note that the 00 minutes field disappears completely, leaving two adjacent dots.  This is probably not what the user wants.  To do this correctly in GNU sed, use sed -E -e 's/^0?//' -e 's/\.0?/./g' (to delete zero or one 0).  Or, to do it portably in GNU or POSIX sed, use sed -e 's/^0//' -e 's/\.0/./g' (to delete one 0, or not; it’s OK if the substitution simply fails when there isn’t a leading 0).  … (Cont’d) Sep 6, 2020 at 5:46
  • 1
    (Cont’d) … Note that a g option is useless on a substitute that’s anchored with ^ or $, since it cannot happen more than once. Sep 6, 2020 at 5:46

XFCE setting for no 0 in front of the time

%l:%M %p

Using printf gives you some flexibility.

For example:

/usr/bin/date "+%b %-2d %-H" | xargs printf "%s %2d %02d"

  • What are you saying?  “You can do lots of formatting with printf; go look up the details yourself.”?  This does not produce the YMDHMS format requested by the question, and, after stripping the leading zero from the hour, it puts it back! So this is not an answer to the question. Sep 6, 2020 at 5:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .