Linux
Bash
Programming
Epoch Time
Time Conversion

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:

bash
date +%s

This prints the current Unix timestamp in seconds. For example, a script might capture it like this:

bash
now=$(date +%s)
echo "$now"

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.

bash
1start=$(date +%s)
2sleep 2
3end=$(date +%s)
4
5echo "$((end - start))"

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.

bash
ts=$(date +%s)
date -d "@$ts"

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.

bash
echo "$(date +%s%N)"

This returns seconds plus nanoseconds as a long integer string. If you want milliseconds, trim or divide appropriately.

bash
millis=$(($(date +%s%N) / 1000000))
echo "$millis"

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.

bash
TZ="UTC" date +%s
TZ="America/Toronto" date +%s

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 +%s returns 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 +%s to 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%N and division if you need finer precision such as milliseconds.
  • Keep the distinction clear between absolute timestamps and timezone-formatted date strings.

Course illustration
Course illustration

All Rights Reserved.