Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
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). –  Michael Kjörling Jun 14 '13 at 7:25
    
yes, the title was wrong, though the post was correct, sorry for confusion. –  easl Jun 14 '13 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). –  Michael Kjörling Jun 14 '13 at 7:40
    
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 at 7:18

1 Answer 1

up vote 8 down vote accepted

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.