How to delete a single command from history on a Linux/Unix Bash shell

I‘m working in Ubuntu bash terminal application and remotely on a RHEL server in cloud platform. I typed the wrong and dangerous command which I no longer wish to remember. How do I get rid of dangerous command in the history file? How can I remove or delete a single command from bash history file?

You can use the history command to clear all history or selected command line. In this tutorial, you will learn how to clear a specific command from bash history in Linux, MacOS, and Unix-like systems.
Tutorial requirements
Operating system/appLinux/Unix/macOS with bash
Root privileges requiredNo
DifficultyEasy (rss)
Estimated completion time1m
Table of contents

ADVERTISEMENTS

How do I view history with line number?

Simply type the history command:
$ history
Sample outputs:

Fig.01: Bash history command with line number on a Linux, OS X, and Unix

Fig.01: Bash history command with line number on a Linux, OS X, and Unix

By default, history is stored in ~/.bash_history file. However, the bash variable named $HISTFILE defines the file’s name in which command history is saved. If unset, the command history is not saved when a shell exits. Use the printf command/echo command to display the current settings:

echo "$HISTFILE"
printf "Bash history file - %s\n" $HISTFILE

How to delete a single command number 1013 from history in Linux

## Delete the bash history entry at offset OFFSET ##
history -d offset
history -d number
history -d 1013

Verify it:
$ history

Linux bash shell delete a single command from history file

Remove the bash history entry at position OFFSET. Negative offsets count back from the end of the history list.

How do I delete all the history?

Want to remove all bash history? Try the following syntax:
$ history -c
Add above command to your ~/.bash_logout file to clean when you logout:
$ cat /dev/null > ~/.bash_history && history -c
The -c passed to the history command to clear the history list by deleting all of the entries ${HISTFILE}.

Tip: Control bash history like a pro

First, you can increase your bash history size by appending the following config option in ~/.bashrc file:

## Set the  maximum  number of lines contained in the history file ##
HISTFILESIZE=5000000
 
## Set the number of commands to remember in the command history ##
HISTSIZE=10000
 
## Append it ##
shopt -s histappend
 
######
# Controlling how commands are saved on the history file ##
# ignoreboth means:                       ##
# a) Command which begin with a space character are not saved in the history list               ##
# b) Command matching the previous history entry  to  not  be  saved (avoid duplicate commands) ##
######
HISTCONTROL=ignoreboth

Save and close the file.

Where to find more information about history command?

Read bash man page by typing the following command:
$ man bash
Another option is to type the following command:
$ help history
Sample outputs:

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.

Conclusion

In this quick tutorial, you learned to remove a single command from bash history running on a Linux, macOS, *BSD, or Unix-like systems. I would suggest reading the bash man page for more information here.

This entry is 2 of 5 in the Bash HISTORY Tutorial series. Keep reading the rest of the series:
  1. How to disable bash shell history in Linux
  2. How to delete a single command from history on Linux/Unix Bash shell
  3. Clear Shell History In Ubuntu Linux
  4. Clear Linux / UNIX BASH Shell Command Line Cache / History
  5. Bash History: Display Date And Time For Each Command
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
7 comments… add one
  • Jared Jan 4, 2016 @ 2:08

    Occasionally I will mistakenly paste a bunch of garbage on the command line, such as as some text that had been selected earlier. Sometimes this happens when copy/paste is not working correctly, or maybe I just made a mistake.

    As I don’t want that junk in the command history, I will do the following:

    history -w myhist
    Edit myhist to remove unwanted lines
    history -r myhist

    Another use of this sequence of commands is to prime the history from a particular history file for a particular project. This saves a fair amount of typing at times.

  • Jared Jan 4, 2016 @ 2:09

    I forgot to mention for those that may not be familiar with it, this is in the bash shell.

  • Jalal Hajigholamali Jan 4, 2016 @ 7:23

    Hi,
    Thanks a lot…
    we can change history file format and add time of executed commands by HISTTIMEFORMAT variable….

    example:
    HISTTIMEFORMAT=”%D %T”

  • Per Jan 7, 2016 @ 13:02

    I wonder. How do you intercept dangerous parameters on certain commands from ever being run. For instance could you block “rm -rf” ?

  • Martin Jan 10, 2016 @ 6:33

    You may wan to try https://github.com/dvorka/hstr which brings easy history management (apart to suggest-box style navigation).

  • gman Mar 18, 2017 @ 5:10

    This doesn’t work on macOS 10.12.x

    $ history -d 1
    fc: event not found: -d
    

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.