Categories: Shell Script

History command on Linux (command history) [Basic Guide]

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:

  • -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 
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]

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Recent Posts

Sudo command on Linux (privilege scale) [Basic Guide]

The sudo command on Linux executes a given command as if it were another user.…

2 years ago

SS command on Linux (investigate the network) [Basic Guide]

The ss command on Linux is extremely useful for investigating sockets, providing various information about…

2 years ago

Free Linux command (memory usage) [Basic Guide]

Free Linux command shows the amount of total memory in use and available, as well…

2 years ago

Linux while command (loop – while) [Basic Guide]

The shell has structures for testing conditions and executing certain program sequences several times (loop),…

2 years ago

Linux fstab file (disk mount setup) [Basic Guide]

The /etc/fstab file stores the configuration of which devices should be mounted and what is…

2 years ago

Netcat command on Linux (Swiss network knife) [Basic Guide]

The Netcat Command in Linux or nc is a utility used to do “almost anything”…

2 years ago

This website uses cookies.