Command Line

Let’s Delete An Entry In Your Bash History

In today’s article, we’re going to learn how to delete an entry in your ‘bash’ history. It’s a useful skill to have, for any number of reasons. It’s not all that difficult and this shouldn’t be a very long article. Read on, my dear readers! Even a long-term Linux user might learn something – but I make no promises!

I’ve previously covered how to remove duplicates from your bash history. It may be worth checking that article out, as most of you are going to be using bash. Sure, there are other shells, but bash is the most common on desktops and servers. So, we might as well learn with bash.

If you don’t know, bash is both a language and an application. Unless otherwise specified, you’re almost certainly using bash when you open a terminal or TTY. 

When you enter a command in the terminal or TTY, it’s saved to your bash history. In fact, it’s saved to the hidden file ~/.bash_history. If you so wanted, you could just open the .bash_history file with a text editor and remove lines as you wished. That works just fine.

However, in this article we’ll be using the ‘history’ command. It’s a handy command, useful for recalling previously entered commands and managing those stored commands. I guess this is really more an article about performing some very basic tasks with the history command. Like I said, it’s good for you to know this sort of stuff – especially as a Linux beginner.

Delete An Entry In Your Bash History:

This article requires an open terminal, like many other articles on this site. If you don’t know how to open the terminal, you can do so with your keyboard – just press CTRL + ALT + T and your default terminal should open right up and be ready for use!

So, with your handy-dandy freshly-cracked-open terminal, let’s just display your bash history. You’re going to do that with just the following command:

history

You’ll notice that every stored command from your bash history is assigned a number that’s shown on the left (or right, if you use a RTL language). Well, that’s how you delete it. You use that number in the following command:

history -d <command_number>

You can use that command to delete an entry in your bash history, and you can do so as often as you’d like. While you’re there, you can also just plain clear your entire bash history with this command:

history -c

That will clear all of your bash history – more or less. Your current session may have not been written to the history at the time you issued that command. It might not be saved until you close the terminal instance. This makes it slightly more complicated to make sure the history is truly empty. However, it’s definitely close enough.

Anyhow, you might want to delete your bash history to remove commands that you’ve memorized. You might want to remove commands that didn’t work. If there are sensitive commands in your bash history, this is a way to remove them, surgically or en masse. There are all sorts of reasons why you might want to go through your bash history to delete past commands – and now you can!

Closure:

Yay! It’s another back-to-basics kind of article. In this one, we learn how to delete an entry in your bash history file. In some cases, it may be easier for you to just do so with a GUI and a GUI text editor. You don’t even need ‘sudo’ to make the changes and removing a lot of entries might be faster with a graphical application. Just remove the lines you don’t want showing up in the history any longer.

Also, I should mention somewhere that you can see the history by just pressing the up arrow, but I suspect folks will already know that. ‘Snot the best way to go about managing them, but you can see and select them, running them again as you wish.

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

KGIII

Retired mathematician, residing in the mountains of Maine. I may be old and wise, but I am not infallible. Please point out any errors. And, as always, thanks again for reading.

View Comments

  • Good management article.

    Depending on your ... dependence ... on the history, it's all temporary. Anything complicated you tend to repeat regularly should probably be moved to an alias or function.

    • I really need to do an 'alias' article. It just seems so cumbersome with my format. If you wanna take a whack at it, be my guest!

  • thats sure if its just Arch but if i just type : history
    I only get a list of the last 16 commands used

    if i want the last 50 i type : history 50

    • I'll have to look into that. I've never seen the history command not do what it's expected to do.

      You may have to scroll up. In other words, type history and then use your mouse wheel to scroll up. It should then show your complete history.

Recent Posts

Update Python Packages (PIP)

We've had a run of Python packages recently and you can tell that I'm a…

2 days ago

Save A Command’s Output To A File (While Showing It In The Terminal)

The title is the best I can come up with to describe this exercise as…

4 days ago

Demystifying journalctl: A Comprehensive Guide to Linux System Logging

It was suggested that I write an article about journalctl, which seemed like a large…

6 days ago

Extract Text From Multiple File Types

Today we will have a fairly simple exercise as we're going to just use a…

1 week ago

Meta: I’ve Been At This For Three Years!

I have not done a meta article lately. I don't find them interesting to write…

1 week ago

How To: Disable CPU Cores

This is not something everyone is interested in doing but you might as well learn…

2 weeks ago