Add timestamp + change history size in your linux history

Rob

Administrator
Staff member
Joined
Oct 27, 2011
Messages
1,263
Reaction score
2,430
Credits
3,941
The Linux history command is awesome by default, but you can make it better! By default, it'll only hold about 1000 past commands, but you can increase that - you can also add time/date to it so you can see when you ran each command!

In your ~/.bashrc file, simply add some lines like:
Code:
HISTTIMEFORMAT="%F %T "
HISTFILESIZE=5000

Then, you'll have a 5000 line history file along with timestamps! Give it a go and let me know how it goes!

After you log out and back in, you'll see something like:
Code:
 1991  2020-03-06 20:00:23 cat word_list.txt |wc
1992  2020-03-06 20:00:23 head word_list.txt
1993  2020-03-06 20:00:23 tail word_list.txt
1994  2020-03-06 20:00:23 ls
1995  2020-03-06 20:00:23 cd ..
1996  2020-03-06 20:00:23 ls
1997  2020-03-06 20:00:23 cat scripts/push-changes.sh
1998  2020-03-06 20:00:23 exit
1999  2020-03-06 20:00:23 exit
2000  2020-03-06 20:00:27 history
2001  2020-03-06 20:01:08 ls
2002  2020-03-06 20:01:10 history

Everything before time/date will show the same time, but after that, it'll show you the exact times of command run.

Recently, Ubuntu and some other distros have been changing history to 2000 lines, so if you see that in your .bashrc, either comment it out, or just directly change that to 5000.
 
Last edited:


it may be worth noting for the viewers, rob, too, that if they spend a bit of time as root, they can likewise apply the above to the .bash_history that applies to root's activities.

[chris@localhost ~]$ sudo su
[sudo] password for chris:
[root@localhost chris]# history
1 2020-03-07 12:11:43 history
2 2020-03-07 12:12:40 dnf upgrade
3 2020-03-07 12:48:05 exit
4 2020-03-07 12:50:07 history
5 2020-03-07 12:55:27 clear
6 2020-03-07 12:55:33 exit
7 2020-03-07 12:56:02 history
[root@localhost chris]# echo Thanks Rob for sharing, and for helping us all to enjoy this site and its community!
Thanks Rob for sharing, and for helping us all to enjoy this site and its community!
[root@localhost chris]#

avagudweegend

wizard
 
Last edited:
Rob your post is really good, in production environment I would like to backup all user's history with time stamp from root. Till now i backup ~.bash_history file in user's home directory..Please guide me if is it possible in linux..
 
Last edited:
it may be worth noting for the viewers, rob, too, that if they spend a bit of time as root, they can likewise apply the above to the .bash_history that applies to root's activities.

[chris@localhost ~]$ sudo su
[sudo] password for chris:
[root@localhost chris]# history
1 2020-03-07 12:11:43 history
2 2020-03-07 12:12:40 dnf upgrade
3 2020-03-07 12:48:05 exit
4 2020-03-07 12:50:07 history
5 2020-03-07 12:55:27 clear
6 2020-03-07 12:55:33 exit
7 2020-03-07 12:56:02 history
[root@localhost chris]# echo Thanks Rob for sharing, and for helping us all to enjoy this site and its community!
Thanks Rob for sharing, and for helping us all to enjoy this site and its community!
[root@localhost chris]# history
1 2020-03-07 12:11:43 history
2 2020-03-07 12:12:40 dnf upgrade
3 2020-03-07 12:48:05 exit
4 2020-03-07 12:50:07 history
5 2020-03-07 12:55:27 clear
6 2020-03-07 12:55:33 exit
7 2020-03-07 12:56:02 history
8 2020-03-07 12:57:05 echo Thanks Rob for sharing, and for helping us all to enjoy this site and its community!
9 2020-03-07 12:57:21 history
[root@localhost chris]#

avagudweegend

wizard
you mentioned a useful point thanks wizardfromoz..
 

Members online


Top