Get current time in seconds since the Epoch on Linux, Bash
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
On Linux, the standard way to get the current Unix time in seconds is date +%s. That value is the number of whole seconds since the Unix epoch, which is 1970-01-01 00:00:00 UTC, and it is widely used in scripts for logging, time measurement, and simple scheduling logic.
Core Sections
The basic command
The simplest answer in Bash is:
This prints the current Unix timestamp in seconds. For example, a script might capture it like this:
That is the normal shell answer for “current time in seconds since the epoch.”
Why epoch seconds are useful
Epoch seconds are convenient because they are timezone-independent as a stored numeric value. You can subtract two timestamps directly to get elapsed time in seconds without worrying about string parsing first.
This is one of the most common uses in shell scripts: quick runtime measurement.
They also work well in lightweight log files and cache keys because the value is compact, sortable as a number, and easy to compare in shell conditionals.
Convert back to a human-readable date
You often need to go in the other direction as well. On Linux systems with GNU date, use -d @SECONDS.
That helps when debugging logs or verifying that a numeric timestamp corresponds to the date you expect.
Seconds versus milliseconds
A common mistake is assuming Bash gives milliseconds by default. date +%s gives whole seconds only. If you need higher precision, GNU date also supports nanosecond formatting.
This returns seconds plus nanoseconds as a long integer string. If you want milliseconds, trim or divide appropriately.
Use this only when you genuinely need finer timing. For many scripts, second precision is enough and simpler.
Timezones and what they do not change
One subtle point is that the Unix timestamp itself is not altered by your displayed timezone. Changing TZ affects how dates are rendered, but not what date +%s means as the current absolute moment.
These represent the same current moment. The formatted calendar display differs, but the epoch timestamp is still the same underlying time reference.
Use shell built-ins carefully
In Bash, there are also shell features such as printf with time formatting in some contexts, but date +%s remains the most portable and recognizable Linux shell idiom. If the script is specifically Linux-targeted, this is usually the best answer.
For highly portable shell scripts across different Unix variants, always remember that some date options differ outside GNU/Linux. But on Linux and Bash specifically, date +%s is the standard tool.
Common Pitfalls
- Assuming
date +%sreturns milliseconds instead of seconds leads to wrong arithmetic and scheduling logic. - Forgetting to quote command substitutions in larger scripts can cause hard-to-see shell bugs unrelated to the timestamp itself.
- Mixing human-readable local time and epoch time in the same variables makes scripts harder to reason about.
- Expecting timezone changes to alter the actual epoch timestamp confuses display formatting with absolute time.
- Using second precision for high-resolution measurement when you actually need milliseconds or nanoseconds produces misleading timing data.
Summary
- On Linux, use
date +%sto get the current time in seconds since the Unix epoch. - Epoch seconds are useful because they are easy to store, compare, and subtract.
- Convert back to a human-readable form with
date -d "@TIMESTAMP"on GNU/Linux. - Use
+%s%Nand division if you need finer precision such as milliseconds. - Keep the distinction clear between absolute timestamps and timezone-formatted date strings.

