Announcement

Collapse
No announcement yet.

Article - How to Clear Bash History on Linux

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

    Article - How to Clear Bash History on Linux

    Long, long ago, I asked this of the forum and today, I found this simple set of commands:

    https://www.howtoforge.com/clear-bash-history/

    The article is very short but I quote from it:

    To clear the Bash History on Linux, use this combination of 3 commands:

    Code:
    cat /dev/null > ~/.bash_history && history -c && exit
    What do these commands do?

    • The 'cat' command empties the .bash_history file of the currently logged in user.
    • The 'history -c' command cleans the history of the current user session.
    • The 'exit' command quits the current session.
    I normally have a few commands in the bash history that I use daily, my "workhorse" commands. However, occasionally I will do a lot of bash commands and my daily set is pushed way down the list and it become time consuming to step back through the history. So then I clear the bash history and re-enter my daily workhorse commands.

    Hopefully this will help you in a similar way.
    Kubuntu 24.11 64bit under Kernel 6.11.0, Hp Pavilion, 6MB ram. Stay away from all things Google...

    #2
    What , clear the history . heavens NO , thats how I remember what I was up to last night , "clear the history" , crazy talk!

    OK so amusement aside , nice post , that may be in order once in a wile

    VINNY
    i7 4core HT 8MB L3 2.9GHz
    16GB RAM
    Nvidia GTX 860M 4GB RAM 1152 cuda cores

    Comment


      #3
      Vinny: Two words: Postit Notes

      'course I have to be able to read my handwriting from 3am notes...
      Kubuntu 24.11 64bit under Kernel 6.11.0, Hp Pavilion, 6MB ram. Stay away from all things Google...

      Comment


        #4
        Originally posted by TWPonKubuntu View Post
        However, occasionally I will do a lot of bash commands and my daily set is pushed way down the list and it become time consuming to step back through the history.
        Just in case you aren't aware of the functionality, searching through bash history should be less time consuming than stepping back through it (if you have a long history)...and also less time consuming than clearing your history and retyping the commands (once you run the searched commands, they'll be in recent history again).
        https://www.digitalocean.com/communi...h-bash-history

        Comment


          #5
          kubicle, Yes, I do know how that works; the rotation of executed commands to the top of the history stack. Normally that works well, Except when I also do a session of lots of other bash commands. The history can get very long... say 50 or more commands during one or more sessions in Konsole/Terminal. It takes too long to manually scroll simply to find a couple of commands.

          When I'm done with a long terminal session, I really like to flush the history and start over with a minimal few commands in history. As I said, I do have a set of about 3-4 commands that I run daily. No always together or I'd put them in a script file.

          Hence the three bash commands in my original post, which could be put in a script file too...
          Kubuntu 24.11 64bit under Kernel 6.11.0, Hp Pavilion, 6MB ram. Stay away from all things Google...

          Comment


            #6
            Originally posted by TWPonKubuntu View Post
            The history can get very long... say 50 or more commands during one or more sessions in Konsole/Terminal. It takes too long to manually scroll simply to find a couple of command.
            I didn't mean manual scrolling through commands with the arrow keys...I meant searching the history using ctrl+r and type a few chars that are in your command to find the command you're looking for (subsequent ctrl+r will cycle through matches). It's very fast even if you have thousands of history entries.

            Comment


              #7
              IMO your command line history is potentially very valuable. For years I have maintained a bunch of aliases and settings to manage this. Gradually, bash has improved its history facilities, but I've just recently concluded that my approach is inadequate. I wonder if there's people with ideas here that I could use.

              Presently I have a separate history for four differently coloured konsoles, and use
              Code:
              HISTCONTROL=ignoredups
              HISTIGNORE="&:h"
              HISTSIZE=2000
              HISTFILESIZE=421
              but the only reason that I only save 420 entries, is that when the history number gets to 4 digits it's too long to type and takes up too much space when I list them; to do that I use:
              Code:
              alias h='history $((LINES*2)) | pr -2tw$COLUMNS'
              alias ha='history | less +G'
              Ideally I'd like to keep all history for years in a searchable form, but be able to reset the history number to something small when I want to.

              Perhaps it's another hint that I really should be using zsh.
              Regards, John Little

              Comment


                #8
                Originally posted by jlittle View Post
                Perhaps it's another hint that I really should be using zsh.
                I can't tell how big a difference there is these days (haven't kept up with bash development), but better history options was one of the reasons I went zsh years ago (like the ability of sharing history with open sessions, timestamps etc.)...and haven't really looked back. (The biggest reason for switching was superior tab completion).

                One of my latest zsh history discoveries is this one, which I find pretty neat: https://github.com/zsh-users/zsh-autosuggestions
                (I also really like zsh-syntax-highlighting, even though it's not related to history.)
                Last edited by kubicle; May 02, 2018, 04:12 AM.

                Comment


                  #9
                  kubicle;

                  Ok, you're usage certainly exceeds my needs. I seldom use bash for more than a few commands, mostly related to updating my systems (I have more than one). I find that some GUI update managers (I use Linux Mint) do not report the same updates from the repositories as the command line does. So I check in Terminal/Konsole.

                  So, limited need for very large history, except in rare instances, and then I want to clear the history to return to my simpler set of commands.
                  Kubuntu 24.11 64bit under Kernel 6.11.0, Hp Pavilion, 6MB ram. Stay away from all things Google...

                  Comment


                    #10
                    Originally posted by TWPonKubuntu View Post
                    Ok, you're usage certainly exceeds my needs. I seldom use bash for more than a few commands, mostly related to updating my systems (I have more than one). I find that some GUI update managers (I use Linux Mint) do not report the same updates from the repositories as the command line does. So I check in Terminal/Konsole.

                    So, limited need for very large history, except in rare instances, and then I want to clear the history to return to my simpler set of commands.
                    I hear you, but ctrl+r can still be very useful and fast, even with just a few dozen lines of history.

                    And certainly faster than retyping your basic commands (after clearing your history)...of course, such a thing could easily be automated as well (rather than completely removing your history, you could replace it a file that only has your basic "command set" as history items). That way you wouldn't need to retype your commands after every "clearance".

                    Comment


                      #11
                      Riley's Believe It or Not!
                      In 20 years of using Linux I never heard of ctrl+r & ctrl+s to search through bash history.

                      I tried it a while ago. IF I am not doing something wrong I find it very awkward and clumsy to use, IMO. Every time I tried to ctrl+r for "mount /dev/sdc /backup" it gave me "I found 3740 ..." but to use it I had to continue typing the command to narrow the results.
                      It was a lot easier just to type the command. I frequently use the up arrow and find the desired command within seconds.
                      "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


                        #12
                        Originally posted by GreyGeek View Post
                        I tried it a while ago. IF I am not doing something wrong I find it very awkward and clumsy to use, IMO. Every time I tried to ctrl+r for "mount /dev/sdc /backup" it gave me "I found 3740 ..." but to use it I had to continue typing the command to narrow the results.
                        It was a lot easier just to type the command.
                        The search works best if you search for a string that's distinctive. In your example, I'd probably search for "/backup" rather than "mount". (ctrl+r, then "/backup" and if finds a wrong match, press ctrl+r again to find the next match...chances are you'd find it pretty quickly).

                        Of course, the usefulness of the search depends greatly on the length of the command, some commands are quicker to type than to search.

                        Originally posted by GreyGeek View Post
                        I frequently use the up arrow and find the desired command within seconds.
                        That works well if you have used the command recently, but no one really wants to press the up arrow for 500 times to reach a command they used a month back

                        Comment


                          #13
                          That works well if you have used the command recently, but no one really wants to press the up arrow for 500 times to reach a command they used a month back
                          My point, exactly.

                          I'd like to see an option to display the history cache as a pulldown selection item which I could scroll while reading each command, in the sequence that each was used.
                          Kubuntu 24.11 64bit under Kernel 6.11.0, Hp Pavilion, 6MB ram. Stay away from all things Google...

                          Comment


                            #14
                            I've used this modification to .bashrc for years:

                            Code:
                            # enable command line history recall navigation filtering by using up and down arrows like in 4dos/NT/TC 
                            bind '"\e[A"':history-search-backward
                            bind '"\e[B"':history-search-forward
                            
                            # This gives the equivalent 4Dos/NT/TC Page-Up command to show all previous entered commands based
                            # on the partial input on the current line
                            bind '"\e[5~"':"\"\C-k\C-ahistory | grep '^ *[0-9]* *\C-e.'\C-m\""
                            I was a big fan of 4Dos back in my DOS days. The above gives a very similar function to what 4Dos could do.

                            You just type one or more letters of the command(s) you are looking for and each matching command will appear. Just keep typing up or down arrows or add more characters to find the one you're looking for.

                            The second mod allows you to see all previous commands based on a partial input line. I don't use this one so much myself as I've forgotten how to make more use of the list.
                            Desktop PC: Intel Core-i5-4670 3.40Ghz, 16Gb Crucial ram, Asus H97-Plus MB, 128Gb Crucial SSD + 2Tb Seagate Barracuda 7200.14 HDD running Kubuntu 18.04 LTS and Kubuntu 14.04 LTS (on SSD).
                            Laptop: HP EliteBook 8460p Core-i5-2540M, 4Gb ram, Transcend 120Gb SSD, currently running Deepin 15.8 and Manjaro KDE 18.

                            Comment


                              #15
                              Thanks Rod
                              Kubuntu 24.11 64bit under Kernel 6.11.0, Hp Pavilion, 6MB ram. Stay away from all things Google...

                              Comment

                              Working...
                              X