history -w command in Ubuntu

In this article, we cover the basics of history -w command in Ubuntu. Everything we type in the Shell ultimately gets stored in BASH history file. If you want to know the location and name of the BASH history file then, issue the following command in terminal –


If you or your System Administrator haven’t made any changes regarding BASH history file then, the file for you would be –


So, what happens is that – the commands we enter in Shell are stored in Cache. And, they are not written to BASH file till we logout. Once we logout, BASH file gets written. And, next time we log in to Shell – we see the commands previously entered. Use Up and Down arrow keys to access the previously entered commands in BASH Shell. But, there is more to it. We will cover that separately.

Now, what if we could just write all the current session commands in our BASH file without logging out. Yes, there is a way out and this is what we are about to discuss next.

history -w command in Ubuntu

So, type following two commands in BASH shell –

ls -l

Just basic commands to list files and directories. And, now check whether these two commands have been written in your BASH file.

cat /home/$USER/.bash_history

It would show you the previously entered commands. Not the current ones. Now, to have these two commands written in BASH history file –

history -w

Now, again check whether these two commands are now written in BASH history file –

cat /home/$USER/.bash_history

This time around you would see all the commands in BASH history file you have entered in current session.

So, what history -w does is, it help us write the current session commands stored in cache to the BASH history file.

In conclusion, we have covered history -w command in Ubuntu. In next article, we cover history -c command, which is used to clear current session history.

Similar Posts