3

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!

6

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
| improve this answer | |
  • Hey, @bytecommander - this is just what I was looking for. To be clear though you mention "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". When you say "you will see the complete history again" you're referring to the complete history BEFORE the cleared session right? For example, I open ubuntu and run git add ., git commit x etc.. then I run history -a and shut down, when i reload i won't see git add.. and git commit x right? only the commands before? – Anthony Pinto Apr 10 '17 at 21:14
  • Yes, the sentence goes on: "... you will see the complete history again, with only the range between start of your last session and the clear command missing." - the "complete history" you will see in the next session is everything before your current session plus everything from your current session, after the history -c or history -r command. If you run history -a (-a = append), you will write your current session to the history file (appending, not overwriting). – Byte Commander Apr 11 '17 at 7:42
  • Also, in case your question is completely solved now, don't forget to accept the answer that helped you most by clicking the grey check button on the left of it. – Byte Commander Apr 11 '17 at 7:43
2

In case it helps:
You can add timestamp to ~/.bash_history
It's set in the environment variable called HISTTIMEFORMAT . This variable is of the date type (see man date to how to format it).
You can permanently add it at user level by adding it at the end of .bashrc
Example:

$ echo 'HISTTIMEFORMAT="%F %T "' >> ~/.bashrc

(here it put it like YYYY-MM-DD hh:mm:ss ).

The output will now like like: #history

You can too add it to all users history but I've not try it. Apparently you do this by adding the same at the end of /etc/profile or equivalent (files in /etc/profile.d/ ).

I found all the above information by googling and finding this here: https://www.2daygeek.com/display-date-time-linux-bash-history-command/ (but I'm no link of any type with this website neither do I know its author)

| improve this answer | |
0

You can remove lines of the history with

 history -d NNN

where nnn is the 3 digit line number. have a look also to man history You can delete the history of the current shell session with:

 history -c 
| improve this answer | |
  • Thanks, so if I delete the current shell session with history -c, next time I or someone logs onto that server they won't see the history that I deleted, correct? – Anthony Pinto Apr 10 '17 at 19:07
  • Yes. this is true – 0x0C4 Apr 11 '17 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.