Ubuntu – How to delete bash_history for current day only

14.04bashcommand line

I'm working on a client project that is hosted on digital ocean on an ubuntu server, I'm working on it with a few other developers and I'm kind of a new and made a bunch of commands and it's really messy. How can I remove command history up to a certain point?

For example, I'd like to remove all the commands that I typed in today, and leave all of the previous commands before that, is this possible?

It's easier for other developers to find previous commands without going through all of mine. Is it possible to clear bash_history up to a certain point, for example only todays history? If so, how?

Please advise!

Thanks!

Best Answer

Lines in Bash history are saved as plain lines of text without any meta-information like an invocation timestamp. So it is not possible to automatically erase all the command history from a single day, because you simply can not tell any more which command was run when.


If you simply want to remove the entire command history of your current Bash session (i.e. everything that is still only in memory and not written to the disk yet), you can either clear the in-memory history buffer or replace it with the old history file's contents from the disk.

  • Clearing the history buffer will result in an empty in-memory history for your current session, meaning that no commands will show up if you press , etc. Once you exit your Bash session, all new commands that were entered after the clearing will be appended to the history file and in your next session, you will see the complete history again, with only the range between start of your last session and the clear command missing.

    history -c
    
  • Replacing the current in-memory history buffer with the history file's content from the disk will have the same final result as clearing the history buffer, except that in your current session you will have access to the old command history instead of an empty list.

    history -r
    

If that is not helpful for you because you want to tidy up entries from previous sessions or only a partial session, the surgical method is what you need:

Synchronize your current in-memory history buffer to the history file on the disk and then open that file in a text editor and remove all the offending entries manually.

To append the history from your current session to the history file manually (because it only happens automatically when you exit your session), run this first:

history -a

After that, open the Bash history file ~/.bash_history in your favourite text editor (as you're on a server, I'd suggest e.g. nano) and edit the command list to your likes. The file lists all recorded commands in chronological order, newest commands last. Simply remove the offending lines and save your work.

After that, you should reload the modified history into your in-memory history buffer using the command below, in order to prevent old entries from memory to be appended to the tidied history file when you exit your session:

history -r