Unix time, POSIX time or Epoch time is defined as the number of seconds that have elapsed since 00:00:00 January 1, 1970 UTC. There are a few methods to retrieve the Epoch time on Unix and Linux.
Epoch Time with GNU date
On Linux or any systems with the GNU date installed, just specify the “%s
” format to the date
command.
1 2 |
$ date +%s 1412406438 |
Epoch Time with Perl
If GNU date
is not available, such as on Solaris system, perl
can be used.
1 2 |
$ perl -e 'printf "%d\n", time()' 1412406438 |
Epoch Time with Python
Python can also be used to retrieve the Epoch time. On Python 2.x, use the following syntax.
1 2 |
$ python2 -c 'import time; print "%d\n" % time.time();' 1412406438 |
On Python 3.x, use the new syntax below.
1 2 |
$ python3 -c 'import time; print("%d\n" % (time.time()));' 1412406438 |
Epoch Time with Awk
The final method is to use the pattern scanning and text processing tool – awk
.
1 2 |
$ echo | awk '{print srand()}' 1412406438 |
If the awk
command gives syntax errors, especially on Solaris systems, replace awk
with nawk
instead.
1 2 |
$ echo | nawk '{print srand()}' 1412406438 |