Often, you’ll go looking for a shell command that you used in the past – you can remember around when you did it but of course that won’t help you because by default the bash history won’t show you dates/times. Here is an easy way to remedy that. It won’t go into the past and give you dates, but from this point forward you’ll know when commands were executed.
There are two ways to do this: globally or per user
Globally: edit /etc/bashrc (or /etc/bash.bashrc in ubuntu)
Per user: edit ~/.bashrc
Add this to the bottom of the file to add date/time:
1 |
export HISTTIMEFORMAT="%h %d %H:%M:%S " |
Now, when you type history, it will show this:
1 2 3 4 |
422 Jun 10 14:14:42 cd themes/standard/ 423 Jun 10 14:14:42 ls 424 Jun 10 14:15:02 grep -ri "google" * 425 Jun 10 14:15:07 vim single.php |
Now let’s say that you want more history to be kept – by default it’s set to 500 lines. Add this to the bottom to increase it to 5000 lines:
1 |
HISTSIZE=5000 |
See:
1 2 3 4 |
1140 Jun 10 14:41:58 cd 1141 Jun 10 14:41:58 vim .bashrc 1142 Jun 10 14:41:58 exit 1143 Jun 10 14:42:00 history |
Let me know how it works out for you!
Dude! You’ve got some seriously helpful information going on here. I’ve already bookmarked your site, and now I’m starting a reference notebook with the posts that I’ve printed off. Thanks for saving me a bunch of time & headache.
I think this will be very handy when I need it. I wish I would known about this sooner, it would have saved me a lot of time. Thanks for the detailed guide as always, much appreciated.
Wow, that would have come in handy more times that I would like to admit! I would never have thought to check into adding time-stamps like that. You’ve inspired me to dig further into learning the in’s and out’s of bash.
This is another great tip. Sometimes I’ll have a really important command that I haven’t used in a week or two, and it just disappears into the history. This is a great way to keep older commands without having to make a text file to contain all of them. Also I believe for certain command line interpreters there’s a GUI option to set the BASH history.