How to check shell history in Ubuntu

Updated: January 28, 2024 By: Guest Contributor Post a comment

Introduction

Checking the shell history in Ubuntu can be quite useful for recalling commands, learning user activity, and for general system administration tasks. The shell or command line records a list of commands that users have entered. In this tutorial, we’ll explore how to effectively check shell history in Ubuntu, using the default bash shell as our primary example.

Basic Commands for Viewing History

To start with the basics, you can simply type the command history in your shell to get a list of previously executed commands:

$ history

The output will show a list of commands with an associated number for each command entry. It usually looks something like this:

  997  ls
  998  cd /var/log
  999  tail syslog
 1000  sudo apt-get update

The history command is useful, but if you’re interested in only the most recent entries, you can limit the output with a number:

$ history 5

This command outputs the last 5 commands you executed:

  996  cat /etc/hostname
  997  ls
  998  cd /var/log
  999  tail syslog
 1000  sudo apt-get update

Searching Through History

Searching through your history can be accomplished using the grep command. For example, to search for all instances of ‘apt-get’, you would run:

$ history | grep apt-get

The output will list all lines from your history containing ‘apt-get’. It might look like this:

  500  sudo apt-get install nginx
  600  sudo apt-get remove nginx
 1000  sudo apt-get update

Advanced History Commands

Focusing on more advanced usage, the shell history in Ubuntu has more capabilities. You can run commands directly from your history list by referencing their associated number using the following syntax:

$ !997

This will execute command number 997 from your history (‘ls’ if we refer to the examples above).

If you want to repeat the previous command you can abbreviate this to:

$ !!

You can also edit a previous command before running it, which can be useful if you’ve made a typo. Use the fc command:

$ fc 997

This will open command number 997 in your default text editor, allowing you to edit and then execute the command upon saving and exiting.

Managing Your History

It’s possible to exclude commands from your shell history using the HISTIGNORE environment variable. For example, you might not want to history to include any command that starts with a space. To set this rule, you can place the following in your ~/.bashrc:

HISTIGNORE=' *:ls'

After updating your .bashrc, execute source ~/.bashrc to apply changes. Now, any command entered with a space at the beginning won’t be recorded in your history.

Automating History Searches

For automated or programmatic access to the history, one way is to use shell scripting. You can create a simple shell script that searches your history and outputs results to a file. Here’s a quick example:

#!/bin/bash
history | grep "$1" > history_search_results.txt

After saving and making that script executable with chmod +x yourscript.sh, you can run ./yourscript.sh search_term to output a filtered history to a text file.

Persisting History Across Sessions

In a default setup, your shell history is saved in a file called .bash_history in your home directory. If you want to expand history so it’s shared across sessions in real-time, you can manipulate the PROMPT_COMMAND and shopt settings in .bashrc:

PROMPT_COMMAND="history -a;$PROMPT_COMMAND"
shopt -s histappend

The first command appends every command to the history file after execution, and the histappend option combines session histories together rather than overwriting them.

Guarding Your History

Shell history can contain sensitive information, such as passwords passed as arguments (which is not a good practice). You can clear your entire command history using the command:

$ history -c

To remove a specific entry from the bash history, you can use the -d option along with the entry line number:

$ history -d 1000

This would remove the entry with the number 1000. However, be aware that your .bash_history will not be cleared until you exit the shell or execute history -a.

Conclusion

Understanding how to utilise the shell history feature in Ubuntu will provide you with not only a handy command log but the flexibility to retrace and reproduce your command sequence efficiently. With practice, these skills can significantly speed up your command line proficiency.