When we execute a command in the Linux console, it is saved in the history. Sometimes, for various reasons, it may be of interest to delete the command history.
For example, it may be that some command requires a password as a parameter and we do not want anyone to see it. Or maybe we just deleted the entire database of our company "accidentally" and we do not want the boss to find out ...
The history saves the last 500 commands that have been executed in the Terminal, and it is very useful to have this list if we want to access a specific command. With the history command you can see the list of commands, they can be modified, deleted, etc.
Linux History command
On more than one occasion we have wanted to delete the history of the shell script. The bash commands that we have executed on our computer in linux.
How to Delete Bash History
Delete the .bash_history file located in our home is an option, but it is always better to use the tools or options that the system itself offers .
We will learn two ways to do it. Through bash commands and shell script.
History -c: So simple! A simple parameter (-c) tells the system to delete the command history.
- Another method that should work is also:
Echo ""> ~ / .bash_history
This should put a blank line in our history deleting everything else.
See the full history
To see a complete list with the last 500 commands, we must run in the Terminal:
Clear ALL history
If we need to delete all the command history, we execute:
Delete the X command
If we want to eliminate a specific command, we must keep its number (we visualize the list with the history command and we keep the number that appears on its left). Then we write:
history -d X
where "X" is the number of the command that we want to delete.
See the last X commands
Sometimes we need to see only the last ones, for example, 5 commands executed. To do this, we write the following command (substituting the five for the number of commands we want, logically):
Keep in mind that the command that we write is also included in the list of the last commands.
Execute the X command from the command list
If we want to execute a command from the list again, we visualize the whole list of commands and we keep the number that appears on the left. Then, assuming we want to execute the 1044 command again, we write
Easy and quick. Hope it helps!