This post will guide you how to delete a single line from bash shell history in Linux system. How to delete/clear a certain line or command from bash history file under Linux system. How to delete history command of last 10 commands in bash shell.
Linux Delete a Single Line from History
If you want to check the history with line number in bash shell, you just need to execute the history command, type:
#history
Outputs:
70 hwinfo --bios 71 man hwinfo 72 ls /etc/xxxx 73 ls /etc/xxxx 2>fio.txt 74 cat fio.txt 75 find -name *.txt ./ 76 find ./ -name *.txt 77 find /etc -name *.conf 78 find /etc -name a*.conf 79 ls 80 cd tmp 81 ls 82 find /tmp -name a*.conf 83 find / -name a*.conf 84 cd 85 find / -name a*.conf &> fio.txt 86 cat fio.txt 87 history
From the above output, you will see that the line number and the commands have been displayed.
Delete a Specified Command from History
If you only want to delete or remove a specified command from the bash shell history, you need to execute the history command with the –d option, and then pass to the line number of command that you want to delete.
Type the following command:
#history –d 86
Delete All History Command or Line
If you want to delete or remove all history command from bash shell, you just run the history command with –c option, type:
# history –c
Outputs:
root@ubuntu-dev:~# history -c root@ubuntu-dev:~# history 1 history
Get more information about History command
If you want to get more information about history command, you can check it man page of history command or just use help command to get the usage of history command.
Type the following command:
#man history
Or
#help history
Outputs:
root@ubuntu-dev:~# help history history: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...] Display or manipulate the history list. Display the history list with line numbers, prefixing each modified entry with a `*'. An argument of N lists only the last N entries. Options: -c clear the history list by deleting all of the entries -d offset delete the history entry at offset OFFSET. -a append history lines from this session to the history file -n read all history lines not already read from the history file -r read the history file and append the contents to the history list -w write the current history to the history file and append them to the history list -p perform history expansion on each ARG and display the result without storing it in the history list -s append the ARGs to the history list as a single entry If FILENAME is given, it is used as the history file. Otherwise, if $HISTFILE has a value, that is used, else ~/.bash_history. If the $HISTTIMEFORMAT variable is set and not null, its value is used as a format string for strftime(3) to print the time stamp associated with each displayed history entry. No time stamps are printed otherwise. Exit Status: Returns success unless an invalid option is given or an error occurs.
Delete History of Last 10 Commands
If you want to delete last 10 commands from bash shell history, you can use the following simple shell command:
#for h in $(seq $(history | tail -1 | awk '{print $1-N}') $(history | tail -1 | awk '{print $1}') | tac); do history -d $h; done; history -d $(history | tail -1 | awk '{print $1}')
You just need to update the value of N to 10. And it will delete the last 10 commands from history.
Or you can run the following command to get your current history to get the line number of last 10 commands:
#history | tail –n 10
The outputs like below:
[root@devops ~]# history | tail -n 10 171 rpm -qi wget 172 rpm -qip wget-1.14-10.el7.x86_64.rpm 173 rpm -qdf wget 174 rpm -Vp wget-1.14-10.el7.x86_64.rpm 175 rpm -e wget 176 rpm -Vp wget-1.14-10.el7.x86_64.rpm 177 ls /etc/pki/rpm-gpg/ 178 rpm -qa gpg-pubkey* 179 history 180 history | tail -n 10
So you should know the start and end positions for the last 10 commands that you want to delete. It’s 171 to 180. Then executing the second command to delete last 10 commands:
# for i in $(seq 171 180 | tac); do history -d $i; done
This command will generate history –d commands for 180, then 179, then 171.