Linux clear history last

Delete last N lines from bash history

When accidentally pasting a file into the shell it puts a ton of ugly nonsense entries in the bash history. Is there a clean way to remove those entries? Obviously I could close the shell and edit the .bash_history file manually but maybe there’s some kind of API available to modify the history of the current shell?

14 Answers 14

Just this one liner in command prompt will help.

for i in ; do history -d START_NUM; done 

Where START_NUM is starting position of entry in history. N is the number of entries you may want to delete.

ex: for i in ; do history -d 1030; done

askubuntu.com/a/978276/22866 has a nice way to delete the «delete from history command» from history 🙂

@RajeevAkotkar If the delete command is the nth line, then using N+1 in the for loop will also help delete the command that did the deleting.

Alternatively, you can also use for i in <1030..1080>; do history -d 1030; done instead — which might be a bit more intuitive. I wrote a small bash script, just replace 1030 with $1 and 1080 with $2 .

As of bash-5.0-alpha , the history command now takes a range for the delete ( -d ) option. See rastafile’s answer.

For older versions, workaround below.

You can use history -d offset builtin to delete a specific line from the current shell’s history, or history -c to clear the whole history.

It’s not really practical if you want to remove a range of lines, since it only takes one offset as an argument, but you could wrap it in a function with a loop.

Call it with rmhist first_line_to_delete last_line_to_delete . (Line numbers according to the output of history .)

(Use history -w to force a write to the history file.)

Since the OP asked for deleting the N last lines, this script should be modified by doing something like: tot_lines=$(history | wc -l) and then repeat history -d $(( tot_lines — $1 )) .

The history bash built-in command also takes a range, now in 2020:

-d offset

Delete the history entry at position offset. If offset is negative, it is interpreted as relative to one greater than the last history position, so negative indices count back from the end of the history, and an index of -1 refers to the current history -d command.

-d start-end

Delete the history entries between positions start and end, inclusive. Positive and negative values for start and end are interpreted as described above.

Only the command itself does not tell with —help :

 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 . 

E.g. history -d 2031-2034 deletes four lines at once. You could use $HISTCMD to delete from the newest N lines backwards.

You can also export with history -w tmpfile , then edit that file, clear with history -c and then read back with history -r tmpfile . No write to .bash_history.

If you delete line N from the history, then line N+1 moves in position N etc.

For this reason, I prefer identifying the oldest and newest history line between which I want to delete all history. (Note: oldest < newest ).

Читайте также:  Различия между unix linux

If for instance I want to delete the history lines from oldest = 123 up to newest = 135 , I’d write:

$ for i in ; do history -d $i ; done 

I find it easier to read; besides: the for command can also decrement a range.

the history -d arg takes a range and $HISTCMD is the max number in the history.

This function works to remove the last n entries from history (just pass in the number of history commands to remove like, eg rmhist 5 :

Or.. Go fancy with an arg like this to remove from a point in history (inclusive) or last ‘n’ commands:

The result looks something like this:

 5778 ls /etc 5779 ls /tmp 5780 ls /dev 5781 ll 5782 cd /tmp 5783 cd 5784 history (base) ~ $ rmhist --last 3 (base) ~ $ history 5 5778 ls /etc 5779 ls /tmp 5780 ls /dev 5781 ll 5782 history 5 (base) ~ $ rmhist --from 5780 (base) ~ $ history 5 5776 history 10 5777 ls 5778 ls /etc 5779 ls /tmp 5780 history 5 

I get «bash: history: N: history position out of range» errors, but this worked for me to delete the last entry hdl() < history -d $(($HISTCMD - $1))-$(($HISTCMD - 2)) ;>I’ll write any error understandings in my answer here: unix.stackexchange.com/a/573258/346155

What’s different in this answer: it also displays what is being deleted.

For those using Bash 4.x (e.g. in Centos 7 and lower), I use this to delete the last N items in the history including the command to do this. (here N=5)

for i in ; do echo "clearing line $(($HISTCMD-2)): $(history -p \!$(($HISTCMD-2)))"; history -d $(($HISTCMD-2)); done; history -d $(($HISTCMD-1)) 

To delete the history lines between n and m (e.g. here 544 and 541), I use the following. NOTE: you must enter the bigger line number first and then the smaller one:

for i in ; do echo "clearing line $i: $(history -p \!$i)"; history -d $i; done; history -d $(($HISTCMD-1)) 

The answer by user2982704 almost worked for me but not quite. I had to make a small variation like this.

Assuming my history is is at 1000 and I want to delete the last 50 entries

start=1000 for i in ; do count=$((start-i)); history -d $count; done 

It works for me. To delete line 2 to 29 (includes line 2 and line 29):

This should delete every history list entry in reverse order N times:

If you want to add this to your .bashrc, use:

..and then source ~/.bashrc to reload bash config. Use as:

hd , where N is the number of lines to delete 

My other answer for a different approach that is not working, but is long and complicated.

EDIT: this started giving me the error «bash: history: -N: history position out of range» as does history -d -2 for some reason. So I used another users answer, that even though it gave me the same error, I could adjust to make work. This example deletes only the last line, but can be extended.

Before I give up on this alternative approach, I want to put down what I’ve learned. Part of my difficulty in debugging my first answer is that its a bit unclear how many N ends up being, because it includes the current command, so I think it is really N-1 or maybe N-2.

To make it more clear, I was trying a solution that would delete from a line number going forward. And wow, what a big surprise of how difficult that is. The main problem is the history routine is running in the background so deleting say number 50 will result in number 51 now being 50 in less than a second. So I tried making a function to delete going backwards from the end. And when trying to deleting 10 in a row and it gets about 5 before running out of commands as they shift down. So I tried making a function to delete forwards and it, for some reason only gets every other one. Consistent with the history routine running about half as fast as the function. I also tried inserting sleep at certain points.

Читайте также:  Linux ubuntu install samba

I tried turning off history temporarily, but then $HISTCMD show the total from $HISTFILE not the history list. So it looks nary a possible. My goal was to clean the history of unnecessary commands so that I could memorize the bang number !# and use the same ones going forward for frequent and difficult commands.

Here’s some of the functions I tried. Maybe someone can improve or diagnose:

Going from the Number to the end: function hd2 < a=$HISTCMD; echo $a; echo $1; echo $(($a%$1)); for i in $(seq $1 $a); do echo $i; history -d $i; done; >Deleting from the end repeatedly with sleep: function hd3 < a=$HISTCMD; echo $a; echo $1; echo $(($a%$1)); for i in $(seq 1 $a); do history -d $HISTCMD; sleep 1; done; >Deleting the Number repeatedly with sleep: function hd4

The beginning echo variable are for debugging. % is remainder or mod. $HISTCMD does not carry through command substitution and need to be assigned as a variable.

new idea: since the function below works with repeated use:

echo "function hdn < history -d \$1; >">>~/.bashrc echo "function hdn2 < for i in $(seq 1 \$2); history -d \$1; sleep 1; done; >">>~/.bashrc 

Источник

How to clear all history in linux/ubuntu terminal or bash permanently? [closed]

When you use the up key in a Linux terminal, you can use previous commands again. Great feature. However, I started logging mysql into mysql with the sensitive details in the command. How can I delete that history permanently?

Since this question is closed I can’t add this as an answer. You can tell bash not to save any history for a particular session with this command: export HISTFILE=/dev/null

2 Answers 2

You can clear your bash history like this:

Really like this answer — this takes effect immediately, rather than deleting .bash_history which requires the shell to be restarted to take effect.

Does not work on my Ubuntu 14.04 machine. History just appears with new terminal. All that worked is >~/bash_history . Have to restart terminal for this though.

Also works on Mac OS X El Capitan (tested on version 10.11.2), but you have to add that following line to your ~/.bash_profile : export SHELL_SESSION_HISTORY=0 , then do a source ~/.bash_profile and to finish quit and restart your Terminal app. If you want to understand what this export command does, then you should definitely check this following link: superuser.com/questions/950403/…

isn’t only -c enough? Manuals says -c clears the history list by deleting all of the entries. -w wirtes the current history to history file and append them to history list. Just -c works fine.

If you use bash, then the terminal history is saved in a file called .bash_history. Delete it, and history will be gone.

However, for MySQL the better approach is not to enter the password in the command line. If you just specify the -p option, without a value, then you will be prompted for the password and it won’t be logged.

Another option, if you don’t want to enter your password every time, is to store it in a my.cnf file. Create a file named ~/.my.cnf with something like:

Читайте также:  Установить python3 на linux

Make sure to change the file permissions so that only you can read the file.

Of course, this way your password is still saved in a plaintext file in your home directory, just like it was previously saved in .bash_history.

Источник

How do I clear the terminal History?

I am using Linux Mint 17.1 Rebecca for about 2 days and accidentally typed my password into the terminal which is now displayed in the history list of commands I have previously typed. I want to clear the terminal history completely. I have tried using the following commands in the terminal which I thought would clear the history forever but they do not:

history -c reset tput reset 

The above commands «will» clear the history from the terminal but when I exit and bring up a new one all my previous history is still there and can all be listed again using the — history command and also by pressing the UP arrow on my keyboard. I do not want this to happen until I have totally cleared my history, then I want to continue using it. How can I clear my terminal history completely — forever and start fresh? Please Note: I do not want to exit the terminal without saving history just clear it forever in this one instance.

@jasonwryan That alone wouldn’t solve the problem since the sed command would end up in the shell history.

I tried using the code from @jasonwryan but I got: sed: -e expression #1, char 0: no previous regular expression which I think I know why and that lead me to come up with this from a search and some messing around: cat /dev/null > ~/.bash_history && history -c && exit

7 Answers 7

reset or tput reset only does things to the terminal. The history is entirely managed by the shell, which remains unaffected.

history -c clears your history in the current shell. That’s enough (but overkill) if you’ve just typed your password and haven’t exited that shell or saved its history explicitly.

When you exit bash, the history is saved to the history file, which by default is .bash_history in your home directory. More precisely, the history created during the current session is appended to the file; entries that are already present are unaffected. To overwrite the history file with the current shell’s history, run history -w .

Instead of removing all your history entries, you can open .bash_history in an editor and remove the lines you don’t want to keep. You can also do that inside bash, less conveniently, by using history to display all the entries, then history -d to delete the entries you don’t want, and finally history -w to save.

Note that if you have multiple running bash instances that have read the password, each of them might save it again. Before definitively purging the password from the history file, make sure that it is purged from all running shell instances.

Note that even after you’ve edited the history file, it’s possible that your password is still present somewhere on the disk from an earlier version of the file. It can’t be retrieved through the filesystem anymore, but it might still be possible (but probably not easy) to find it by accessing the disk directly. If you use this password elsewhere and your disk gets stolen (or someone gets access to the disk), this could be a problem.

Источник

Оцените статью
Adblock
detector