fbpx Linux history Command: View and Manage Command History | Certificação Linux

Linux history Command: View and Manage Terminal History

📅 01/01/2019 ⏱ 4 min ✍️ Uira Ribeiro
Linuxcommandshow-to
Linux history Command: View and Manage Terminal History
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

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
!nExecutes the command on line n in the .bash_history file
!textoExecutes the most recent command that starts with the text
!?textoExecutes the most recent command that contains the text
^texto1^texto2Executes 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 pRetrieves commands stored in the .bash_history file backwards
Ctrl nRetrieves listed commands forwards
Ctrl bMoves back one character in retrieved commands
Ctrl fMoves forward one character in retrieved commands
Ctrl aGoes back to the beginning of the line in retrieved commands
Ctrl eGoes to the end of the line in retrieved commands
Ctrl lClears the screen
Ctrl dDeletes characters from cursor text to the end of the line
Ctrl kDeletes text from cursor to the end of the line at once
Ctrl yPastes the text deleted by the previous command on screen
Ctrl rtextoSearches for commands containing the text from the last command to the first
Ctrl stextoSearches 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

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp