How to Clear Linux Command Line History

You may want to clear the history file and the screen for security reasons. Some Linux distributions may clear the screen when you logout but others do not. Many programs read input as a single line at a time.

The GNU history library is able to keep track of those lines, associate arbitrary data with each line, and utilize information from previous lines in composing new ones. Bash and other shells may use this history library. The default file is ~/.history or ~/.bash_history.

Bash's history feature

bash’s history function depends on a variable called HISTFILE, normally set to the current user’s .bash_history file (located in the user’s home directory). When echoed, it returns the full path and name of the user’s history file, like so:

Chrome - How to Clear Cache
Chrome - How to Clear Cache
$ echo $HISTFILE

1) Remove Linux history command

History can be reset with some command but after the operation, if you logout and login in your shell, you will see the same history

a. Delete the previous commands

You can use history -c command to clear the previous history command 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.

The example below shows our current history

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

Now let's use the command.

$ history -c
$ exit

When you exit bash, the history is saved to the history file. The history created during the current session is appended to the file, entries that are already present are unaffected. Let's check the new history

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

Let us add some command to our history.

$ history 
 1 history 
 2 vim .bash_history 
 3 exit
 4 exit
 5 history 
 6 ls -l
 7 mkdir aa
 8 ls -li
 9 history

To overwrite the history file with the current shell's history, run history -w after history -c command

$ history -c
$ history -w
$ exit

After login again, let's us check our history

$ history 
 1 history -w
 2 exit
 3 history

You can see that our history begin at history -w entry command.

b. Delete a single command

You can delete the history's entries which you don't want with the -d option. This will delete the history entry at position offset. But when you close terminal and open it again histories are not deleted. So we finally use history -w to save the changes.

# history -d offset

For examples:

# history
 1 cd
 2 history
 3 ls -alhF
 4 history
 5 wget
 6 mkdir

Now, if you want to delete the sixth entry which is mkdir command just use:

# history -d 5
# history -w

See the result

# history
 1 cd
 2 history
 3 ls -alhF
 4 history
 5 wget
 6 history
 7 history -d 6

You can see that we didn't have mkdir command entry above.

2) Clear bash completely

To clear the bash history completely on the server, an alternative solution is to link ~/.bash_history to /dev/null

$ ln -sf /dev/null ~/.bash_history

However, one annoying side-effect is that the history entries is linked to the memory and it will flush back to the file when you log out. To workaround this, you can use the following command:

ln -sf /dev/null ~/.bash_history && history -c && exit

3) Turn off bash history

You can stop logging history using one of the two ways: turn it off for all users, or turn off logging history for a single user.

a. Turn off for all users

You can turn off the bash history for all user adding unset HISTFILE line in /etc/profile file. This line deactivate the history file of each user on the system

# echo "unset HISTFILE" >> /etc/profile

You need to have the permission to apply the command above

b. Turn off for a specific user

The command above, it is possible to turn off the bash history of a specific user. You just need to indicate his bash_profile file.

# echo "unset HISTFILE" >> /home/papso/.bash_profile

Every the user will login, his history will be reset as below. All his history command will save until user logout

$ history 
 1 history

c. Edit .bashrc

You can remove the history command by editing two values of history command parameters.

  • HISTSIZE which is the number of lines or commands that are stored in memory in a history list while your bash session is ongoing
  • HISTFILESIZE which saves the amount of lines used for the history stack when it’s written to the history file.

To do it, edit your .bashrc and add


Now you can successfully delete the bash history and even stop logging to bash history using any of the above-listed commands.

4) Delete some entries lines

You can use history -d offset built in to delete a specific line from the current shell's 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 with a loop.

Just this one liner in the command prompt will help.

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

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

Let's check the last 15 entries of our history command

# history 15
 986 stat synchro 
 987 ln synchro sync2
 988 stat synchro 
 989 debugfs /dev/vda
 990 exit
 991 echo $HISTFILE
 992 cat .bash_history 
 993 ls -l .bash_history 
 994 passwd patrick
 995 echo "unset HISTFILE" >> /home/papso/.bash_profile
 996 history 
 997 for i in {1..10}; do history -d 867; done
 998 history 
 999 history 30
 1000 history 15

Now let us delete 13 entries beginning at the line 986

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

Now let's check the result

# history 15
 975 ls -il /home/bobbin/ filesync 
 976 ls -l /home/bobbin/ filesync 
 977 ln /home/bobbin/ synchro
 978 ls -il /home/bobbin/ synchro 
 979 cp script script2
 980 ls -il script script2
 981 stat script
 982 stat script2
 983 man find
 984 find / -inum 517333
 985 stat filesync 
 986 history 30
 987 history 15
 988 for i in {1..13}; do history -d 986; done
 989 history 15

You can see that we don't have the same result.

When a user logs in with either a login or interactive/non-login shell, the user’s .bash_history file is opened. Now can operate on this file with some useful commands. The history is useful to retrieve commands used before but you can need to delete some entries of theses used commands. The ~/.bash_history file does not record what you type in response to other programs’ prompts, just what you type at the bash prompt itself.

Read Also:

Leave a Comment