Delete the History of the Last n Commands on Linux


In Linux, the command history is a record of previously executed commands. The command history is stored in a file called . bash_history, which is located in the home directory of each user. The history command can be used to view the command history, and the history command can also be used with other commands, such as grep, to search for specific commands in the history. The commands in the history are assigned a number, which can be used to quickly execute a previous command by prefixing it with an exclamation mark (!). For example, typing "!123" will execute the command that has the number 123 in the history.

There are also several options to change the behavior of command history

  • The history command can be used with the -c option to clear the command history.

  • The HISTFILE environment variable can be used to specify a different file to store the command history.

  • The HISTSIZE and HISTFILESIZE environment variables can be used to specify the maximum number of commands stored in the history and the maximum size of the history file, respectively.

  • The HISTIGNORE variable can be used to ignore specific commands or pattern of commands from history

  • The set -o history enables command history for a shell that has it disabled by default.

Difference Between history and Commands in .bash_history

The history command is a built-in command in Bash (the default shell on most Linux systems) that allows you to view the commands that have been executed in the current shell session. When you run the history command, it will display a list of the commands that have been executed, with the most recent commands at the top of the list. Each command is assigned a number, which can be used to quickly execute a previous command by prefixing it with an exclamation mark (!). For example, typing "!123" will execute the command that has the number 123 in the history.

The .bash_history file, on the other hand, is a plain text file that stores the command history for a specific user. The file is located in the user's home directory and is updated every time a command is executed in the shell. The contents of the .bash_history file can be viewed using a text editor, and the commands can be executed by copying and pasting them into the shell.

Delete Last n Lines from .bash_history

You can delete the last n lines from the .bash_history file using the sed command, which is a command-line utility for editing text files. The basic syntax for deleting the last n lines from the .bash_history file is −

sed -i -e :a -e '$d;N;2,N;ba' -e 'P;D' ~/.bash_history

This command will remove the last line of the file. To remove the last n lines, you can use the following command −

sed -i -e :a -e "1,${n}d" ~/.bash_history

Here, n is the number of last lines you want to delete from the .bash_history file. This command will remove the last n lines from the .bash_history file.

Please keep in mind that this command will change the .bash_history file directly and the change will take effect immediately. Also, these changes are not reversible, so if you are unsure about the operation, it is a good practice to make a backup of the file before making any modifications.

It's also worth mentioning that, this will remove the commands only from the history file not from the current session history, so you still can use the command 'history' to see them, but they will not be saved in the future session.

Delete Last n Lines in history

To delete the last n lines in the current shell session's history, you can use the history command with the -d option, followed by the line numbers of the commands you want to delete.

For example, to delete the last 3 commands from the history, you can use the following command −

history -d $(($(history | tail -n1 | awk '{print $1}')-3)) $(($(history | tail -n1 | awk '{print $1}')-1))

This command will delete the 3 last commands from the current history, including the command 'history' used to get the last command number and awk used to extract it.

You can also delete a range of commands by specifying the starting and ending line numbers. For example, to delete commands 10 to 15, you can use the following command −

history -d 10 15

Please keep in mind that, this will remove the commands only from the current session history and will not affect the .bash_history file or any other session history. Also, these changes are not reversible, so if you are unsure about the operation, it is a good practice to make a backup of the history before making any modifications.

Changes in History Since Bash 5

The history feature in Bash version 5, which was released in January 2019, introduced several changes and improvements over previous versions. Some of the most notable changes include −

  • Timestamps − Bash 5 now includes timestamps in the command history, which allows you to see when each command was executed. This feature can be enabled by setting the HISTTIMEFORMAT variable to a value that specifies the timestamp format.

  • Appending to the history file − In previous versions of Bash, the history file was overwritten every time a new command was executed. With Bash 5, the history file is now appended to, which means that the history from previous sessions is not lost.

  • Histignore − A new variable, HISTIGNORE, allows you to ignore specific commands or patterns of commands from being added to the history file.

  • Largefile − Bash 5 now supports large history files, allowing the history file to grow beyond the 32,768 commands limit of previous versions.

  • Incremental search − Bash 5 includes an incremental search feature for the history, which allows you to search for commands by typing part of the command and pressing the up arrow.

HISTSIZE and HISTFILESIZE can now have a value of unlimited, meaning no limit for the number of commands stored in the history and the maximum size of the history file, respectively.

Overall, these new features make it easier to manage and search through the command history, and make it more convenient to work with long history files, which is specially helpful when you have to work with sessions that last long.

Conclusion

The history feature in Linux is a powerful tool that allows you to easily view and execute previous commands, as well as delete specific commands from the history if needed. The .bash_history file stores the command history for a specific user across multiple shell sessions, and it can be edited using the command line tools like sed. The history command allows you to view the commands that have been executed in the current shell session and delete the specific commands from the current session history.

Updated on: 24-Jan-2023

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements