How to delete a single command from history on a Linux, OS X and Unix Bash shell

Posted on in Categories , , , , , , last updated February 7, 2017

I‘m working in Ubuntu bash terminal application and remotely on a RHEL server in cloud platform. I typed the wrong and dangerous command. I no longer wish to remember 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.

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

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

The syntax is:

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

Verify it:
$ history

How do I delete all the history?

The syntax is:

history -c

OR (add to your ~/.bash_logout file to clean when you logout):

cat /dev/null > ~/.bash_history && history -c

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 ##
## Set the number of commands to remember in the command history ##
## 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) ##

Save and close the file.

Where to find more information about history command?

You can read bash man page by typing the following command:
$ bash(1)
Or simply 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.
      -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
      -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.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

Share this on (or read 7 comments/add one below):

7 comment

  1. 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.

Leave a Comment