cat </dev/tcp/time.nist.gov/13

Get the time from NIST.GOV

The format is JJJJJ YR-MO-DA HH:MM:SS TT L DUT1 msADV UTC(NIST) OTM and is explained more fully here: http://tf.nist.gov/service/acts.htm
Sample Output
cat </dev/tcp/time.nist.gov/13

55168 09-12-03 20:32:43 00 0 0 350.2 UTC(NIST) * 

8
By: drewk
2009-12-03 21:40:14

These Might Interest You

  • Ever needed to test firewalls but didn't have netcat, telnet or even FTP? Enter /dev/tcp, your new best friend. /dev/tcp/(hostname)/(port) is a bash builtin that bash can use to open connections to TCP and UDP ports. This one-liner opens a connection on a port to a server and lets you read and write to it from the terminal. How it works: First, exec sets up a redirect for /dev/tcp/$server/$port to file descriptor 5. Then, as per some excellent feedback from @flatcap, we launch a redirect from file descriptor 5 to STDOUT and send that to the background (which is what causes the PID to be printed when the commands are run), and then redirect STDIN to file descriptor 5 with the second cat. Finally, when the second cat dies (the connection is closed), we clean up the file descriptor with 'exec 5>&-'. It can be used to test FTP, HTTP, NTP, or can connect to netcat listening on a port (makes for a simple chat client!) Replace /tcp/ with /udp/ to use UDP instead.


    15
    exec 5<>/dev/tcp/time.nist.gov/13; cat <&5 & cat >&5; exec 5>&-
    tyzbit · 2015-07-30 21:12:38 9

  • 1
    utime(){ python -c "import time; print(time.strftime('%a %b %d %H:%M:%S %Y', time.localtime($1)))"; }
    4Aiur · 2010-05-14 08:00:44 0
  • Adds the time in 12hr AM/PM format to the beginning of a prompt. Change \@ to \t for 24-hour time or \T for 12hr without AM/PM. To keep the time the next time you open a terminal, edit ~/.bashrc and stick the command at the bottom. Show Sample Output


    1
    export PS1="(\@) $PS1"
    cmdq · 2010-11-22 04:50:21 0
  • the --time-style argument to 'ls' takes several possible modifiers: full-iso, long-iso, iso, locale, +FORMAT. The +FORMAT modifier uses the same syntax as date +FORMAT. --time-style=+"%Y-%m-%d %H:%M:%S" strikes a happy medium between accuracy and verbosity: ls -lart --time-style=long-iso doesn't show time down to the nearest second, ls -lart --time-style=full-iso displays time to 10E-9 second resolution, but with no significant digits past the full seconds, also showing the timezone: -rw-r--r-- 1 bchittenden bchittenden 0 2011-02-10 12:07:55.000000000 -0500 bar Show Sample Output


    4
    ls -l --time-style=+"%Y-%m-%d %H:%M:%S"
    bartonski · 2011-02-10 17:15:37 2
  • I've had a horrible time trying to pipe the output of some shell built-ins like 'time' to other programs. The built-in doesn't output to stdout or stderr most of the time but using the above will let you pipe the output to something else. Show Sample Output


    1
    TIME=$( { time YOUR_COMMAND_HERE; } 2>&1 ) ; echo $TIME
    allrightname · 2010-11-18 15:48:05 0
  • Realtime lines per second in a log file using python ... identical to perl version, except python is much better :) Show Sample Output


    0
    tail -F /var/log/nginx/access.log | python -c 'exec("import sys,time\nl=0\ne=int(time.time())\nfor line in sys.stdin:\n\tt = int(time.time())\n\tl += 1\n\tif t > e:\n\t\te = t\n\t\tprint l\n\t\tl = 0")'
    pykler · 2012-05-15 21:56:46 0

What Others Think

I don't get it to work with bash. What are the prerequisites ?
CodSpirit · 441 weeks and 3 days ago
CodSpirit: On some systems, such as Debian and Debian offshoots, net redirections aren't enabled in Bash. You can do the same thing with curl, wget, or nc, though: curl -s time.nist.gov:13 nc time.nist.gov 13 wget -q -O- time.nist.gov:13
eightmillion · 441 weeks and 3 days ago
You have to enable net-redirections when compiling bash to do it. That doesn't work on debian derivated distros by default...
sputnick · 441 weeks and 3 days ago
Works for me on Ubuntu and OS X. I do not have Debian...
drewk · 441 weeks and 3 days ago
Indeed it is not enabled on Ubuntu 9.04 Thanks eightmillion for alternatives
CodSpirit · 441 weeks and 3 days ago
To convert to local time, you can use the date command, thus the full command will be: wget -q -O- time.nist.gov:13 | awk '{print $2,$3}' | grep -v "^ *$" | (read t; date "+%Y-%m-%d %H:%M:%S %Z" -d "$t +0000") (if it needs explaining, we get the date portion, skip empty line, read into variable t, then use date to print in local time)
Uriqimadh · 208 weeks and 3 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: