History with date linux

How to see time stamps in bash history?

Is there any way I can see at what time the commands were executed from the bash history? We can see the order but is there any way I can get the time also? Bottom-Line: Execution time in the Bash history

According to the blog referenced by @dog0 , timestampse apparently do not get saved unless the HISTTIMEFORMAT variable was set when commands were issued. In other words, if you didn’t have it set, setting it now will not help you retrieve timestamps of previously issued commands.

9 Answers 9

Press Ctrl + Alt + T to open a terminal, then run one of the commands below:

HISTTIMEFORMAT="%d/%m/%y %T " # for e.g. “29/02/99 23:59:59” HISTTIMEFORMAT="%F %T " # for e.g. “1999-02-29 23:59:59” 

To make the change permanent for the current user run:

echo 'HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bashrc # or respectively echo 'HISTTIMEFORMAT="%F %T "' >> ~/.bashrc source ~/.bashrc 

For commands that were run before HISTTIMEFORMAT was set, the current time will be saved as the timestamp. Commands run after HISTTIMEFORMAT was set will have the proper timestamp saved.

Note that this will only record timestamps for new history items, after HISTTIMEFORMAT is set for sessions, i.e. you can’t use this retrospectively. Some answers here give the impression the history command immediately shows timestamped entries

Just a note to zsh users out there — unlike many builtins it does not mirror bash. See this instead: unix.stackexchange.com/questions/103398/…

Weirdly, this did appear to work for me retrospectively — I was able to see the timestamps of history items prior to my explicit setting of this environment variable. That may be because HISTTIMEFORMAT was set to an empty string before, or something similar?

Open terminal Ctrl + Alt + T and run,

To make the changes permanent follow the below steps,

you need to add the below line to .bashrc file and then save it,

export HISTTIMEFORMAT="%d/%m/%y %T " 

run the below command to source .bashrc file,

After that run history command.

enter image description here

Hey, I’m doing this in OS X and Windows (through MINGW), and I’m adding it into .bash_profile, what’s the diff between profile and rc?

Yes, you can: if you set $HISTTIMEFORMAT , the .bash-history will be properly timestamped. That doesn’t help with existing .bash-history content, but will help in the future.

Читайте также:  Installing java linux terminal

Correction to previous post: Setting HISTTIMEFORMAT enables the display of the timestamps. even existing. My favorite is:HISTTIMEFORMAT=’%F %T ‘ as it matters not from which country you reside. everyone knows immediately what time it is. 🙂

@user491029 «. even existing». This is true, but misleading. On ubuntu 14.04, at least, it did start showing timestamps for all history entries once I had set HISTTIMEFORMAT, but it looks like the timestamps for any command run before the current session were the login timestamp of the current session.

Changing HISTIMEFORMAT didn’t work for me, because I’m using zsh.

If you want to make it work with zsh, you just have to type : history -i

You ‘ll see changes on next login.

echo 'HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bashrc 
HISTTIMEFORMAT="%F %T %z " history | grep 'your command' 
╭you@your-server:/some/path ╰$ HISTTIMEFORMAT="%F %T %z "; history | grep 'some command' 1947 2019-12-17 15:54:34 +0800 ./some.sh 1948 2019-12-17 15:54:34 +0800 ./my-command 

View full syntax for HISTTIMEFORMAT here

To enable history timestamps for all users, create a script in /etc/profile.d :

echo 'HISTTIMEFORMAT="%Y%m%d %T "' >> /etc/profile.d/timestamp.sh 

This will likely not work for users logging in via the GUI, since the default terminal will not run login shells. /etc/bash.bashrc would be a better place.

Make sure permission of /etc/profile.d/timestamp.sh is ‘644’ , so that history command shows timestamp for all users .

I maintain a history per tty device per user, in .bash_profile, and let them grow large. Sometimes I want to find where a command was used across all those files using grep, say. I have just spent a while getting how I want, with time stamps as strings and headers for each file plus which tty before the time so if I am grepping I will see where and when used:

$ awk 'BEGIN " FILENAME "; if ((ln==0) && ($1 ~/^5+$/)); if ($1 ~/^6+$/) else >> ' .bash_history_ttys003|head ==>.bash_history_ttys003 

Can't go into all the background as life is too short (LITS) but here are my dev notes as I worked it up. This is on macOS so no special gawk but I do use date -r where it may differ for you, date -d?. Enjoy! :

How would I list all .bash_history* lines that grep finds together with the Unix timestamp as string? sample data lines in history (#unix time \n command) head .bash_history_ttys007 \#1502956374 man top \#1502956600 top As 2 lines looks like an awk task. Can't print FILENAME in begin as there is no current file at that stage :( so use FNR file rec numb check. awk 'BEGIN ; "date -r "$1"" | getline textDate; close(date);print textDate, $0>' awk 'BEGIN ; "date -r "$1"" | getline textDate; close(date);print textDate, $0>' .bash_history_ttys00[67]|head Works but 3 lines per item as x0a line-returns are in there. WILL HAVE OTHER PROBLEMS TOO (later). Progress by field sep on new lines and carve up filename for controlling terminal awk 'BEGIN " FILENAME "; if ($1 ~/^4+$/) >' .bash_history_ttys00[67]|head $ awk 'BEGIN " FILENAME "; if ($1 ~/^4+$/) >' .bash_history_ttys002 Works OK but runs a bit slow: just do dates every once in a while? then if grep-ing will probably miss it. awk 'BEGIN " FILENAME "; if ($1 ~/^9+$/) >' .bash_history_ttys002 Do occasional date convert and retain string and offset secs? this does run much faster and requires timestamps to avoid # in commands. About 5s for 20,000 commands for me. awk 'BEGIN " FILENAME "; if ((ln==0) && ($1 ~/^6+$/)); if ($1 ~/^8+$/) else >> ' .bash_history_ttys003 

Источник

Set Date and Time for Each Command You Execute in Bash History

By default, all commands executed by Bash on the command line are stored in history buffer or recorded in a file called ~/.bash_history. This means that a system administrator can view a list of commands executed by users on the system or a user can view his/her command history using the history command like so.

Linux History Command

From the output of the history command above, the date and time when a command was executed is not shown. This is the default setting on most if not all Linux distributions.

In this article, we will explain how you can configure time stamp information when each command in the Bash history was executed to be displayed.

The date and time associated with each history entry can be written to the history file, marked with the history comment character by setting the HISTTIMEFORMAT variable.

There are two possible ways of doing this: one does it temporarily while the other makes it permanent.

To set HISTTIMEFORMAT variable temporarily, export it as below on the command line:

In the export command above, the time stamp format:

  1. %F – expands to full date same, as %Y-%m-%d (year-month-date).
  2. %T – expands to time; same as %H:%M:%S (hour:minute:seconds).

Read through the date command man page for additional usage information:

Then check your command history as follows:

Display Linux Command History with Date and Time

However, if you want to configure this variable permanently, open the file ~/.bashrc with your favorite editor:

And add the line below in it (you mark it with a comment as your own configuration):

#my config export HISTTIMEFORMAT='%F %T'

Save the file and exit, afterwards, run the command below to effect the changes made to the file:

That’s all! Do share with us any interesting history command tips and tricks or your thoughts about this guide via the comment section below.

Источник

Enable Timestamp in History Command in Linux

You can easily see which commands you ran in the past. But do you know when did you run it? The history command can tell you that as well.

You are familiar with the history command in Linux. It lets you see which commands you ran in the past.

But there is one issue. By default, the history command does not show when the command was executed (with date and time).

This could be helpful in some cases to know the time when a certain command was executed last.

And in this quick tip, I will show you how you can enable timestamps in the history command.

Enable timestamp in history command

To enable timestamps in the history command, you have to export the HISTTIMEFORMAT environment variable.

The modifications should now be reflected when you use the history command again:

enable timestamp in history command

However, as the modifications are only effective during the current session, the history command will return to its previous appearance after a reboot.

And if you want to apply timestamp permanently, here's how you do it.

Enable history timestamp permanently

To have a timestamp in the history command permanently, you'll have to make changes in .bashrc file.

Yes, no surprises there. If you want to make an environment variable permanent, it goes in your bashrc.

Open the .bashrc file in your favorite text editor. I am using Nano here.

You can use Alt + / to jump to the end of the file in Nano and add the following line:

enable timestamp in history command permanently

Now, to take those changes into effect, you will have to source the .bashrc file using the source command:

And now, whenever you will use the history command, it will show the timestamps with every command.

Wrapping Up

This was a quick tutorial on how you can enable timestamps in the history command. I hope you find this quick history command tip helpful in your Linux journey. Here are a few more.

Do leave a comment if you have any doubts or suggestions.

Источник

Оцените статью
Adblock
detector