Terminal History Manipulation

 BASH (Bourne Again SHell) is the default command line shell used in Ubuntu and Kubuntu and many other Linux distributions. BASH allows you view and manipulate the command line history.

The following examples are from Kubuntu Konsole

List History

user@hostname:~$ history
1 sudo restart
2 sudo restart --help
3 reboot
4 sudo reboot
.
..
...
709 history
user@hostname:~$

List last 10 lines of history

user@hostname:~$ history 10
700 dmesg | less | grep error
701 sudo nano /etc/modprobe.d/alsa-base.conf
702 history
703 ls
704 cd Annex
705 cd Annex 1
706 cd Music
707 cd ..
708 echo $PROMPT_COMMAND
709 history 10
user@hostname:~$

Search History

Search history for lines containing “fastboot”

user@hostname:~$ history | grep fastboot
170 sudo apt-get install android-tools-fastboot android-tools-adb
172 fastboot ?
175 fastboot devices
176 fastboot getvar ?
177 fastboot reboot
178 fastboot devices
183 fastboot devices
184 fastboot reboot
187 fastboot devices
516 fastboot oem get_identifier_token
518 fastboot oem get_identifier_token
520 fastboot oem get_identifier_token
711 history | grep fastboot
user@hostname:~$

Run Previous Command

!n where n is the command line number

user@hostname:~$ !138
lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.3 LTS
Release:        14.04
Codename:       trusty
user@hostname:~$ 

Note that the first line of output echoes the command itself

For more information see Chris Jean’s excellent blog¬†Command Line History

 

Leave a Reply

Your email address will not be published. Required fields are marked *