The GNU history library keeps track of all the commands that run on the system in the ~/.bash_history file by default. You can use this history to see and recall the past commands.
In case you have security concerns, you can also clear this history.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Linux GNU system queries.
In this context, we shall look into how you can view, manipulate and delete the terminal history in ubuntu.
1. View all past commands in the History
You can display the previous history through the following command:
$ history
2. View last 'n' number of commands in the History
You can also narrow down the history display to a specific number of lines. For example, to display the most recent 5 commands, run the following command:
$ history 5
1. Delete All Previous Commands in History
Run the following command to delete the current terminal history:
$ history -c
You will see that the above command will only delete the current terminal history. It will not affect the previous history that is stored in the history file.
To overwrite the whole history file, you need to run the following command after running the "history -c" command:
$ history -w
Here, you will notice that the previous history will be deleted and overwritten in the output.
2. Delete Single Command in History
You can delete specific lines in the history with the following command:
$ history -d <<line number>>
Note: Insert the line number of the command you want to delete in the above command. You can see the line number displayed with the command while viewing the history.
3. Delete History by Clearing ~/.bash_history File
You can clear the entire terminal history with the following command:
$ ln -sf /dev/null ~/.bash_history && history -c
This command will link the /dev/null file to the history file and delete the records.
4. Delete History by Disabling the Terminal/Bash History Logging
To turn off the logging of the terminal command, run the following commands:
$ sudo sh -c "echo "unset HISTFILE" >> /etc/profile"
Note: you will need sudo/root privilege to execute the above command.
5. Delete History by Editing the .bashrc File
You can change HISTSIZE and HISTFILESIZE variables in the .bashrc file to stop the history logging. To do that open a .bashrc file with the following command:
$ nano .bashrc
Now go to the lines mentioning HISTSIZE and HISTFILESIZE and change their value to zero. Press Ctrl+O and Ctrl+X to save changes and exit.
This article covers how to view / preserve or delete terminal history on Ubuntu Linux System. Terminal history can be a benefit in case you want to revisit/recall the past commands. But it can be a security risk because they can show system tools, configurations, and settings.
The history mechanism in Ubuntu keeps track of terminal commands you executed.
To view your command line history, Open the Ubuntu terminal and Type :
$ history
This will display the list of previous bash commands you executed in the terminal.
To delete your Ubuntu terminal history, Type:
$ history -c
This will clear your Ubuntu terminal history. Also, you need to know that command line history is user independent, means every user has their own separate history. When you execute history -c command, it will only clear terminal history of the current user.