Pin Me

Linux Command Line: history

written by: jlwallen•edited by: J. F. Amprimoz•updated: 7/4/2011

The Linux operating system has thousands of commands of which you will use many. Knowing what commands you've used in recent history can help you administer a desktop or server. The "history" command is a tool that will show you what commands you have used when your memory fails.

  • slide 1 of 4

    What "history" does

    So far in the Linux Command Line series we have covered a lot of ground. In just a short time you have read about more commands than you can probably remember. After administering a Linux machine you might have trouble remembering which commands have been run on a machine. Or, as an administrator, you might want to know what commands each user has issued. That is where history comes in. The bash shell keeps a list of the commands issued. The number of commands retained is quite large. This number, naturally, can be cleared. The history command can also be configured to only retain a set amount of commands. By setting the number of commands history remember you can avoid certain security issues. Say, for instance, you accidentally leave your root user logged in and you have just issued a specific iptables rule chain. A user could come by, issue the history command, and know what the iptables rule is (thereby having a foot in the door of your server.)

  • slide 2 of 4

    Basic Usage

    The history command is issued from a terminal emulator such as aterm, eter, or xterm. If you issue the command without any arguments you will see something like this:

    485 su

    486 cd Desktop/

    487 ls

    488 sudo mv wireframe-DR16.tar.gz /usr/share/e16/themes

    489 cd /usr/share/e16/themes/

    490 sudo mkdir WIRE

    491 sudo mv wireframe-DR16.tar.gz WIRE

    492 cd WIRE/

    493 sudo tar xvzf wireframe-DR16.tar.gz

    494 exit

    495 cd .e16/menus/

    496 nano user_apps.menu

    497 exit

    498 gnome-control-center

    499 kilalll -9 thunderbird

    500 exit

    501 history

    502 man history

    503 history

    This would be the last 19 commands issued from my terminal. To be able to go through the entire list pipe, the history command through the less command enter:

    history | less

    Which would allow you to scroll one page at a time through the history list.

  • slide 3 of 4

    More Usage

    Here are some other ways to use history.

    history -c: Clears the history buffer.

    history -a: Append the new history lines to the ~/.bash_history file. The new lines are those that have been entered since the newest bash session started.

    history -w: Write out the current history to the ~/.bash_history file.

    The above list is really the most useful of the arguments.

  • slide 4 of 4

    Final Thoughts

    Keeping tabs on your history file can, in certain circumstances, mean the difference between a secure system and an insecure system. Making a habit of clearing your bash history can keep prying eyes from knowing what commands you have issued. So keep that history cleared on a regular basis.

Keeping Your Linux Machine Clean

If you have used a PC for any length of time you know that a hard drive can very quickly become cluttered with files, remains of uninstalled applications, and disorganized files and directories. In this Brighthub series you will learn the tools to keep your Linux system clean.
  1. Keep Your Linux Box Clean with These Tools
  2. Linux Command Line: cruft-remover
  3. Linux Command Line: history
  4. Linux Command Line: rm
  5. Linux Command Line: locate