Linux history clear one command

How to delete history of last 10 commands in shell?

I can delete single one by history -d 511 , but how to delete last 10 commands and in between 10 commands history using single command in shell? Can we write a bash script and execute for deletion of history?

Is this question off-topic? or This is not wright place to ask this kind of question? If so, where should I ask this question, I mean where in stack exchange?

Unix and Linux StackExchange (unix.stackexchange.com) is probably a better venue for this question. It’s off-topic here because it doesn’t relate strictly to programming.

Programming a shell script is the ideal solution to this question, consequently it should be considered related to programming. Not to mention the fact that the OP actually asked specifically for a script.

21 Answers 21

Have you tried editing the history file directly:

If you do this with vim you may find your deletion history in ~/.viminfo so be sure to remove that too!

@FizerKhan I mean that changes made in .bash_history can be overwritten by what is kept in memory. I found this solution helpful: superuser.com/a/384383/253543

My answer is based on previous answers, but with the addition of reversing the sequence so that history items are deleted from most recent to least recent.

Get your current history (adjust the number of lines you want to see):

This gives me something like

1003 25-04-2016 17:54:52 echo "Command 1" 1004 25-04-2016 17:54:54 echo "Command 2" 1005 25-04-2016 17:54:57 echo "Command 3" 1006 25-04-2016 17:54:59 echo "Command 4" 1007 25-04-2016 17:55:01 echo "Command 5" 1008 25-04-2016 17:55:03 echo "Command 6" 1009 25-04-2016 17:55:07 echo "Command 7" 1010 25-04-2016 17:55:09 echo "Command 8" 1011 25-04-2016 17:55:11 echo "Command 9" 1012 25-04-2016 17:55:14 echo "Command 10" 

Select the start and end positions for the items you want to delete. I’m going to delete entries 1006 to 1008.

for h in $(seq 1006 1008); do history -d 1006; done 

This will generate history -d commands for 1006, then 1007 becomes 1006 and 1006 is deleted, then 1008 (became 1007) is now 1006 and gets deleted.

If I also wanted to delete the history delete command then it’s a bit more complicated because you need to know the current max history entry.

You can get this with (there may be a better way):

Putting it together you can use this to delete a range, and also delete the history delete command:

for h in $(seq 1006 1008); do history -d 1006; done; history -d $(history 1 | awk '') 

Wrap this all up in a function to add to your ~/.bashrc :

Example deleting command 4, 5 and 6 (1049-1051) and hiding the evidence:

[18:21:02 jonathag@gb-slo-svb-0221 ~]$ history 11 1046 25-04-2016 18:20:47 echo "Command 1" 1047 25-04-2016 18:20:48 echo "Command 2" 1048 25-04-2016 18:20:50 echo "Command 3" 1049 25-04-2016 18:20:51 echo "Command 4" 1050 25-04-2016 18:20:53 echo "Command 5" 1051 25-04-2016 18:20:54 echo "Command 6" 1052 25-04-2016 18:20:56 echo "Command 7" 1053 25-04-2016 18:20:57 echo "Command 8" 1054 25-04-2016 18:21:00 echo "Command 9" 1055 25-04-2016 18:21:02 echo "Command 10" 1056 25-04-2016 18:21:07 history 11 [18:21:07 jonathag@gb-slo-svb-0221 ~]$ histdel 1049 1051 [18:21:23 jonathag@gb-slo-svb-0221 ~]$ history 8 1046 25-04-2016 18:20:47 echo "Command 1" 1047 25-04-2016 18:20:48 echo "Command 2" 1048 25-04-2016 18:20:50 echo "Command 3" 1049 25-04-2016 18:20:56 echo "Command 7" 1050 25-04-2016 18:20:57 echo "Command 8" 1051 25-04-2016 18:21:00 echo "Command 9" 1052 25-04-2016 18:21:02 echo "Command 10" 1053 25-04-2016 18:21:07 history 11 

The question was actually to delete the last 10 commands from history, so if you want to save a little effort you could use another function to call the histdel function which does the calculations for you.

histdeln()< # Get the current history number n=$(history 1 | awk '') # Call histdel with the appropriate range histdel $(( $n - $1 )) $(( $n - 1 )) > 

This function takes 1 argument, the number of previous history items to delete. So to delete the last 10 commands from history just use histdeln 10 .

Читайте также:  Chakra linux что это

Источник

How to Clear BASH Command Line History in Linux

The bash history keeps a record of all commands executed by a user on the Linux command line. This allows you to easily run previously executed commands by using the “up arrow” or “down arrow” keys to scroll through the command history file.

In this article, we will show you two simple ways to clear your command-line history on a Linux system.

The major reason for removing command-line history from the Linux terminal is to prevent another user, who could be using the same account.

For instance if you have typed a command that contained a password in plain-text and you don’t want another system user or an attacker to see this password, you need to delete or clear the history file.

Take a look at the command below, here the user aaronkilik has typed the database server password on the command line.

If you look into th bash history file towards the end, you will see the password typed above in there.

Check Last Executed Commands

The bash_history file is normally located in a user’s home directory /home/username/.bash_history.

$ ls -l /home/aaronkilik/.bash_history

To remove a single line from the history file, use the -d option. For example, if you want to clear a command where you entered clear-text password as in the scenario above, find the line number in the history file and run this command.

To delete or clear all the entries from bash history, use the history command below with the -c option.

Alternatively, you can use the command below to delete history of all last executed commands permanently in the file.

$ cat /dev/null > ~/.bash_history

Note: A normal user can only view his/her own command history, but the root user can view the command history of all other users on the system.

You can learn more about the bash history file and useful history commands here: The Power of Linux “History Command” in Bash Shell.

Always remember that all commands you run are recorded in a history file, so do not type plain-text passwords on the command line. If you have questions or thoughts to share with us, make use of the feedback form below.

Источник

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.

Читайте также:  Networking with linux pdf

@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.

Источник

How to clear bash history completely?

I want to clear all previous commands from the history of my server. I used history -c and it seems all things are cleared but when I ssh to the server, all the commands are still there. How can I clear them permanently?

9 Answers 9

The file ~/.bash_history holds the history.

To clear the bash history completely on the server, open terminal and type

cat /dev/null > ~/.bash_history 

Other alternate way is to link ~/.bash_history to /dev/null

Читайте также:  Акронис диск директор linux

However,

One annoying side-effect is that the history entries has a copy in the memory and it will flush back to the file when you log out.

To workaround this, use the following command (worked for me):

cat /dev/null > ~/.bash_history && history -c && exit 

you can also put above command in .bashrc & .bash_logout . what it mean when you login u will have clear history & when you logout out your history will be cleared

I tried to do it on a raspberry box while connected via SSH. I added the above command ( cat /dev/null > ~/.bash_history && history -c && exit ) to both .bashrc & .bash_logout as suggested by @Qasim . Now as soon as I connect via SSH the remote host closes the connection (after printing the motd) . Help :/

Goes to show what happens when you blindly do things without reading properly and without understanding the processing and data flow. It also shows the pitfalls of bad communication. What devav2 did was run it one time, and what Qasim should have written is to remove the exit command before adding it in login script.

In every open bash shell (you may have multiple terminals open):

Why: As noted above, history -c empties the file ~/.bash_history . It is important to note that bash shell does not immediately flush history to the bash_history file. So, it is important to (1) flush the history to the file, and (2) clear the history, in all terminals. That’s what the commands above do.

For some reason this does not work on Ubuntu 14.04, probably others. It should but it doesn’t. If you issue the command «history -cw» you can confirm with the up arrow that the history isn’t there anymore, but if you start another terminal window (in Unity desktop) with shift + click on the terminal icon (I have it pinned in the launcher) the commands history are back, no matters how many times you do «history -cw». «cat /dev/null > ~/.bash_history» is the only way that worked for me.

It’s history -c and then history -w, you first clear the history then write the changes. This is easily confirmed by closing the terminal and opening it again, with -w first the commands are there again, with -w last history is effectively cleared.

execute the following commands to clear history forever

This answer would be even more helpful if it explained what these flags do (and therefore why/when they’re the right commands to use).

There’s another much simpler one: running history -c on the terminal prompt and gone are all entries in the bash_history file.

If you read the OP’s question you would know that using history -c is the exact method that led to this question.

Clear the current shell’s history:

When you log out, your current shell’s history is appended to ~/.bash_history, which is a cache of previous shells’ histories, to a maximum number (see HISTFILESIZE in «man bash»).

If you want to remove the history altogether, then you essentially have to empty out ~/.bash_history which many of the above entries have suggested. Such as:

This clears the current shell’s history and then forces the current shell’s history (empty) to overwrite ~/.bash_history. or to be more accurate, it forces it to overwrite HISTFILE (which defaults to ~/.bash_history).

Источник

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