- View history of commands run in terminal
- How to View Your Command History in Linux
- Scrolling Through Your Previous Commands in Linux
- Viewing Your Command History in Linux
- Reissue a Previous Command
- Enhanced Linux History Search using Grep
- Using a Reverse Search of Linux Command History
- Quickly Reissuing the Previous Linux Command
- Hiding Your Commands from Linux History
- Related Tutorials
- Stay on the Cutting Edge
- How to Use the Linux history Command
- How to Use Linux history Command
- Use Date and Timestamps
- View the Size of the History Buffer
- Repeat a Command
- Search a Command by String
- List the Matching Commands
- Change the Executed Command
- Prevent Recording Commands in History
- Delete History
- Update the History File
View history of commands run in terminal
To clear the history, delete the file and clear the temp history:
rm ~/.bash_history && history -c
The history size defaults to 500 commands. You can, however, increase this by adding a line to your ~/.bashrc file to set the HISTSIZE variable:
This will not take effect immediately, but only to newly started sessions. To apply this, re-source the .bashrc file:
or run HISTSIZE=. in your current session.
By default it only keeps the latest 500 commands though. You can change the number to be kept, but you only have to accidentally start it with the default settings once, and all your older history will be gone.
You can type history on a terminal to view all the previous executed commands.
You can truncate the output to some lines (where 5 is the number of lines):
If do you want to view only commands containing a string (i.e. mv ), you can do this:
You can recall a command by typing ! followed by the entry number.
Let’s say that I have a history like this:
1 ls -la 2 mkdir foo 3 mv bar.txt foo
- To run mkdir foo , you can type !2 .
- To run the last command, you can use !-1 or !!
- To run the penultimate, you can use !-2
If you run a command that fails because it needs root privileges (i.e. touch /etc/foo ), you can use sudo !! to run the last command as root.
- If you type !man you will execute the last command that begins with man
- If do you type !?man? it will execute the last command that contains man (not neccessarily at the line begin)
If do you have a typo in a command, you can fix it this way. Let’s say that I type cat .bash_hi , to replace .bash_hi by .bash_history I only need to type ^hi^history^ .
How to View Your Command History in Linux
Save time by looking back in history to what you typed before.
Remember that one command that solved your problem? Was it cat, less, more, wc or something else? When we’re at the terminal, we can issue dozens of commands to solve a problem and in the background our Linux OS is recording these commands to a history file.
In this how-to we’ll look at various ways of searching and re-using our command history. Whilst you become accustomed to these commands it’s important to double check you don’t unintentionally reissue a command that could cause problems. Take your time using these new techniques and double check the details before pressing enter!
All the commands in this how-to will work on most Linux machines. We’ve used a Ubuntu 20.04 install but you could run this how-to on a Raspberry Pi. All of the how-to is performed via the Terminal. You can open a terminal window on most Linux machines by pressing ctrl, alt and t.
Scrolling Through Your Previous Commands in Linux
The simplest way to look through your recent commands is to use the up and down arrow keys on your keyboard to scroll through the previous commands. If you want to reissue a found command simply press the enter key.
Viewing Your Command History in Linux
The history command, in its most basic use case, lists and annotates the last 1000 commands issued in the terminal emulator. Each command has a number associated with it.
1. Run the history command to see a list of the last 1000 commands. You’ll see that all the listed historical commands are given a unique reference number.
2. Reissue the history command but constrain the amount of results to a specific number. This is useful if you know roughly when a command you are looking for was issued. You should see only the last 20 results listed.
Reissue a Previous Command
Now we can use history to view our previous commands, we can choose and reissue a command using the number assigned to the history results.
1. Run history 20 to create a list of commands, choose a command to reissue making sure that the chosen command is safe to run. Choosing a simple command like cd Music (1660) is a good safe example. Note there is no space between the exclamation mark and the command number.
Enhanced Linux History Search using Grep
By piping the output of history into grep we can perform a search of our command history returning results for a specified term or string. This is an excellent approach for finding a partially remembered command.
1. Search for a specific term using history and grep. We used the example search term “silhouette” as we recalled we had issued some commands to rectify a problem with a silhouette vinyl cutter. Replace that search term with something suited to your machine.
Using a Reverse Search of Linux Command History
Another handy approach to retrieve previous Linux commands is to use the reverse search function built into the terminal. To enter this mode you simply press ctrl and r. You can then enter a search term and use repeat presses of ctrl and r to step back through the list of previous commands containing that term. When you find a command you want to reissue press enter.
1. Press ctrl and r enters the reverse search mode, you should see the prompt now reads (reverse I search)`’:
2. Type a search term and you should see the last command issued that contained this term. For example we added the search term sudo to show the previous commands issued with sudo privileges.
3. Repeat pressing ctrl and r to step through other results.
4. Run a previous command by pressing enter or quit the reverse search by pressing esc .
Quickly Reissuing the Previous Linux Command
Often we will want to simply rerun the last command we issued. We can achieve this simply using the !! command.
1. Run the ls command to set this as the example to test.
2. Reissue the last command using . Note that the previous command is listed and performed.
Sometimes we may try to reuse a command that requires elevated privileges, for example editing a file outside of our home directory. To do this we can preface the previous command with sudo. In the following example we append the first ls command to be reissued with sudo.
Hiding Your Commands from Linux History
There may come a time where you need to keep a command out of your history, and if that scenario ever occurs, all you need to do is preface the command with a single press of the spacebar.
For example here are two ls commands, the second has a single space, hiding it from the history file.
With a little practice all the above approaches become quite instinctive to use and can make your terminal session more powerful and efficient. The ability to locate and reissue commands is extremely useful, especially when recovering a rarely used command or a command that was hard to create in the first instance.
Related Tutorials
Stay on the Cutting Edge
Join the experts who read Tom’s Hardware for the inside track on enthusiast PC tech news — and have for over 25 years. We’ll send breaking news and in-depth reviews of CPUs, GPUs, AI, maker hardware and more straight to your inbox.
By submitting your information you agree to the Terms & Conditions and Privacy Policy and are aged 16 or over.
Jo Hinchliffe is a UK-based freelance writer for Tom’s Hardware US. His writing is focused on tutorials for the Linux command line.
How to Use the Linux history Command
The history command in Linux is a built-in shell tool that displays a list of commands used in the terminal session. history allows users to reuse any listed command without retyping it.
In this tutorial, we will show you how the history command works and different ways to use it.
How to Use Linux history Command
Using the history command without options displays the list of commands used since the start of the terminal session:
To display the command history list with a limited number of entries, append that number to the history command. For instance, to show only the latest five entries, use:
Once you close the terminal, the Bash shell saves new command history entries in the .bash_history file.
Use Date and Timestamps
The .bashrc file stores the Bash shell settings. Modifying this file allows you to change the output format of the history command.
Open the .bashrc file using a text editor such as Nano:
To change the output format to include date and timestamps, add the following line to the .bashrc file:
Note: The blank space before the closed quotation marks prevents the timestamp from connecting to the command name, making the history list easier to read.
Using different arguments after HISTTIMEFORMAT allows you to customize the level of detail in the timestamp:
Save the changes to the .bashrc file, relaunch the terminal, and run the history command to confirm the new output format:
View the Size of the History Buffer
The .bashrc file contains two entries that control the size of the history buffer:
- HISTSIZE : The maximum number of entries for the history list.
- HISTFILESIZE : The maximum number of entries in the .bash_history file.
Editing the HISTSIZE and HISTFILESIZE values changes how the Bash shell displays and saves the command history.
For instance, changing the HISTSIZE value to 10 makes the history command list show a maximum of 10 latest entries.
Saving the changes to the .bashrc file, relaunching the terminal, and running the history command confirms the new output format:
Repeat a Command
Running the history command allows you to reuse any of the commands on the list. For instance, to run the first command ( sudo apt update ) again, use:
Adding a dash (—) before the command number starts the count from the end of the list. For instance, to reuse the tenth last command ( history 5 ), use:
Use double exclamation points to repeat the last command:
Search a Command by String
Adding a string after the exclamation point runs the latest command that starts with that string. For example, to reuse the latest command that begins with sudo , use:
Using this method can cause problems if the shell runs an unexpected command, especially when searching for a command that starts with sudo . As a precaution, adding the :p argument displays the command without running it, allowing you to review the command and decide if you want to execute it.
To search for a command that contains a string, but may not start with it, add a question mark next to the exclamation point. For instance, to reuse the last command that contains echo :
In the example above, the shell reuses the last command that contains the echo string even though the command starts with sudo .
Check out our article on sudo command to learn how to use the sudo command with examples.
List the Matching Commands
Combining history and grep allows you to display a list of commands that contain a string. For example, to list all commands that contain ufw , use:
Note: Learn more about using the grep command in Linux.
Change the Executed Command
Use the following syntax to change the last executed command:
For instance, the ufw command to enable port 20 shows that the port is already enabled:
Use the syntax above to change the port number from 20 to 22:
Prevent Recording Commands in History
To prevent recording commands in the history list, temporarily disable recording by using:
To re-enable recording, use:
Delete History
Use the -d option with the history command to delete a command from the history list. For instance, delete command number 87 with:
Use the -c option to clear the whole history list:
Update the History File
The Bash shell saves any updates to the command history list when you exit the terminal session. The history command also allows you to save changes while in the terminal session.
Using the -a option lets you append the command history entries from this session to the .bash_history file:
Another method is to use the -w option to save the entire history list to the .bash_history file:
After reading this tutorial, you should be able to use the history command in Linux to view, edit, and delete the command history list and reuse commands from it.
If you are interested in learning more about Linux commands, have a look at our Linux commands cheat sheet.