Skip to main content
Topic: Short handy trick for .bash_history file (Read 661 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Short handy trick for .bash_history file

 
 Hi all. Found a somewhat simple but cool trick in getting things more organized when talking about .bash_history file.

So in order to have not only my most used commands in general but also in a specific order those most used of them all at the bottom of .bash_history that of course will pop up first in commands suggestions in terminal and other less important commands at the top of .bash_history file so those will be suggested last.

So to have this sorted out just found that making  .bash_history file read only and adding manually my desired more useful commands or just by changing this file permission from read only to read write so latest commands can be added up to the history can save some huge time. When .bash_history file starts getting huge and cluttered with duplicates or commands that aren't that good at what we want to achieve after we found the better ones, this my new little trick can make our work flow much more pleasant.

This trick keeps .bash_history file at a minimum size so commands can be much more easily found and eventually save us some of our most precious 'metal', time.

Of course this trick can be implemented thru a small script but I prefer doing it old school fashion 😃.

When find new useful command I just add it to the text file either at the bottom or at the top depending on its importance.

Last but not least another file would look way out better if we make it read only .bashrc. I mean is better to have it read only as nobody would be happy if some exploit or hack would change aliases from their .bashrc file for example instead of ping  or idk top or htop we would have rm /home/john/Documents/*

As example this is how my .bash_history looks like currently.



Well what do you think guys? 😃  Sure this is for the not so advanced users but still I guess those more experienced can implement this as they wish. I'm propping just the principle not more than that.


Re: Short handy trick for .bash_history file

Reply #1
It's an interesting idea, and like most things, well if that's how you like to do things then why not? But I wouldn't want to. Bash history can be useful to record commands that didn't work, so  you can fix what went wrong later when you wonder what you did. Also if I need to find a command in there I mostly just do
Code: [Select]
 $ ag "some part of the command I remember" ~/.bash_history
then copy paste the right one, which is another possible non-standard solution  :D  (ag is now deprecated by rg but I only read that recently and I haven't tried rg yet  ;D )

Re: Short handy trick for .bash_history file

Reply #2
 Yeah that's also nice ✅ solution but have you checked how big your history file is? As for me if I'm not doing this history trick I would
 end up having a 1GB history file  😵

There's one thing when you think you might have your command in there while knowing exactly is there cos it's important saves a lot of time. And also had a million versions of the same command so I didn't know exactly which one is the good one.

Made the same for the root history file so in my history I kinda know what's in there. But for the root history file there's more complicated as when you work with the root also the memory is modified as root so me as user can't trick by just closing that instance. I have to somehow delete what's being stored by the root in memory and I don't even think it's possible without a reboot. So that I've fixed it thru a cronjob that reads from a file and overwrites hourly what should be in my root history. When I wanna add something I just modify that one from where it reads. But even that way if I don't close that root instance before cronjob runs I'll still having duplicate entries but those eventually vanish after a reboot


Re: Short handy trick for .bash_history file

Reply #3
You could probably use aliases in .bashrc for frequently used commands, or scripts in /usr/local/bin as an alternative. Many of the problems you describe can be resolved if you look at the man page for "bash", then (assuming you are using less as the man pager) press / then type history as the search term and enter to search, press n for next one. It has all manner of built in features to control and search the history,  using ag is a kludge because it's easier than memorizing all the bash history key combos! You can set things in your ~/.bashrc like HISTFILESIZE=100000 to allow a big history file, but stop it growing too big. Don't set the other HISTSIZE variable though, otherwise instead of the file being truncated to the right length, it is deleted when full and a new empty one started. There are some defaults if nothing is set so it should be limited even in standard trim.
These commands turn history on and off temporarily in the shell they are issued in, perhaps helpful for the situation you describe for your root shell, or if you are saving history but need to enter some secret codes etc.:
Code: [Select]
Turn Off
set +o history
Turn on
set -o history