Linux history Command: View and Manage Terminal History
The history command is used to list the contents of the logged-in user's ~/.bash_history, and it numbers the command history lines.
"~" is a Linux shortcut for the logged-in user's HOME directory.
You can pass the number of lines you want it to show since the last typed command as a parameter:
$ 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 mv geralivro.php geraflivro.php 570 sudo mv 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 [email protected] 148 ssh [email protected]
Once you know the line number you want to use again, just put an exclamation mark followed by the corresponding line number:
$ !144ssh -i uribeiro.pem [email protected] login: Thu Sep 12 09:51:33 2019 from 172.30.0.241
The history command has some options you should know:
- -c Clears the history list
- -d x Deletes line number x
- -a Adds the commands from the current bash session to the history file
Bash also offers other possibilities for working with the command history:
| Shortcut | Description |
| !! | Executes the last typed command |
| !n | Executes the command on line n in the .bash_history file |
| !texto | Executes the most recent command that starts with the text |
| !?texto | Executes the most recent command that contains the text |
| ^texto1^texto2 | Executes the last command replacing texto1 with texto2 |
| Alt M < | Goes to the beginning of the .bash_history file |
| Alt M > | Goes to the end of the .bash_history file |
| Ctrl p | Retrieves commands stored in the .bash_history file backwards |
| Ctrl n | Retrieves listed commands forwards |
| Ctrl b | Moves back one character in retrieved commands |
| Ctrl f | Moves forward one character in retrieved commands |
| Ctrl a | Goes back to the beginning of the line in retrieved commands |
| Ctrl e | Goes to the end of the line in retrieved commands |
| Ctrl l | Clears the screen |
| Ctrl d | Deletes characters from cursor text to the end of the line |
| Ctrl k | Deletes text from cursor to the end of the line at once |
| Ctrl y | Pastes the text deleted by the previous command on screen |
| Ctrl rtexto | Searches for commands containing the text from the last command to the first |
| Ctrl stexto | Searches for commands containing the text from the first command to the last |
Bang-bang !!
A very practical use of command history is the double exclamation mark !!, which executes the last typed command, especially when you need to run the command again with super-user powers using the "sudo" command:
$ apt install lynxE: Não foi possível abrir arquivo de trava /var/lib/dpkg/lock-frontend - open (13: Permissão negada)E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?
In this example, we tried to install the lynx package, but apt didn't allow it because only root can install software.
To solve this, you can simply use sudo with !!:
$ sudo !!
sudo apt install lynx
[sudo] senha para uira:
Lendo listas de pacotes... Pronto
Os NOVOS pacotes a seguir serão instalados:
lynx lynx-common
0 pacotes atualizados, 2 pacotes novos instalados, 0 a serem removidos e 57 não atualizados.
É preciso baixar 1.539 kB de arquivos.
Depois desta operação, 5.481 kB adicionais de espaço em disco serão usados.
Você quer continuar? [S/n]
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Liked it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.