How Do I generate Unix timestamps?

In Linux, the timestamps (also known as epoch time) are generated to keep the data record for the particular event that is happening. There are various methods to generate timestamps in Linux, such as using the date command, bash script, or a python module.

This post will demonstrate methods to generate Unix timestamps in Linux.

  • Method 1: Using the “date” Command
  • Method 2: Using the Bash script 
  • Method 3: Using Python Module

Method 1: Using the “date” Command

The “date” is the command considered for printing the date and time. This can also be considered for generating timestamps in Linux. For instance, the following date command prints the format of “Year-Month-Day Hour-Minutes-Seconds

$ date +'%Y-%m-%d %H:%M:%S'

The current timestamp “2023-03-28 07:03:15” is printed on the screen.

Method 2: Using the Bash script 

The user can also use the bash script to generate the timestamp in Linux. Open the script with the nano editor and run the following script:

stamp=$(date +'%Y-%m-%d %H:%M:%S')
echo  "The timestamp is $stamp"

The script is defined as 

  • The shebang “#!/bin/bash” represents the bash script.
  • The “stamp” variable stores the output of the “date +’%Y-%m-%d %H:%M:%S’” command.
  • The “echo” command displays the timestamp stores in the “$stamp” variable.

Save the script file and exit.

Execute the script file using the “bash” command:

$ bash

The current timestamp “2023-03-28 07:20:22” is printed.

Method 3: Using the Python Module

Another method to generate the timestamp in Linux is by using the python module. Enter into the python module by typing the “python3” in the terminal and execute the following code to generate the time stamp:

$ python3
>>> import datetime
>>> time =
>>> time_stamp = time.timestamp()
>>> date_time = datetime.datetime.fromtimestamp(time_stamp)
>>> print(date_time)

The python code is defined as 

  • Import the “datetime” module 
  • Retrieve the current time and date using the “” module 
  • Convert the current time into a timestamp through the “time.timestamp()” module
  • Generate timestamp into date time format using the the “datetime.datetime.fromtimestamp(time_stamp)” module.

The current timestamp “2023-03-28 07:11:50.441331” is generated.


In Linux, the timestamp is generated through the date command, bash script, and the python module in various formats like “Year-Month-Day Hour-Minutes-Seconds”. This format is generated using the “date +’%Y-%m-%d %H:%M:%S‘” command in the terminal as well as in the bash script. For the python module, import the “datetime” module and generate the timestamp.

This write-up has enlightened the various methods to generate the timestamp in Linux.