Linux command line – The terminal, like all shells, stores a history of commands executed by the user in the past. This story is constant and remains in memory even after rebooting our system. These saved commands can be recalled and reused by us in the most optimal way for the effective use of the function of preserving the history of most shells.
Linux bash comes with a very powerful team called history. This command is a built-in bash command used to retrieve historical information about the commands that Linux users executed in all previous sessions. In this guide, we will give you the opportunity to make the most of your shell history by learning how to use the history command correctly. Linux stores the command history for a specific user in the default ~ / .bash_history file.
We will use the Terminal Linux application to run the commands mentioned in this guide.
View full story
A user can view the entire history of their shell commands from a history file saved exclusively for that user using the following simple command:
The story will be printed on the screen in the following default format:
In the image above you can see a list of all the teams with numbers assigned to each of them. Command number 1 corresponds to the first command you executed, and the last numbered command represents the last command you executed.
If, after starting bash, you executed many commands, you will notice that hundreds and even thousands of these commands will be displayed here. To access several relevant commands, you can filter the results displayed by the history function. You can also customize the story team to show a specific number of teams for you.
History output filtering
If you want to filter the history output based on a specific keyword that you could use in previous commands, you can use the history command as follows:
$ history | grep
Example: In this example, I want to view only those ping commands that I have ever run in a shell. Therefore, I will use the following command:
$ story | grep ping
You can see that my output now only displays commands, including my search keyword “ping”
View a number of recent commands
You can also view a certain number of commands, for example:
- N number of recent teams
- N number of oldest teams
N Number of recent teams
You can view a certain number of recently launched commands with the following command:
$ history | tail -n
In this example, I want to view the last 3 commands that I executed with the keyword “ping”:
$ history | grep ping |tail -3
The output only shows the last 3 ping commands instead of the whole story
N Number of oldest teams
You can view a certain number of the oldest run commands with the following command:
$ history | head -n
In this example, I want to view the oldest 3 commands that I executed with the keyword “ping”:
$ history | grep ping |head -3
The output displays only the 3 oldest ping commands instead of the whole story
List all commands with date and time
If you want to view the command history along with the date and time stamp, use the following export command:
$ export HISTTIMEFORMAT='%F, %T '
Now that you want to view the story, just run the story command to see the output in the following format:
Please note that this format change is temporary and will be restored to the previous standard when closing the session.
History Team Navigation
While you are on the command line, you can navigate through previously executed commands, as well as call some recently used commands, providing a keyword.
You can use the following keyboard controls to navigate history commands
|Up Arrow / Ctrl + P||Using these controls, you can display the previous command that you used. You can click these controls several times to return to a specific command in the story.|
|Down arrow / Ctrl + N||Using these controls, you can display the following command that you used. You can click these controls several times to jump to a specific command in the story.|
|Alt + R||If you are editing a command that you extracted from history in the current line, you can use this control to return it to the original command.|
You can remember whether or not to run a specific command from a story using the following controls:
|Ctrl + R||Using this control, you can call a command from the history by specifying a search string.|
|Ctrl + O||Using this control, you can run the command that you called via Ctrl + R|
|Ctrl + G||Using this control, you can exit the history without executing the command that you called via Ctrl + R|
In this example, I pressed Ctrl + R and then provided the search string “ar”. The “arch” command is displayed in the story.
When I executed the “arch” command by pressing ctrl + O, it displayed the processor architecture of my system:
Using Bash History
The true magic of bash history is to use various commands and configure the history command itself to make the best use of the history function:
Running teams from history
- As we have seen, the history command displays commands, associating a number with each. Using the following command, you can run a specific command from the history based on its number:
Here I run command number 95 from my story:
The output displays the result of “lscpu”, which was indicated as command number 95 in my bash story. This command displays the characteristics of my processor and operating system.
- You can re-run the previous command using the following command:
Here you can see that I executed the command “arch”. By running the above command, I can re-run the “arch” command to view my processor architecture as follows:
- To find a command from history and then run it, use the following command:
I will use the following command to re-run the last command with the keyword “dconf”:
In the following image, you can confirm again that this was my last dconf command.
Using arguments from previous commands
Linux bash allows you to run a new command using the arguments of the previous command. This is especially useful when you want to avoid retyping long or complex arguments. For this purpose you can use the following command:
Using the last argument from the previous command
$ [Command] !$
In a variable! $ The last argument of the previous command is stored.
In this example, I will create a file with a long name.
Then I will copy it to another location, avoiding re-entering the long name as follows:
Cp !$ [destinationfolder]
You can see that I was able to copy the file without re-entering the file name using the variable! $
Using the first argument from the previous command
Sometimes only the first argument from the previous command is useful in the current.
In this case, you can use the following command:
$ [command] !^
In the following example, I ran the ping command, specifying the host name and number as arguments. Now I want to use the hostname (first argument) instead of 2 (last argument) in my new command. I will use the following command for this purpose:
$ ping !^
Using all the arguments from the previous command
You can also use all the arguments from the previous command using the following command:
Using one argument from the history command
You can also use commands from your history by reusing their arguments. To do this, use the following command:
$ [command] !:#
$ echo !cp:2
This command will take the following parameters:
#: 2 (second argument to the command)
The following output shows the second argument to the last cp command that I ran:
Using all arguments from the history command
The following command will save all the arguments from the history search command and use them in the current command:
$ command !:*
In the following command, I will print all the arguments of the last cp command using the echo command as follows:
$ echo ![cp]:*
Modify and run previous commands
If you typed the command incorrectly or want to restart the command by changing any text in it; Here is a way to do it:
I typed the “grep” command as “gep” and now I want to run the command, correcting the text:
Now you can see that my corrected command will work correctly as follows:
To erase the history of your bash, use the following command:
$ history -c
This command will clear your history and delete all contents from the ~ / .bash_history file
In this lesson, we learned that while listing all the contents of a story might not be very useful, optimizing the output with the commands we learned is a real change to the game. Practice along with this article will allow you to view the exact commands you want, re-run and reuse these commands, and use the arguments from previously executed commands to optimize new ones.
Getting the best of Linux Bash history