A few words of thanks would be greatly appreciated.

Shell - Accessing last commands in history

The shell Linux/Unix provide several features saving time.

Thus, it is possible to go back in history with the recent orders navigation arrows by pressing the up arrow.

Moreover there are ways to access commands quickly in the history! By typing exclamation point (!) followed by the first letters of the command, the shell automatically runs the last command starting with those letters:

  • Either the following history:
system:/home/user$tail /var/log/syslog 
system:/home/user$vi /etc/apache/httpd.conf 
system:/home/user$/etc/init.d/mysql restart 
system:/home/user$apachectl restart 
system:/home/user$tail /var/log/apache/error.log

The following command allows you to access the command n-4 in the history(vi /etc/apache/httpd.conf)

    • !vi
  • Further application :
    • !! //The last commant
    • !23 //The 23th command
    • !-2 // Execute the last command
    • !?profil //The last command containing the word "profil"
    • ^13^14^ //Substitution in the last command
  • The history of command is registered at each end of session under the file:
    • ~/.bash_history
  • To avoid duplication in this file and history, add the line ~/.bash_profile in the command:
    • export HISTCONTROL=ignoredups
  • You can however display the search command function by pressing CTRL+R

A few words of thanks would be greatly appreciated.

Ask a question
CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jean-François Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.
This document, titled « Shell - Accessing last commands in history », is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).