Skip to main content

How to manage your Linux command history

Taking the mystery out of your Bash history.
Image
Managing your bash history
Image by Pete Linforth from Pixabay

You probably know about using the up and down arrow keys to scroll through your Bash history, but did you know that there's a lot more to Bash history than just repeating commands? There is much more to the story. Or, should that be much more to the history? In either case, the history command is one of those obscure commands that is powerful and handy to know on at least a basic level. This article will take the mystery out of Bash history to make it a more friendly sysadmin tool.

The background

In Bash, your command history is stored in a file (.bash_history) in your home directory. The leading (.) makes the file hidden from normal view. To see it, issue the ls -a command.

$ ls -a
.  ..  .bash_history  .bash_logout  .bash_profile  .bashrc

You can operate on the .bash_history file as you would any other plain ASCII text file.

The three related environment variables you need to be aware of are HISTFILE, HISTFILESIZE, and HISTSIZE.

  • HISTFILE—/home/<username>/.bash_history
  • HISTFILESIZE—1000
  • HISTSIZE—1000

The HISTFILE variable holds the name and location of your Bash history file. HISTFILESIZE is how many commands can be stored in the .bash_history file. HISTSIZE is the number of cached commands. Once you reach 1000 commands, the oldest commands will be discarded as new ones are saved.

$ echo $HISTSIZE
1000
$ echo $HISTFILESIZE
1000
$ echo $HISTFILE
/home/khess/.bash_history

Each user, including root, has these variables and sizes assigned by default. If you want to change these variables, edit the .bashrc file in your home directory. For example, if you want to change the size variables to 500 commands, edit the .bashrc file and use the following entries:

export HISTSIZE=500
export HISTFILESIZE=500

To use the new values without logging off and back on again, you can execute the .bashrc file.

$ . ~/.bashrc

Your values are now active for your current shell and any subshells.

$ echo $HISTSIZE
500
$ echo $HISTFILESIZE
500

If you don't want your history to keep duplicated commands, you can instruct your history to ignore duplicate entries by adding the following to your .bashrc file:

export HISTCONTROL=ignoredups

The problem

The problem with Bash history is that it's not written to the .bash_history file until you log off. This makes it impossible to use the history command for scripting. For example, say you're teaching a Linux class, and you want to check to see if students have run a particular command to copy their files to a mounted external drive. If the student stays logged onto the system, you won't be able to determine if the copy has been performed because your script uses their history file (/home/student/.bash_history) to check if the command has run.

The solution

To work around this feature, use the write option for the history command. As part of the class instruction, you'd have the students run this command to save their history even if they don't log off.

$ history -w

This writes all current session command history to the HISTFILE. And now your check script can easily find if the command has been run. You also have to trust that the student will run the history command.

[ Check out Seth Kenlon's history lesson: Make Bash history more useful with these tips ]

Another problem

Since the HISTSIZE and HISTFILESIZE are relatively large (1000 commands), it's possible that the student ran the command before. You can add the export HISTCONTROL=ignoredups entry into their .bashrc, but this won't help if the student ran a similar command two days ago because the file will only show a single command.

The solution to this problem

To work around this problem, you should clear the student's history upon logging on. Edit the .bashrc file again and add the following command to it.

history -c

This command clears their history on each log on and removes the problem of picking up a previously run command. You don't have to worry about picking up a command from previous sessions because the system logs off idle users after a specified period of time if you set the idle time limit in /etc/profile. Edit the /etc/profile file and add the following entry.

TMOUT=300

The TMOUT variable uses a number of seconds of idle time. In this case, 300 seconds is five minutes. You can set this value to any number of seconds. This value will take effect on the next log on for all users. To set individual timeout values, use .bashrc but realize that users may change this file at will. 

[ Download now: A sysadmin's guide to Bash scripting. ] 

Wrap up

Bash history is a handy tool, but it can be frustrating if you aren't familiar with its options and quirks. There are many more options available in history. Check the man pages for further information. And remember that each command you enter is history in the making.

Check out these related articles on Enable Sysadmin

Author’s photo

Ken Hess

Ken Hess is an Enable SysAdmin Community Manager and an Enable SysAdmin contributor. Ken has used Red Hat Linux since 1996 and has written ebooks, whitepapers, actual books, thousands of exam review questions, and hundreds of articles on open source and other topics. More about me

On Demand: Red Hat Summit 2021 Virtual Experience

Relive our April event with demos, keynotes, and technical sessions from
experts, all available on demand.

Related Content

OUR BEST CONTENT, DELIVERED TO YOUR INBOX