What is bash history?
Bash is the program that reads the commands you type in a Linux terminal. A terminal is a window where you type instructions to the server. Bash keeps a list of the commands you ran before. That list is the history. It is like the history list in a web browser. It helps you repeat old commands and see what happened on a server.
The list is saved in a hidden file called .bash_history in your home folder. Each user has their own.
Steps to view it
- Connect to your server with SSH, or open the terminal.
- Type this and press Enter:
history
This prints your past commands with a number beside each one.
- To see only the last 20, type:
history 20
- To search for a word, such as
nano:
history | grep nano
The | sign passes the list to grep, which keeps only the lines that match.
Handy shortcuts
- Press the Up arrow to bring back the last command.
- Press Ctrl + R and type a few letters to search old commands.
- Type
!42to run command number 42 again. - Type
!!to run the last command again.
Show the time of each command
- Run this:
export HISTTIMEFORMAT="%F %T "
This adds a date and time to each line shown from now on.
- Run
historyagain.
Reviewing another user's history
If you have root access, you can read another user's file. Replace username with theirs:
cat /home/username/.bash_history
This helps when you check for strange activity on a server.
Clearing history
Warning: this deletes the list for good.
history -c
This clears the list in your current session. The saved file may still hold old lines. Deleting it with rm ~/.bash_history removes those too.
Quick recap
- Bash history is a list of commands you ran.
- Type
historyto see it. - Use grep or Ctrl + R to search it.
- It is stored in
~/.bash_history.