How To Use ‘history’: The Complete Linux Command Guide
History is a command-line tool on Linux that comes in handy when you’re working in a terminal. Many of its best features, however, are hidden because it’s not intuitive or readily apparent. In this article, I’ll show you how to get the most out of history.
The ‘history’ command on Linux is used to look up commands that have been executed in the past. It stores these previously used commands in a list so they can be searched for and re-run.
We’ll start by discussing the most common ways ‘history’ is used. Read on to learn tips and tricks that will take you to the next level of terminal mastery.
Command Syntax
The ‘history’ command has a syntax that typically looks like this:history <options> <arguments>
It starts with the main command and is followed by these optional parts:
- <option>: These command-line options, or flags, lets you manipulate ‘history’ to perform different tasks. We’ll review a few useful examples followed by in-depth coverage below.
- <arguments>: Most ‘history’ features won’t require you to provide arguments, but there are a few options that will require extra input.
Usage Examples
Syntax on its own is always a little hazy at first, so let me show you examples that will let you immediately take advantage of the ‘history’ command.
View Recently Used Commands
The core use of ‘history’ is to pull a chronological list of your previously used commands. Maybe you want to remember a program you’ve executed recently or what the exact syntax was. To see your history list: history
My history list outputs about 300 entries, but your system might default to a smaller or bigger list. You can scroll up to view all of them.
But if that’s too much info, you can reduce the output. For example, if you want to see your last ten commands only: history 10
If you’re new to the Linux command line, this article will give you the most important Linux commands to know, plus a free downloadable cheat sheet to keep handy.
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now
Repeat the Last Used Command
Perhaps the most essential benefit of ‘history’ is that you can repeat commands with minimal typing. For example, to re-run your last used command, all you need to enter is: !!
In the example above, I wanted to update system packages but had to cancel the download. When I was ready to resume, I issued “!!” to rerun the previous command. Even if you’re a fast typer, you’ll be grateful for this shortcut when repeating much longer commands.
Run a Specific Command From History
Next up is my personal favorite. What if you don’t want to run the last command, but a different one from your history? All you need to do is specify which line entry you want to run:!<number>
In the example above, I pulled up my history list and identified that the command I wish to run is number 284. Entering !284 in my terminal executes this command again without retyping the details.
Join Our Community!
Connect, learn, and grow with other Raspberry Pi enthusiasts. Support RaspberryTips and enjoy an ad-free reading experience. Get exclusive monthly video tutorials and many other benefits.
Learn moreSearch for a Command in History
What if you only know part of your command but don’t feel like scrolling through hundreds of lines of history? You can search your history list by using a keyboard shortcut: CTRL+R.
In this example, I hit CTRL+R, and the “reverse-i-search” prompt appears (Reverse-i-search incrementally searches backward from the most recent command). When I type “ip,” it auto-populates with a match from my history. That’s what I was looking for, so I hit Enter to run it.
But what if the first match isn’t what you want? Hit CTRL+R again to cycle to the next match. Repeat until it reaches the command you were looking for. Hit Enter to run the command, but if you decide not to run anything, CTRL+G will exit instead.
Typing ‘history’ to view all past commands or searching with CTRL+R is probably good enough for your everyday needs. However, if you still prefer an alternate method, you could always pipe your history list through the trusty grep command: history | grep -i <searchterm>
This is what I generally use, as I find it more predictable than the CTRL+R shortcut.
I like to use -i to broaden the search by making it case-insensitive. In the example above, I used grep to search my history for the times wget was used to download a file.
Clear Current History
There comes a time in every Linux user’s life when his or her history has become a cluttered mess. In that case, you can always clear your history: history -c
Command-Line Options
We’ve reviewed the most common uses, but ‘history’ can do more. To understand the options below, it’s important to know that there’s a temporary history list (in memory) from your current session written to a persistent history file (on disk) when you log out.
Here’s a run-down of its command-line options:
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now
- -c: Clears the user’s history list.
- -d OFFSET: Deletes the history entry at the specified line number (OFFSET).
- -a: Appends the newest commands from your current history list (in memory) to the history file (on disk). This option makes this process happen immediately instead of waiting until you log out.
- -n: Appends new commands from the history file to your current history list. This option is like the reverse of -a.
- -r: Reads the history file (on disk) and adds its contents to your current history list (in memory). Like -n, except -n appends, whereas -r overwrites your current history list.
- -w: Writes the current history list to the history file. Like -a, except -a appends, whereas -w overwrites the history file.
- -p ARGS: Displays what a history command (ARGS) would output without saving it to history.
- -s ARGS: Single entry addition to history. This option lets you manually add a command you specify (ARGS) to your history list without executing it.
These cover the main options, but if you want to read everything else there is to know, you could always peruse its official documentation: man history
Tips
If you’ve gotten this far, here are a few neat tricks with ‘history’ to make using the terminal even more convenient.
Cycle Through Your History Using Arrow Keys
A keyboard shortcut can auto-populate the command line from your history list.
Push the up/down arrow keys to cycle through your history.
Run the Previous Command With Root Privileges
Don’t you hate when you forget to run a command with sudo? Here’s a shortcut to repeat the command with elevated privileges: sudo !!
Use ALT + Period to Bring Up the Previous Argument
What if you just carried out a command on a directory/file/etc but now want to carry out a different command on the same target? To auto-type the argument from the previous command, use the keyboard shortcut: “ALT+.“.
In this example, I first use cat domains.sql to read a file but the file is too long and scrolls for days. Therefore, I type less followed by ALT+. to retype the last argument, domains.sql, for me.
Okay, I admit that this particular example saves only a little bit of time, but this shortcut’s usefulness becomes more obvious when you’re working with long directory trees or complex operations.
Tag a Command to Find It Later
If there’s a command you often forget the syntax of, you can make it easier to find by giving it a human-readable label. Simply add a hashtag comment to the end of the command:command #<tag>
In the example above, I’ve tagged my tar command with: #backup. The next time I need this command, I can hit CTRL+R to search for “backup” and find it again quickly.
Change History Size
If you find your history list lists too few or too many entries, change its size by specifying how many you’d like to store:export HISTSIZE=50
Remove Duplicate Commands
If you have a lot of duplicate commands in your history and want to clean it up, you can erase just the duplicates: export HISTCONTROL=erasedups
Notice from the example above that the duplicate entries of the ls command only get erased from the history list once a new duplicate has been added.
Save Your History List to a Separate File
I like to keep a file in my home directory with the commands I need for server administration. If you’d like to create your own cheat sheet, you can save your history list to a separate text file: history > cmds.txt
Edit the History File Manually
Your system automatically writes your history list from your current terminal session to a history file when you log out. If you feel like modifying this file, you can use a text editor like nano to edit it:nano ~/.bash_history
Alternatives
The Fix Command: fc
The ‘fc’ utility, sometimes referred to as the “fix command,” can be used as an alternative to the ‘history’ command. Although I don’t have a need for ‘fc’ at all, one common use is to filter a subset from your history list:fc -l <start> <end>
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now
Related Questions
How Do I Enable or Disable History?
Turning off your history makes it so that your commands aren’t saved: set +o history
To turn your history back on: set -o history
How do I Stop a Command That’s Currently Running?
Hit CTRL+C to cancel a command in progress. This can be useful if you need to grab some data first, and then you can use your history to run the command again. Or, if a command is taking too long, you can cancel the command and use screen to run the command in the background.
What Are the Security Implications of Using History?
The ‘history’ command can be helpful when auditing the security of your Linux system. You can review your history to check if suspicious activity—like reading the sudoers file or downloading exploits—was performed by your user, root, or other users.
However, this capability can go the other way, too. If your user account has been compromised, an attacker can pull up your history list to find out what you’ve been up to recently. That’s why you may want to delete specific entries or clear your history after running sensitive commands.
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now
Want to chat with other Raspberry Pi enthusiasts? Join the community, share your current projects and ask for help directly in the forums.
Additional Resources
Overwhelmed with Linux commands?
My e-book, “Master Linux Commands”, is your essential guide to mastering the terminal. Get practical tips, real-world examples, and a bonus cheat sheet to keep by your side.
Grab your copy now.
VIP Community
If you just want to hang out with me and other Linux fans, you can also join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
More details here.
Need help building something with Python?
Python is a great language to get started with programming on any Linux computer.
Learn the essentials, step-by-step, without losing time understanding useless concepts.
Get the e-book now.