Announcement

Collapse
No announcement yet.

Clearing Bash History

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Clearing Bash History

    This article explains how to clear the bash history file used by most terminal apps.

    http://www.techrepublic.com/article/...-bash-history/

    This has long been a PITA for me when I get into a long terminal session.

    This solved the problem.

    [edit] please note there are TWO possible routes for this, depending on whether you have one or more terminal windows open...
    Last edited by TWPonKubuntu; Jun 06, 2016, 08:12 PM. Reason: added info
    Kubuntu 24.11 64bit under Kernel 6.11.7, Hp Pavilion, 6MB ram. Stay away from all things Google...

    #2
    One can unset the HISTFILE environment variable or set it to HISTFILE=/dev/null (you can do this in .bashrc, for example) to prevent history from being saved to disk at all. The history should still be kept in memory for the duration of the shell session, just not not saved to disk after the shell closes. (IMO this is a bit cleaner than using a daily cron job to clear history).

    https://www.gnu.org/software/bash/ma...acilities.html
    Last edited by kubicle; Jun 06, 2016, 10:05 PM.

    Comment


      #3
      kubicle, That would work, although I do like to keep a couple of commands in the history.

      I suppose I could retype them after I have done some heavy terminal sessions that fill up the history, but I'd rather not have to retype them.

      I do a daily "apt-get update" and "apt-get dist-upgrade" and it is handy to have those bash commands in the history.

      At least with the ability to easily clear history, I now only need to retype commands perhaps once a week.
      Kubuntu 24.11 64bit under Kernel 6.11.7, Hp Pavilion, 6MB ram. Stay away from all things Google...

      Comment


        #4
        Start using aliases for your most used terminal operations. It's what I do.
        Windows no longer obstructs my view.
        Using Kubuntu Linux since March 23, 2007.
        "It is a capital mistake to theorize before one has data." - Sherlock Holmes

        Comment


          #5
          By contrast, I increase $HISTSIZE and make extensive use of the history (and aliases and script files).

          It's such a frustration on Windoze to open a command prompt and try to recall previous commands and get nothing.
          I'd rather be locked out than locked in.

          Comment


            #6
            Thanks for suggestions on working with bash history, I'll need to play with this for while...
            Kubuntu 24.11 64bit under Kernel 6.11.7, Hp Pavilion, 6MB ram. Stay away from all things Google...

            Comment


              #7
              Originally posted by SecretCode View Post
              By contrast, I increase $HISTSIZE and make extensive use of the history...
              So do I... I use a different $HISTFILE with each konsole profile with different colours. The set up evolved gradually and these days it's five different history files, 400 items each.

              Regards, John Little
              Regards, John Little

              Comment


                #8
                John, how do you use 400 entries?

                Is there an easy way (gui?) to view and them?

                Do you have them in some sort of order or grouping?
                Kubuntu 24.11 64bit under Kernel 6.11.7, Hp Pavilion, 6MB ram. Stay away from all things Google...

                Comment


                  #9
                  As SecretCode said, set HISTFILE, typically in your .bashrc:
                  [code ]HISTFILE=400[/code ]
                  Note that bash instances will use the same file by default, and the last to exit overwrites the other's (unless it's been changed, it's been much requested and zsh does something clever.)

                  Regards, John Little
                  Regards, John Little

                  Comment


                    #10
                    Originally posted by TWPonKubuntu View Post
                    Is there an easy way (gui?) to view and them?

                    Do you have them in some sort of order or grouping?
                    No GUI, but I use a program to list the recent history in two columns, older on the right. (on Tapatalk here, or I'd do a screenshot)
                    The history files are organized by the text colour of the corresponding konsole profile.

                    Regards, John Little
                    Regards, John Little

                    Comment


                      #11
                      Which program do you use for two column display?
                      Kubuntu 24.11 64bit under Kernel 6.11.7, Hp Pavilion, 6MB ram. Stay away from all things Google...

                      Comment


                        #12
                        Originally posted by SecretCode View Post
                        By contrast, I increase $HISTSIZE and make extensive use of the history (and aliases and script files). ...
                        I make bash history unlimited. It's part of my "memory"
                        "A nation that is afraid to let its people judge the truth and falsehood in an open market is a nation that is afraid of its people.”
                        – John F. Kennedy, February 26, 1962.

                        Comment


                          #13
                          Originally posted by TWPonKubuntu View Post
                          Which program do you use for two column display?
                          Originally I used an alias like
                          Code:
                          alias h='history $(((LINES-1)*2)) | pr -2 -t -l $((LINES-1)) -w $COLUMNS'
                          but that put the older column on the left, would mess up if I'd used a multi-line command, or had various control characters, and would waste 6 columns. So I wrote a C programme to get the output I wanted, long ago rewritten in C++. Slightly blurry screenshot:
                          Click image for larger version

Name:	Screenshot_20160608_120537.jpg
Views:	1
Size:	80.3 KB
ID:	643233
                          The history line number colour matches the coloured bash prompt, which varies by konsole profile.
                          Originally posted by GreyGeek
                          I make bash history unlimited. It's part of my "memory"
                          I don't do that because the history numbers become quite long, taking up more space on screen, and I use alias r='fc -s' to rerun commands from the history, and long history numbers are a nuisance. Of course, what I should do is move to zsh, but I've never got around to it.
                          Regards, John Little

                          Comment


                            #14
                            Originally posted by TWPonKubuntu View Post
                            John, how do you use 400 entries?

                            Is there an easy way (gui?) to view and them?

                            Do you have them in some sort of order or grouping?
                            gui, not that I know of

                            history displays them, and by piping to grep you can filter out likely candidates.

                            Built in to bash is 'reverse-i-search' - press ctrl-r and start typing - the most recent history item that matches (anywhere) will be displayed. Handy.

                            I add some non-default entries in ~/.inputrc for 'history-search-backward; and 'history-search-forward' - this means you type the first few characters and then you can press the up or down arrow keys and cycle through every command in your history that begins with those characters. Very handy!
                            Code:
                            "\e[A": history-search-backward
                            "\e[B": history-search-forward
                            "\e[1;5A": history-search-backward
                            "\e[1;5B": history-search-forward
                            "\e[5~": history-search-backward
                            "\e[6~": history-search-forward
                            Left as an exercise for the reader to work out which keys those are.
                            I'd rather be locked out than locked in.

                            Comment


                              #15
                              Originally posted by jlittle View Post
                              Note that bash instances will use the same file by default, and the last to exit overwrites the other's (unless it's been changed, it's been much requested and zsh does something clever.)
                              There is a way to get this to work, and I have it working, I just don't know which bit of my .bashrc (which has migrated through many *buntu installations) makes it work.

                              Some net research suggests:
                              Code:
                              export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"
                              I'd rather be locked out than locked in.

                              Comment

                              Working...
                              X