- 2 Ways to Re-run Last Executed Commands in Linux
- How to use the history command in Linux
- History 101
- Searching history
- Changing an executed command
- Removing history
- Next steps
- 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
2 Ways to Re-run Last Executed Commands in Linux
One of the greatest features of Bash is the command history, which stores all commands a user runs, in a history file within his/her home directory (typically /home/$USER/.bash_history). This allows the user to easily recall, edit and rerun previous commands.
In this article, we will demonstrate how to re-execute a specific command from the history of commands entered to a shell. This is useful to avoid typing the same commands over and over again.
Normally, to get a command you recently run, you can use the Up arrow keys to retrieve a previous command. Pressing it constantly takes you through multiple commands in history, so you can find the one you want. Use the Down arrow to move in the reverse direction.
However, the history file may contain a lot of entries, to re-execute a specific command from the history of commands, you can run the history command.
Then get the number(s) of the command(s) you want to re-execute (if, for example you want to restart PHP-FPM and view its status, you need to re-execute the commands 997 and 998) as shown.
You can also re-execute previously used command (sudo yum update) with ‘!’ character followed by a few of the first characters (for instance sud or sudo) of that particular command as shown.
For more information about Bash history, see these following guides:
That’s all! Bash history is a cool feature that allows you to easily recall, edit and rerun previous commands. If you know other ways to re-excute the last exucuted command do share with us in the comment section.
How to use the history command in Linux
As I spend more and more time in terminal sessions, it feels like I’m continually finding new commands that make my daily tasks more efficient. The GNU history command is one that really changed my work day.
The GNU history command keeps a list of all the other commands that have been run from that terminal session, then allows you to replay or reuse those commands instead of retyping them. If you are an experienced terminal user, you know about the power of history , but for us dabblers or new sysadmin folks, history is an immediate productivity gain.
First of all, the history command isn’t actually a command. You can see this for yourself by looking for the command on your system:
$ which history which: no history in (/usr/local/bin:/usr/bin:/bin:/usr/games:/usr/local/sbin)
Your computer can’t find the history command because it’s a built-in keyword of your shell. Because it’s written into the shell you’re using, there can be some variation in how history behaves depending on whether you’re using Bash, tcsh, Zsh, dash, fish, ksh, and so on. This article is based upon the Bash implementation of history, so some functions may not work in other shells. However, most of the basic functions are the same.
History 101
To see history in action, open a terminal program on your Linux installation and type:
1 clear 2 ls -al 3 sudo dnf update -y 4 history
The history command shows a list of the commands entered since you started the session. The joy of history is that now you can replay any of them by using a command such as:
The !3 command at the prompt tells the shell to rerun the command on line 3 of the history list. I could also access that command by entering:
This prompts history to search for the last command that matches the pattern you provided (in this case, that pattern is dnf) and run it.
Searching history
You can also use history to rerun the last command you entered by typing !! . By pairing it with grep , you can search for commands that match a text pattern or, by using it with tail , you can find the last few commands you executed. For example:
$ history | grep dnf 3 sudo dnf update -y 5 history | grep dnf $ history | tail -n 3 4 history 5 history | grep dnf 6 history | tail -n 3
Another way to get to this search functionality is by typing Ctrl-R to invoke a recursive search of your command history. After typing this, the prompt changes to:
Now you can start typing a command, and matching commands will be displayed for you to execute by pressing Return or Enter.
Changing an executed command
You can also use history to rerun a command with different syntax. You can revise history with history . For example, if I want to change my previous command history | grep dnf to history | grep ssh , I can execute the following at the prompt:
The command is rerun, but with dnf replaced by ssh . In other words, this command is run:
Removing history
There may come a time that you want to remove some or all the commands in your history file. If you want to delete a particular command, enter history -d . To clear the entire contents of the history file, execute history -c .
The history file is stored in a file that you can modify, as well. Bash shell users find it in their home directory as .bash_history .
Next steps
There are a number of other things that you can do with history :
- Set the size of your history buffer to a certain number of commands
- Record the date and time for each line in history
- Prevent certain commands from being recorded in history
For more information about the history command and other interesting things you can do with it, take a look at Seth Kenlon’s articles about parsing history, history search modifiers, and the GNU Bash Manual.
This article was originally published in June 2018 and has been updated with additional information by the editor.
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.