Mohammedz.com

For Linux and Shell scripting.


Leave a comment

HISTTIMEFORMAT: add execution time to Linux’s history file

As you might know, Linux logs all the commands executed on a shell prompt to its history file. The file is .bash_history by default, but you can change it. Also, you can change HISTTIMEFORMAT to enable timestamping to history.

If you add the following line to your bashrc or bash_profile, the time stamp information associated with each history entry will be written to the history file.

HISTTIMEFORMAT=”%h-%d-%Y – %H:%M:%S ”

Regards,
Mohammed.


Leave a comment

Linux: Timestamp conversion methods

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