Minggu, 02 September 2018

How to delete history command or Remove a certain line from Bash history file

  Tidak ada komentar
There are different ways to accomplish this task, but lets clarify something before going further.
There is a file named: ~/.bash_history, which contains your older terminal sessions history, whenever you close your terminal, your history will be saved there.
At the same time the history of your old sessions along with current session is temporary accessible by history commands until you close the terminal which then will be saved into ~/.bash_historyfile.
So if you remove 5 lines at the end of ~/.bash_history, then closing terminal will cause your current command to be accessible at next sessions.

You can follow this step to delete the history

  1. Typing history -d <line_number> deletes a specified line from the history in memory.
  2. Typing history -w writes the current in-memory history to the ~/.bash_history file.
  304  history
  305  #example
  306  history
[[email protected] ~]$
[[email protected] ~]$
[[email protected] ~]$ history -d 305
[[email protected] ~]$

  1. After we typing history -w the in memory history write to ~/.bash_history
  304  history
  305  history
  306  history -d 305
  307  history
[[email protected] ~]$

history file ~/.bash_history before we typinhistory -w as shown bellow 
screen -r
screen -r  32122
screen -r
df -h
ssh [email protected]
screen -r
df -h
After we typing history -w the in memory history write to ~/.bash_history
history -d 305
vi .bash_history
history -w

If you want to delete more than 1 line you can delete using vi by open the ~/.bash_history. file
You need to log out and back in or run history -a so the current history is committed to disk.
Then just edit the file ~/.bash_history.
[[email protected] ~]$
[[email protected] ~]$ vi .bash_history
[[email protected] ~]$
[[email protected] ~]$ history -a
[[email protected] ~]$
You can read the option of history command as shown bellow, you can also can read manual of history command by typing man history
 -c   Clear the history list by deleting all the entries.
 -d   offset
       Delete the history entry at position offset.
 -a   Append  the  ``new'' history lines (history lines entered since the beginning of the current bash session)  to  the
       history file.
 -n   Read  the history lines not already read from the history file into the current  history  list.   These  are  lines
       appended  to  the history file since the beginning of the current bash session.
 -r   Read the contents of the history file and use them as the current history.
 -w  Write  the current history to the history file, overwriting the history file's contents.
 -p   Perform history substitution on the  following  args  and display  the  result  on  the  standard output.  Does not
       store the results in the history list.  Each arg must  be quoted to disable normal history expansion.
 -s   Store  the  args  in  the history list as a single entry. The last command in the history list  is  removed  before
       the args are added.

Source :

Tidak ada komentar :

Posting Komentar