In my opinion, one of the most important things about Linux is history. Thanks to it, we know what has been done in the system and we can quickly check what commands were executed in the system. When working on different systems it is very useful to use grep and use (ctrl + r) in the shell to quickly search the command history. However, to make it even more useful, we will try to enlarge the history to 10000 by default, it is 1000 command lines. We will also add a date that will be next to the command issued. If we want to run the history command, just:
1 |
$ history |
To enlarge the history and add a date when executing a given command, you should:
1 2 3 4 |
$ cd $ echo 'HISTFILESIZE=5000000' >> ~/.bash_profile $ echo 'HISTSIZE=10000' >> ~/.bash_profile $ echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bash_profile |
The whole thing can be reduced to one command.
1 |
$ cd && echo 'HISTFILESIZE=5000000' >> ~/.bash_profile && echo 'HISTSIZE=10000' >> ~/.bash_profile && echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bash_profile |
After re-logging, it looks like this:
Finally, I would like to introduce a few more tricks that I mentioned, e.g. greping history:
1 |
$ history | grep cp |
Of course, you can use any other command instead of ‘cp’.
Mentioned useful way to search history is to use (ctrl + r). After pressing this combination, we can start writing any command. History will be searched. If we hold ctrl again and press kolena once ‘r‘ we will jump to the command above from the bottom. In my case, as you can see, this is the second command from the bottom, that is:
If you are interested in where the history is saved and in what form, you can view it or delete it from the file:
1 |
$ vi ~/.bash_history |
If we accidentally use the wrong command on the system, it makes sense to remove it.