Quantcast
Channel: How to delete history of last 10 commands in shell? - Stack Overflow
Viewing all articles
Browse latest Browse all 23

Answer by Gabriel Staples for How to delete history of last 10 commands in shell?

$
0
0

How to delete unwanted history entries in your shell

...or even edit or add entries.

Quick summary

# check your shell's cached historyhistory# force-write the cached history to the `~/.bash_history` history filehistory -w# open the history file in your editor of choice (MS VS Code in my case)# - THEN MANUALLY EDIT THE FILE AS NEEDED, AND SAVE ITcode ~/.bash_history# force-read the history file into the shell's cached historyhistory -r# check your shell's cached history again to ensure it worked and accepted# your edits abovehistory# As a final sanity check, view the history file one more time to ensure it# looks as you expect. code ~/.bash_history

Details

I ran history and saw a command with some confidential information in it (a security key) that I don't want in my history.

So, I opened and checked the ~/.bash_history file per the main answer here, and saw that it did not match what was in my shell's history. This got me digging into the help menu to examine the -w and -r options to see what they did. A bit of experimentation later and I came up with the process above.

You might also consider using the append history -a option instead of history -w above. See the help menu I pasted below for details.

References

  1. I learned about history -w here: Unix & Linux: Why is the bash_history always coming up the same and not updating using non-login shells?
  2. See the history help menu:
    history --help
    Example run and output:
    $ history --helphistory: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...]    Display or manipulate the history list.    Display the history list with line numbers, prefixing each modified    entry with a `*'.  An argument of N lists only the last N entries.    Options:    -c  clear the history list by deleting all of the entries    -d offset   delete the history entry at position OFFSET. Negative            offsets count back from the end of the history list    -a  append history lines from this session to the history file    -n  read all history lines not already read from the history file            and append them to the history list    -r  read the history file and append the contents to the history            list    -w  write the current history to the history file    -p  perform history expansion on each ARG and display the result            without storing it in the history list    -s  append the ARGs to the history list as a single entry    If FILENAME is given, it is used as the history file.  Otherwise,    if HISTFILE has a value, that is used, else ~/.bash_history.    If the HISTTIMEFORMAT variable is set and not null, its value is used    as a format string for strftime(3) to print the time stamp associated    with each displayed history entry.  No time stamps are printed otherwise.    Exit Status:    Returns success unless an invalid option is given or an error occurs.

Viewing all articles
Browse latest Browse all 23

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>