How to Delete History in Linux Easily

Written by: Bobbin Zachariah   |   Last updated: July 13, 2022

The .bash_history file located in the user’s home directory keeps the history of commands you typed on the terminal. This helps users to reuse listed commands without retyping it.

If you don't want others to see your typed commands for any reason, you can easily delete the history. In this guide, we learn how to delete history from Linux.

1. Delete History List Command Line

To see your bash history using history command, type:

$ history 
 1 history 
 2 vim .bash_history 
 3 exit
 4 history

To delete history list from the command line, use the following commands:

$ history -c
$ history -w

2. Delete specific command from history

From the history list if you want to delete specific command, use the following syntax:

$ history -d <line-number>

For example to delete the sixth entry history list, type:

$ history -d 5
$ history -w

3. Delete history without a trace

To clear the bash history completely without a trace on the server, one solution is to link ~/.bash_history to /dev/null.

$ cat /dev/null > ~/.bash_history

Alternatively, you can use the wipe tool to securely delete .bash_history file:

$ wipe ~/.bash_history

4. Turn off bash history

The $HISTFILE variable store the bash history file location.

You can stop history logging into the history file by adding unset HISTFILE to user profiles.

$ echo "unset HISTFILE" >> /etc/profile
or
$ echo "unset HISTFILE" >> /home/tom/.bash_profile

Alternatively, you can set zero values to HISTSIZE and HISTFILESIZE in the .bashrc file.

5. Delete History Range

If you want to delete N lines from the bash history, you may use the following one-liner.

for i in {1..N}; do history -d START_POSITION; done

Where,

START_POSITION is the line number in the history list from where you need to start deleting.

N is no of the entries you want to delete.

For example to delete 13 entries from line 986, type:

for i in {1..13}; do history -d 986; done

Conclusion

In this guide, we learned how to clear history in Linux. Thanks for reading, please share your feedback and suggestions in the comment section.

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin Zachariah is an experienced Linux engineer who has been supporting infrastructure for many companies. He specializes in Shell scripting, AWS Cloud, JavaScript, and Nodejs. He has qualified Master’s degree in computer science. He holds Red Hat Certified Engineer (RHCE) certification and RedHat Enable Sysadmin.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment