Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Delete the History of the Last n Commands on Linux
In Linux, the command history is a record of previously executed commands stored in a file called .bash_history, located in each user's home directory. The history command displays this history, and commands are assigned sequential numbers that can be executed using !number syntax. For example, typing !123 will execute the command numbered 123 in the history.
There are several options to customize command history behavior:
The
history -ccommand clears the current session's command history.The
HISTFILEenvironment variable specifies a different file to store command history.The
HISTSIZEandHISTFILESIZEvariables control the maximum number of commands stored in memory and in the history file, respectively.The
HISTIGNOREvariable excludes specific commands or patterns from being recorded.The
set -o historycommand enables command history for shells that have it disabled.
Difference Between history Command and .bash_history File
The history command is a built-in Bash command that displays commands from the current shell session, showing the most recent commands first. Each command has a number that can be used for quick execution with the ! prefix.
The .bash_history file is a plain text file storing the persistent command history for a specific user. It's updated when the shell session ends and can be viewed with any text editor. Commands from this file are loaded into memory when a new shell session starts.
Delete Last n Commands from .bash_history File
To delete the last n commands from the .bash_history file, you can use the sed command. Here are the most effective methods:
Method 1: Using head and mv
# Delete last 5 commands from .bash_history head -n -5 ~/.bash_history > ~/.bash_history.tmp && mv ~/.bash_history.tmp ~/.bash_history
Method 2: Using sed
# Delete last n lines (replace 5 with desired number) sed -i '$d' ~/.bash_history # Delete last line sed -i -e :a -e '$d;N;2,5ba' -e 'P;D' ~/.bash_history # Delete last 5 lines
Method 3: Using tail and sponge
# Install moreutils first: sudo apt install moreutils head -n -5 ~/.bash_history | sponge ~/.bash_history
Warning: These commands modify the .bash_history file directly and changes are irreversible. Always backup the file before making modifications:
cp ~/.bash_history ~/.bash_history.backup
Delete Last n Commands from Current Session
To delete commands from the current shell session's history without affecting the .bash_history file:
Delete Specific Range
# Delete commands 100 to 105 history -d 100-105
Delete Last n Commands
# Delete last 3 commands
for i in {1..3}; do history -d -1; done
Alternative Method
# Get current history size and delete last 5 commands
LAST=$(history | tail -1 | awk '{print $1}')
history -d $((LAST-4))-$LAST
These changes only affect the current session and won't persist to the .bash_history file unless you run history -w.
Advanced History Management
Modern Bash versions (5.0+) provide enhanced history features:
Timestamps Enable with
HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "History appending Use
shopt -s histappendto append rather than overwriteReal-time updates Set
PROMPT_COMMAND="history -a"for immediate savesUnlimited history Set
HISTSIZE=-1andHISTFILESIZE=-1
Practical Examples
Remove Sensitive Commands
# Remove last command containing password
history -d $(history | grep "password" | tail -1 | awk '{print $1}')
Clean Up Before Logging Out
# Remove last 10 commands and save history
for i in {1..10}; do history -d -1; done
history -w
Conclusion
Managing command history in Linux provides control over what commands are stored and accessible. You can delete specific commands from both the current session using history -d and from the persistent .bash_history file using tools like sed or head. Always backup your history file before making bulk changes, as these operations are irreversible.
