Mastering the Linux History Command: A Beginner’s Guide with 8 Practical Examples

If your work involves running tools and scripts on the Linux command line, you’ll undoubtedly execute numerous commands daily. New users should note there’s a helpful tool named history, which provides a list of commands you previously executed.

This tutorial will cover the basics of the history command with easy-to-grasp examples. Note that the examples provided were tested on an Ubuntu 24.04LTS machine.

Linux History Command

Effective use of your command line history can save significant time daily. Below are several Q&A-style examples to help you harness the power of the history command.

Q1. How does the history command work?

Basic usage is straightforward—execute the ‘history’ command.

history

How does the history command work

Your previously executed commands will display. Note the use of the ‘more‘ command to pause output display, as the complete ‘history’ output is often lengthy.

Q2. How can history display the date and time of each command?

To have the history command show the date and time of each execution, export the HISTTIMEFORMAT variable:

export HISTTIMEFORMAT="%F %T: "

Now when you run the ‘history’ command, the date and time will appear alongside each command.

How to make history display date and time

If all commands appear with the same timestamp, refer to this explanation:

If you set the HISTTIMEFORMAT in bash, new entries will include timestamps. Older commands without timestamps (those prior to setting HISTTIMEFORMAT) will show a uniform date-time-stamp (typically the first timestamped entry). This problem resolves itself within a few days once your complete history updates. Check ~/.bash_history for lines starting with a # followed by a timestamp.

Q3. How to stop history from recording commands?

To prevent the system from saving your command history, set the HISTSIZE variable to zero:

How to make history stop recording commands

Notice that running ‘history’ now produces no results.

Additionally, there’s a HISTFILESIZE variable. See the difference between the two:

HISTSIZE is the number of lines or commands stored in memory during your current session. HISTFILESIZE denotes the number of lines or commands allowed in the history file at session startup and stored there for future sessions.

Q4. How to exclude a single command from history recording?

Set the HISTCONTROL variable to ‘ignorespace’. Run:

export HISTCONTROL=ignorespace

To exclude a command from history, execute it with a leading space.

Q5. How to perform a quick history search?

While you can use the grep command to search specific keywords or commands, a faster method is reverse-i-search. Press CTRL+R at the command line to initiate, converting your prompt to:

Type your keyword, and you’ll see matching historical commands. Typing ‘man’ (without quotes) returned ‘man apropos’.

If not satisfied, press CTRL+R again for the next match. For example, ‘man find search’ might appear next.

Once the desired command surfaces, press the right arrow key to place it on your prompt:

You can then execute or modify the command as needed.

Q6. How to rerun the previous command?

Quickly execute the last command using CTRL+P or the following:

!!

Need to rerun the second last command? Use:

!-1

Q7. How to avoid duplicate entries in history output?

Set the HISTCONTROL variable to ‘ignoreboth’ and ‘erasedups’:

echo HISTCONTROL=ignoreboth:erasedups

Running the history command will then omit duplicates.

Q8. How to clear history?

To clear existing history and start fresh, use:

history -c

Conclusion

The history command serves as a vital productivity tool for Linux users. We’ve explored several key tips and tricks to optimize its use. Once comfortable, consult the tool’s man page for further information.

Frequently Asked Questions (FAQ)

What does the ‘history’ command do?

The ‘history’ command in Linux displays a list of commands previously executed in the terminal. It records these commands for reference and efficiency.

How can I enable timestamping for my command history?

By setting the HISTTIMEFORMAT variable, you can enable command timestamping. Use the command export HISTTIMEFORMAT="%F %T: " to include date and time details in your history.

Is it possible to delete specific commands from history?

Yes, you can delete specific commands by editing the ~/.bash_history file directly, or by using the history command number (e.g., history -d [command_number]).

How do I prevent certain commands from being recorded in history?

By setting HISTCONTROL=ignorespace and prefixing commands with a space, you can exempt them from being recorded in your history.

How can I perform a quick search in history?

Press CTRL+R to trigger the reverse-i-search feature, allowing you to search through your command history interactively.