Skip to content

History command on Linux (command history)

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 <strong>ssh</strong> 145 <strong>ssh</strong> uira @192 .30.0.105 148 <strong>ssh</strong> 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:

  • -c Clear the history list
  • -d x Delete the number line x
  • -a & nbsp; Adds the commands from the current bash session to the history file

Bash also offers other possibilities for working with the command history:

ShortcutDescription
!! Execute the last command typed
!nRun the command on line **n** in the .bash\_history file
!textRun the most recent command that starts with the **text**
!?textRun the most recent command that contains the **text**
^text1^text2Run 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 pRetrieve the commands stored in the file .bash\_history from back to front
Ctrl nRetrieve the commands already listed from front to back
Ctrl bReturn a character to the commands already retrieved
Ctrl fGo one character Forward on the commands already retrieved
Ctrl aReturn to the beginning of the line in the commands already recovered
Ctrl andGo to the end of the line in the commands already recovered
Ctrl lClear the screen
Ctrl dDelete characters from the text of cursor to the end of the line
Ctrl kDelete the cursor text to the end of the line at once
Ctrl y Pastethe text deleted by the previous command on the screen
Ctrl rtextSearch for commands containing the text from the last command to the first
Ctrl stextSearch for commands that contain the text from the first command to the last
Bang-bang!! ------------

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 <br></br>E: Couldn't open the lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied) <br></br>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]