Linux history delete line

how do I remove the last 5 lines in bash history?

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:

 489 cd .. 490 cd .zshrc 491 cat .zshrc 

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?

Is the history file not just a simple .txt file that can be edited?. Sorry in Windows right now and never actually tried.

5 Answers 5

You can achieve removal from the history file using the command line in two steps:

Typing history -d 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.

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

@EODCraftStaff Type man and the name of the command manual you want to see. In this case: man history .

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

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 
history -d $((HISTCMD-1)) && type_your_command_here_and_execute 

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

(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

@theking2 For some systems pre-pending a space tells bash not to write the command to the history https://stackoverflow.com/questions/6475524.

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:

Most of the time I’ll get a smaller number than of history | wc -l .

Читайте также:  Usb модем huawei линукс

If you want to remove the last 5 line of the file, you can use this command:

for i in ; 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.

Источник

Delete history without a trace in Linux

Your bash history can be seen using the history command. If you run it, all the user history will be displayed as shown below.

By default, the bash history is located in the file .bash_history file located in the user’s home directory. In my case, the user is linuxhint, to see the history using cat I run:

Note: you can learn your history file location by running the command below.

Deleting your history without a trace in Linux

Deleting the command line activity history is simple, and there are few ways to achieve it. The first method uses the history command shown above, followed by the -c (clear) option, as shown in the screenshot below.

As you can see, after running history -c, if we run the history command without options again, there will not be output except for the current command.

Another way to remove your bash history is to remove the hidden .bash_history file located in the user home directory.

Since we want to remove the command line history without leaving a trace, let’s do it so that recovering the history file is impossible. To do it, we need to install the wipe tool.

To install wipe on Debian based Linux distributions, run:

Now wipe was installed, you can securely remove the .bash_history file without the chance it will be recovered.

Now your .bash_history file was fully wiped but will be automatically generated again to record future commands when you log out and log in back as the same user. At the end of this tutorial, you will find instructions to prevent the .bash_history file from recording commands.

Removing a specific line from the .bash_history file

Let’s suppose, like most Linux users, you use the .bash_history when repeating commands, but you want to remove a specific mistake. In some cases, some users may type a password in their terminal; this is extremely insecure.

In the following screenshot, we can see 7 commands.

Let’s say you only want to remove the sixth command (wipe .bash_history). To do it, you can use the history -d command followed by the line you want to remove, as shown below.

As you can see, the command was removed, but you need to write the changes using the history command followed by the -w flag, as shown below.

Now the specific line you wanted to delete was removed.

You also can remove line ranges. The following example shows how to remove a specific number of lines starting from a specific line. The command below will remove 10 lines starting from line 40. If you want to remove a number other than 10 lines, replace the number 10 with the number of lines you want to get deleted. Replace the number 40 with the line you want the line range to start.

Turning off the bash history

This tutorial section explains different methods to disable the bash history.

If you want to disable the bash history for the current shell only, run the command below.

Читайте также:  Sql server connection linux

As you can see, after running set+o history, that command was the last recorded, which means no commands typed after that were recorded, including the history command used to check the result.

You can see if the history is enabled by running the command below.

Note: You can enable the bash history back by running the command below.

To disable the bash history permanently, run the command below to add the rule set +o history to your .bashrc file. The .bashrc file stores the configuration for your terminal sessions, including the shell history, among other features.

As you can see, at the bottom of your .bashrc file, now you’ll see the set+o history rule added.

Apply the changes by running the .bashrc file as shown below.

To disable the history for the whole system, run the command below with root privileges to unset the HISTFILE variable.

Note: If you want to recover the shell activity history, you can remove the file you just created under /etc/profile.d.

You also can disable the bash history for new or future users, keeping it for existing users. To achieve it, run the command below.

To undo the previous command and restore the bash history, you need to remove the set+o history from the /etc/profile file

Remove the highlighted line (the last one containing the set+o history command).

Limiting the bash history size

There are 2 variables limiting the bash history size, the $HISTFILESIZE and $HISTSIZE variables. The difference between them is that $HISTFILESIZE defines the number of lines that will be saved to disk and remain after the session ends. The second variable, $HISTSIZE, defines the number of lines that will be saved in the history but won’t remain after you close the session.

To see the number of lines saved in your history, run the commands below even after you close the session.

As you can see, after closing and logging back, my history will keep my last 100 commands.

To change the number of lines saved to disk, run the command below.

Note: 50 is the number of lines you want to keep in your history even after the closed session. Replace it with the number of lines you want.

As you can see, now your history saves your last 50 commands to disk.

Run the command below to see the number of lines saved while in the same session until logging out.

As you can see, your session history records your last 500 commands.

To change the $HISTSIZE run:

Now your sessions will keep your last 50 commands.

Conclusion

As you can see, removing your bash history or specific content within it is pretty easy, including for new Linux users. Knowing how to remove your history is especially important when you work remotely and need to keep activity safe. In some cases, users may type their passwords by mistake on the terminal, leaving sensible information. In some cases, some administrators may opt for disabling this feature at all. Yet, the bash history is an excellent feature that helps us repeat commands very easily by pressing a key.

I hope this tutorial explaining how to delete the history in Linux without a trace was useful. Keep following Linux Hint for more Linux tips and tutorials.

Читайте также:  Команды linux head tail

About the author

David Adams

David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.

Источник

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