Mohammedz.com

For Linux and Shell scripting.

Linux: Timestamp conversion methods

Leave a comment


A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred. This data is usually presented in a consistent format, allowing for easy comparison of two different records and tracking progress over time.

Unix timestamp is defined as the number of seconds elapsed since midnight Coordinated Universal Time (UTC) of January 1, 1970, not counting leap seconds. The Unix epoch is the time 00:00:00 UTC on January 1, 1970.

Some different ways to find timestamps:

# timestamp of a file (that means, file creation time in timestamp).
stat -c %Y filename.txt

# present date & time as timestamp.
date +%s

# convert timestamp to real time (readable format).
awk “BEGIN { print strftime(\”%c\”,$timestamp) }”

echo $timestamp|awk ‘{print strftime(“%c”,$1)}’

perl -e “print scalar(localtime($timestamp))”

perl -e “require ‘ctime.pl’; print &ctime($timestamp);”

# you can use date command if it has -d/–date option available.
date -d “1970-01-01 $timestamp sec GMT”

If these commands didn’t work when you copy&paste, you can download it from here.

~mohammed

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s