how do I remove the last 5 lines in bash history?
Solution 1
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_history
file.
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.
So if I do a wc
on .bash_history:
wc -l ~/.bash_history
Most of the time I'll get a smaller number than of history | wc -l
.
If you want to remove the last 5 line of the file, you can use this command:
for i in {1..5}; do sed -i '$d' .bash_history; done;
And if you want to keep all history except last 5 command issued in current session run:
history | awk '{ print $2 }' | head -n -5 > .bash_history
Don't forget to run history -c
too.
Solution 2
You can achieve removal from the history file using the commandline in two steps:
Typing history -d <line_number>
deletes a specified line from the history in memory.
Typing history -w
writes the current in-memory history to the ~/.bash_history file.
The two steps together remove the line permanently from the in-memory history and from the .bash_history file as well.
Ref: Super User
Solution 3
I think you need to try this its very easy & simple.
-
How to delete history without any trace
history -d $((HISTCMD-1)) && history -d NO_of_History_you_want_to_delete
-
if you want to excute a command without leaving any trace.
history -d $((HISTCMD-1)) && type_your_command_here_and_execute
Solution 4
Write the current history to the history file, overwriting the history file's contents
history -w
Edit history as you wish
vi ~/.bash_history
Read the contents of the history file and use them as the current history.
history -r
Solution 5
Open ~/.bash_history
in your editor, and remove last 5 lines.
Related videos on Youtube
Anthony Pinto
Updated on September 18, 2022Comments
-
Anthony Pinto over 1 year
How can I remove the last 5 lines in bash history? So that when I reload the Ubuntu server, or restart it they're not there at all?
history -c
only removes it from current session, but when I re-login I see the commands again, I want to clear the last 5.I've run:
history
Then i'll see the numbers of the commands e.g:
489 cd .. 490 cd .zshrc 491 cat .zshrc
Then I run for example:
history -d 489 history -c
Then i close terminal and reopen it and i still see line 489 it was only deleted for that current session, how do I delete it permentantly from all sessions going forward?
-
Anthony Pinto about 7 yearsWill that remove only the last 5 commands in bash? And permanently? e.g. so when I shut down terminal and re-login to ssh the last 5 won't be there?
-
Anthony Pinto about 7 yearsps. tried it locally first and got "head: illegal line count -- -5"
-
Anthony Pinto about 7 yearsThis is a lot closer, so I run history -wd 493 for example, and it works to delete it for good, however, then in history I have a bunch of history -wd's etc... so it's counterintuitive, how can i clear e.g. the last 5 commands all in one shot that would be best, and then the actual command i used to clear them itself.
-
Anthony Pinto about 7 yearsWhat seems to kind of work is when I run history -wd 493 for example, and it works to delete it for good, however, then in history I have a bunch of history -wd's etc... so it's counterintuitive, how can i clear e.g. the last 5 commands all in one shot that would be best, and then the actual command i used to clear them itself
-
EODCraft Staff about 7 yearsIs there not a history man page? Sry I'm in Windows....
-
Anthony Pinto about 7 yearsNot that great with VIM - can I run, history -wd 1, history -wd 2, history -wd 3, history -wd 4, history -wd 5, and then history -c ? Then next time i open all 5 will be gone plus the commands for that session to delete them?
-
Ravexina about 7 yearsTest the new code. works fine for me.
-
Apologician about 7 years@EODCraftStaff Type
man
and the name of the command manual you want to see. In this case:man history
. -
Anthony Pinto about 7 yearsSays, "head: illegal line count -- -5" still. :(
-
Ravexina about 7 yearswhat is the
head --version
output? -
Anthony Pinto about 7 years[~]$head --version head: illegal option -- - usage: head [-n lines | -c bytes] [file ...]
-
Ravexina about 7 yearsWhat version of Ubuntu are you running?
-
Anthony Pinto about 7 yearsOh was testing locally first, ok so if I run "history | cut -f4 -d' ' | head -n -5 > .bash_history" in Ubuntu, and then run history -c I won't see the command "history | cut -f4 -d' ' | head -n -5 > .bash_history" either right?
-
Ravexina about 7 yearsyou shouldn't get any output. it will read your history, removes the last lines, then write it to the .bash_history file.
-
Nick Rodriguez about 7 yearsIf you want to hide commands from history use the HISTIGNORE variable, e.g. in your .bashrc, HISTIGNORE='clear:history:ls:cd'
-
Anthony Pinto about 7 yearsso it will remove the last 5 lines, but it will still write: "history | cut -f4 -d' ' | head -n -5 > .bash_history" to the .bash_history file? Is there anyway to remove the last 5 lines, and the remove the command that removes the 5 liens as well? If we can do that, then we're all set! :)
-
Ravexina about 7 yearsafter running that command, use
history -c
. but after that don't run the suggested command again because it will remove all history (after running history -c). -
EODCraft Staff about 7 yearsYou can use gedit ~/.bash_history, don't have to use vim or nano.
-
Anthony Pinto about 7 yearsran this command: "history | cut -f4 -d' ' | head -n -5 > .bash_history" it really messed up the history, if you close out of terminal then open terminal again and look at history it's all messed up,, don't think it's doing what it';s supposed to.. it removes a lot more than 5. can you look into it?
-
Ravexina about 7 yearsLet us continue this discussion in chat.
-
Ravexina about 7 years@AnthonyPinto I made the answer more clear, I think it wort to read, also I changed
cut
toawk
in second code. ;) -
Anthony Pinto about 7 yearsThanks! :) ps. any experience with dumping db's from postgresql to local machine? stackoverflow.com/questions/43334419/…
-
Ravexina about 7 years@AnthonyPinto Unfortunately, never touched it.
-
Stephen about 7 yearsUse a space before your history delete command and it wont go to your history after it removes content from the history. Alternately,
kill -s 9 $$
when you're done. -
David Foerster over 6 years(2.) doesn't answer the question and it would be far easier to prepend the traceless command with a space character to omit its addition to the history.
-
theking2 over 5 years(1.) is both an excellent and elegant solution. I'm not sure how adding a space prevents it from being added to the history, David
-
chaptuck almost 5 years@theking2 For some systems pre-pending a space tells bash not to write the command to the history https://stackoverflow.com/questions/6475524.
-
Cadoiz over 2 years@AnthonyPinto You should consider this superuser question