How to check history commands 2-3 days older in ubuntu?

Kunal28

New Member
Credits
11
I know to check history we use falling command:

export HISTTIMEFORMAT='%F %T '

But it displays history of present day only. How could someone check history of past days?
 


JasKinasis

Well-Known Member
Credits
5,160
I know to check history we use falling command:

export HISTTIMEFORMAT='%F %T '

But it displays history of present day only. How could someone check history of past days?
No, to check the bash history, you use the built-in history command.

Setting HISTTIMEFORMAT just sets the format string used for the timestamps displayed in the history file.

After setting this variable to '%F %T ' - whenever you use the history command, you'll get every single command in the file listed, but it will have the "number" of the command in the history file and the timestamp consisting of the date (%F) and the time (%T), followed by the command.

e.g.
Code:
1995 2020-10-12 16:55:05  ls -t | head -n 10
So you'll see every single command in the history file with their time-stamps.

Now, at the time of posting this, the date is the 12th october 2020.
If you look at the man-page for the date command, you'll see that the date format used by %F is YYYY-MM-DD. So today would be 2020-10-12.

Yesterday would have been 2020-10-11.
So - assuming you have the timestamp set - if you want to see the commands from yesterday, you could simply use:
Bash:
history | \grep 2020-10-11
Or, if you didn't know todays date, you could find yesterdays date and use it to filter out commands from yesterday like this:
Bash:
history | \grep "$(date --date='yesterday' +%F)"
Or if you want to find the commands from three weeks ago (assuming there are entries from that long ago):
Code:
history | \grep "$(date --date='-3 weeks' +%F"
Simple!

I use the terminal a lot, so typically - I don't have commands much older than two or three days. If you want to keep a more comprehensive log - you either need to increase the value of HISTSIZE and/or HISTFILESIZE.

OR - if you set them to a negative number - it will log every single command. But it will be up to you to keep an eye on the log-files size.

And if you set it to 0, it won't keep any history at all.
 
Last edited:

dos2unix

Well-Known Member
Credits
3,030
A few comments about this.

"Most" (not all) distros keep history since the last reboot.
However the scroll-back buffer in your console/terminal may truncate the output.
Some distro's rotate the history every so often, but you can change this.

Another thing to keep in mind, is not every has the same shell session.
Not even the same user has the same shell session always. For example
you could login as user Bob, and I could login as user Mike, and if we both typed
history at the same time, we would see two different lists.

We could also both login as user root at the same time, but it would still be two different sessions,
so our history would be different during those sessions.
 

JasKinasis

Well-Known Member
Credits
5,160
A few comments about this.

"Most" (not all) distros keep history since the last reboot.
However the scroll-back buffer in your console/terminal may truncate the output.
Some distro's rotate the history every so often, but you can change this.

Another thing to keep in mind, is not every has the same shell session.
Not even the same user has the same shell session always. For example
you could login as user Bob, and I could login as user Mike, and if we both typed
history at the same time, we would see two different lists.

We could also both login as user root at the same time, but it would still be two different sessions,
so our history would be different during those sessions.
Also very good points!
 

Members online


Latest posts

Top