Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was going through a tutorial and there I came across a command:

printf "%(%s)T"

The output of this command is:

1454299615

Could anybody please tell me what is this command and what the output means?

share|improve this question
    
This seems to be shell dependent. Is your printf a builtin? /usr/bin/printf on Ubuntu 14.04 gives an error: printf: %(: invalid conversion specification. If you're using bash. Try man bash for builtin documentation. – arielf Feb 6 at 20:27
up vote 16 down vote accepted

It's a way to express the formatted time by printf.

The format is:

%(FORMAT)T

Where FORMAT is defined by strftime(3).

So to get the epoch time (Time in seconds since 1970-01-01 00:00:00 UTC), we need strftime(3) format %s:

printf "%(%s)T\n"

Note that you also need \n at the end to add a newline as printf (unlike echo) does not add it by default.

Example:

$ printf "%(%s)T\n"
1454300377

$ printf "%(%Y-%m-%d)T\n"
2016-02-01

$ printf "%(%Y-%m-%d %H:%M:%S)T\n"
2016-02-01 10:20:27

Just for the sake of completeness, you can also use date command in a similar strftime(3) formatted manner to get the time:

$ date '+%s'
1454300542

$ date '+%Y-%m-%d'    ## Short form: date -I
2016-02-01

$ date '+%Y-%m-%d %H:%M:%S'
2016-02-01 10:22:47
share|improve this answer
    
Thank you so much! – rtecxs Feb 1 at 4:27
    
@rtecxs I have moved your edit in a different place just to keep things clear..thanks anyway :) – heemayl Feb 1 at 4:33
    
Ok! Thank you again. – rtecxs Feb 1 at 4:34
    
Is this feature documented? I didn't see it in the printf(1) man page. – Nate Eldredge Feb 1 at 15:36
    
@NateEldredge printf is a builtin here (although the external one has the same behavior)..check help printf .. – heemayl Feb 1 at 16:10

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.