Skip to content

Practice Exercise: Mastering Command Line History and Recall

Objective

Learn to efficiently navigate and utilize the command line history for increased productivity in a Linux environment.

Task 1: Basic Command History Navigation

  • Open a terminal window.
  • Execute a few simple commands in the terminal.
  • Use the history command to display a list of recently executed commands, along with their corresponding numbers.
  • Use the !n command, where n is the command number from the history list, to quickly rerun a specific command.
    [intern@intern-a1t-inf-lnx1 ~]$ history
        1  ip a
        2  man apt
        3  unminimize
        4  sudo unminimize
        5  ip a
    [intern@intern-a1t-inf-lnx1 ~]$ !1
    ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
        inet6 ::1/128 scope host
           valid_lft forever preferred_lft forever
    

Task 2: Command History Expansion

  • Execute several commands that involve file operations, like creating, moving, or deleting files.
  • Use the !! command to rerun the last command executed.
  • Utilize the !$ and !* shortcuts to access the last argument or all arguments from the previous command.
  • Experiment with the !string syntax to rerun a command that starts with a specific string.
    [intern@intern-a1t-inf-lnx1 ~]$ cd /home/projects
    [intern@intern-a1t-inf-lnx1 ~]$ !!
    cd /home/projects
    [intern@intern-a1t-inf-lnx1 ~]$ ls !$
    ls /home/projects
    project_plan.txt
    [intern@intern-a1t-inf-lnx1 ~]$ echo /home/projects /home/intern
    /home/projects /home/intern
    [intern@intern-a1t-inf-lnx1 ~]$ ls !*
    ls /home/projects /home/intern
    /home/intern:
    '#emacs_code.c#'   emacs_code.c~   incremental.snar             incremental_backup2.tar.gz
     bin               go              incremental_backup           installer_linux
     cri-dockerd       hello.txt       incremental_backup.tar.gz    user.sh
     emacs_code.c      hello_world     incremental_backup1.tar.gz
    
    /home/projects:
    project_plan.txt
    [intern@intern-a1t-inf-lnx1 ~]$ !e
    echo /home/projects /home/intern
    /home/projects /home/intern
    

Task 3: Searching Command History

  • Execute various commands with descriptive keywords in them.
  • Use the Ctrl + R keyboard shortcut to initiate a reverse search in the command history.
  • Start typing a keyword, and the terminal will display the most recent command containing that keyword.
  • Continue pressing Ctrl + R to cycle through matching commands.
  • Modify and rerun a command found during the search.
    (reverse-i-search)`cd': cd /home/projects
    (reverse-i-search)`cd': cd
    

Task 4: Command History Management

  • Use the history -c command to clear the entire command history.
  • Execute a few more commands to populate the history again.
  • Limit the number of commands stored in the history by setting the HISTSIZE environment variable.
  • Check the size of your history using echo $HISTSIZE.
  • Test if new commands are added when the history size is reached.
    [intern@intern-a1t-inf-lnx1 ~]$ history -c
    [intern@intern-a1t-inf-lnx1 ~]$ history
        1  history
    [intern@intern-a1t-inf-lnx1 ~]$ echo $HISTSIZE
    1000
    [intern@intern-a1t-inf-lnx1 ~]$ export HISTSIZE=5
    [intern@intern-a1t-inf-lnx1 ~]$ echo "GG1"
    GG1
    [intern@intern-a1t-inf-lnx1 ~]$ echo "GG2"
    GG2
    [intern@intern-a1t-inf-lnx1 ~]$ echo "GG3"
    GG3
    [intern@intern-a1t-inf-lnx1 ~]$ echo "GG4"
    GG4
    [intern@intern-a1t-inf-lnx1 ~]$ echo "GG5"
    GG5
    [intern@intern-a1t-inf-lnx1 ~]$ echo "GG6"
    GG6
    [intern@intern-a1t-inf-lnx1 ~]$ history
        6  echo "GG3"
        7  echo "GG4"
        8  echo "GG5"
        9  echo "GG6"
       10  history
    

Task 5: Saving and Exporting Command History

  • Use the history -w command to write the current command history to a file, such as my_history.txt.
  • Open the saved history file using a text editor and inspect its contents.
  • Export the command history to a new terminal session using the history -r command.
  • Verify that your previous commands are available in the new session.
    [intern@intern-a1t-inf-lnx1 ~]$ history -w my_history
    [intern@intern-a1t-inf-lnx1 ~]$ cat my_history
    echo "GG6"
    history
    man history
    history --help
    history -w my_history
    
  • Open a new terminal session
    [intern@intern-a1t-inf-lnx1 ~]$ history
        1  ip a
        2  man apt
        3  unminimize
        4  sudo unminimize
        5  ip a
    [intern@intern-a1t-inf-lnx1 ~]$ history -r /home/projects/my_history
    [intern@intern-a1t-inf-lnx1 ~]$ history
      245  history -r /home/projects/my_history
      246  echo "GG6"
      247  history
      248  man history
      249  history --help
      250  history -w my_history
      251  history
    

Conclusion

In this lab exercise, you've delved into the world of command line history and recall in a Linux environment. You've learned essential techniques for navigating, searching, and managing your command history, which can significantly enhance your efficiency and productivity when working with the command line.