The history command is used to list the contents of the ~/. bash_history of the logged in user, and lists the command history lines.
The “~” is a Linux shortcut for HOME directory of the logged in user.
You can pass as a parameter the number of lines you want it to show since the last command entered:
$ history 10 564 cd.. 565 ll 566 cp cadastro.php cadastro_livro.php 567 sudo cp cadastro.php cadastro_livro.php 568 sudo vi cadastro_livro.php 569 mb 570 sudo mb geralivro.php geraflivro.php geralivro.php gerarlivro.php 571 cat grava.php
You can also use history with grep to filter commands:
$ history | grep ssh 144 history |grep ssh 145 ssh uira @192 .30.0.105 148 ssh uira @172 .30.0.107
When you know the line number you want to use again, simply add an exclamation followed by the corresponding line number:
$! 144
The history command has a few options, namely:
Bash also offers other possibilities for working with the command history:
Shortcut | Description |
!! | Execute the last command typed |
!n | Run the command on line n in the .bash_history file |
!text | Run the most recent command that starts with the text |
!?text | Run the most recent command that contains the text |
^text1^text2 | Run the last command changing text1 to text2 |
Alt M < | Go to the beginning of the file .bash_history |
Alt M > | Go to the end of the file .bash_history |
Ctrl p | Retrieve the commands stored in the file .bash_history from back to front |
Ctrl n | Retrieve the commands already listed from front to back |
Ctrl b | Return a character to the commands already retrieved |
Ctrl f | Go one character Forward on the commands already retrieved |
Ctrl a | Return to the beginning of the line in the commands already recovered |
Ctrl and | Go to the end of the line in the commands already recovered |
Ctrl l | Clear the screen |
Ctrl d | Delete characters from the text of cursor to the end of the line |
Ctrl k | Delete the cursor text to the end of the line at once |
Ctrl y Paste | the text deleted by the previous command on the screen |
Ctrl rtext | Search for commands containing the text from the last command to the first |
Ctrl stext | Search for commands that contain the text from the first command to the last |
A very practical use of the command history is the double exclamation!! , which executes the last command typed, especially when it is necessary to re-execute the command with superuser powers with the command “sudo“:
$ apt install lynx
E: Couldn't open the lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?
In this example, an attempt was made to install the lynx package, but apt wouldn’t let it because only root can install software.
To solve the issue, you can simply use sudo with the !!
$ sudo !! Sudo apt install Lynx [sudo] password for Uira: Reading package lists... ready The following NEW packages will be installed: Lynx Lynx-Common 0 packages updated, 2 new packages installed, 0 to be removed, and 57 not updated. You need to download 1,539 kB of files. After this operation, an additional 5,481 kB of disk space will be used. Do you want to continue? [S/n]
The sudo command on Linux executes a given command as if it were another user.…
The ss command on Linux is extremely useful for investigating sockets, providing various information about…
Free Linux command shows the amount of total memory in use and available, as well…
The shell has structures for testing conditions and executing certain program sequences several times (loop),…
The /etc/fstab file stores the configuration of which devices should be mounted and what is…
The Netcat Command in Linux or nc is a utility used to do “almost anything”…
This website uses cookies.