My colleague is generating log files with a preceding date format like 2015120, which represent January as 1 instead of 01.

The usual way I'm using to deal with this kind of issue is using date command.Like date +'%Y%m%d'. But I maned date command, it turns out they didn't mention represent January without a preceding 0.

So I'm wondering is there an another way to represent date like 2015120 in Linux?

  • 37
    ...what a bad idea ;-). So, a a glance, 2015112 is November, 2nd or January, 12th?
    – Rmano
    Commented Jan 20, 2015 at 8:57
  • 24
    You should get a better colleague. This is, simply put, a STUPID way of writing dates. Did you ask him why he did this? I suspect he simply didn't think of padding the month.
    – wurtel
    Commented Jan 20, 2015 at 9:58
  • 10
    But you save ONE byte for almost every date! That's 1GB for every 1073741824 log files! </sarcasm> Commented Jan 20, 2015 at 12:46
  • 9
    I would suggest that you and your colleage join efforts to migrate to a better date scheme. The whole purpose of prepending dates is to automatically get date ordering, but it doesn't help if Jan 31 (2015131) > Oct 1 (20151001).
    – Darkhogg
    Commented Jan 20, 2015 at 13:07
  • 9
    Any answer that doesn't start with you bodily assaulting your colleague with a fish (your choice) for choosing such an obviously idiotic date format is sub-par. Fix the date format; fix the problem. Do not implement this datespec. It will bite you in the arse before the year is over.
    – Oli
    Commented Jan 20, 2015 at 15:59

3 Answers 3


With GNU, FreeBSD or OS/X date (or date implementations that use the system's libc's strftime() where that is the GNU libc), adding hyphen - after % prevents numeric fields from being padded with zeroes:

$ date +'%Y%-m%d'

From man date on a GNU system:

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

- (hyphen) do not pad the field

If your system date does not support that, you can use perl:

$ perl -MTime::Piece -e '
  $t = localtime;
  print $t->year, $t->mon, $t->mday;
  • 3
    I'd add a nice # ... but you do NOT want that! comment on the same line Commented Jan 20, 2015 at 19:00
> date +'%Y %m %d' | ( read year month day; echo "${year}${month#0}${day}" )

If you add a one (1) after the hyphen (-) it works on Linux and is also portable to HP-UX (and possibly other flavors of UNIX):

$ date +'%Y%-1m%d'
  • Note that it's independent from the kernel, so "Linux" has no bearing here. ITYM it works on GNU and HP/UX. However note that contrary to date +%-m, it doesn't work on FreeBSD or OS/X. Commented Jan 21, 2015 at 21:08

You must log in to answer this question.

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