How to use the SSH history command

The SSH history command displays the commands you have previously run in your current shell session. In particular, it’s useful for server monitoring, admin auditing, and recalling a long or complex command you used earlier. Furthermore, bash stores your history in ~/.bash_history, so you can also inspect that file directly. The history command works in bash and other common shells on Linux servers via SSH.

Display the last commands run

To see your command history:

history

This lists all commands from the current session plus those loaded from ~/.bash_history. Each line is numbered so you can reference or re-run a command. To show only the last 20 entries, use history 20.

Clear your command history

To clear the history in your current session:

history -c

This removes the in-memory history. Note that history -c does not erase ~/.bash_history—that file is updated when you exit the shell. To wipe the file as well, run history -c and then echo "" > ~/.bash_history before logging out.

Search for a command with grep

To find a specific command in your history, pipe history into grep:

history | grep tar

Replace “tar” with any term you want to search for—for example, history | grep mysql or history | grep wp-config. See our guide on the SSH grep command for more filtering options.

Re-run a command by number

You can re-execute a command from history using its number. Type !123 to run command number 123, or !-1 to run the last command again. Use !! as a shortcut for the previous command.

Common use cases

Typical uses for SSH history command on a Linux hosting account:

  • Recalling a long tar, grep, or SSH command you ran earlier
  • Auditing what commands were executed on a server
  • Finding the exact syntax of a command that worked
  • Cleaning sensitive commands from history before sharing a session

For viewing and editing files, see the SSH cat command. For listing files, see the SSH ls command.

Need more help?

Explore more SSH and hosting guides in our knowledgebase. JetHost web hosting includes SSH access for managing your server directly.