How to Display Command History in Linux
Displaying the Command History
The shell keeps a history of recently entered commands. This history mechanism enables you to view, repeat, or modify previously executed commands.
Using the history Command
The history command displays previously-executed commands. By default, the history command displays the last 16 commands to the standard output. The syntax for the history command is:
$ history option
To display previously executed commands, perform the following command:
$ history ... 87 date 88 cd /etc 89 touch dat1 dat2 90 ps -ef 91 history
The history command is an alias built into the Korn shell that enables you to display previously-executed commands.
The numbers on the left are command numbers. You can use the command numbers to instruct the shell to re-execute a particular command line. To display the command history without line numbers, perform the following command:
$ history -n ... date cd /etc touch dat1 dat2 ps -ef history
To display the current command and the four commands preceding it, perform the following command:
$ history -4 ... 107 date 108 cd /etc 109 touch dat1 dat2 110 ps -ef 111 history
To display the history list in reverse order, perform the following command:
$ history -r 111 history 110 ps -ef 109 touch dat1 dat2 108 cd /etc 107 date ...
To display the most recent cd command to the most recent date command, perform the following command:
$ history cd ls 31 cd 32 man lp 33 date ... (output truncated)
Using the r Command (in ksh shell)
The r command is an alias built into the Korn shell that enables you to repeat a command. To repeat the cal command by using the r command, perform the following command:
$ cal December 2004 S M Tu W Th F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 $ r cal December 2004 S M Tu W Th F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
To re-execute a command by line number, use the r command followed by the respective line number. For example:
$ history 155 history 156 cat dante 157 history 158 date 159 cal 160 ls 161 cd $ r 160 ( output omitted )
You can also use the r command to re-execute a command beginning with a particular character, or string of characters. To rerun the most recent occurrence of a command that begins with the letter “c,” perform the following command:
$ r c $ cd /etc
To rerun the most recent occurrence of the ps command, perform the following command:
$ r ps ps -ef
You can use the r command to repeat a previous command, perform a simple edit, and perform the modiﬁed command. For example, to repeat the most recent occurrence of a command beginning with the letter “c,” replace dir1 with dir2, and perform the modiﬁed command as follows:
$ history ... 100 cd 101 cat dante 102 ls 103 cd ~/dir1 104 history $ r c cd ~/dir1 $ r dir1=dir2 cd ~/dir2
Editing and Rerunning Previously Executed Commands
You can edit previously-executed commands and rerun these commands using a shell in-line editor. Use the vi editor to turn on and enable the shell history editing feature with one of the following commands:
$ set -o vi
$ export EDITOR=/bin/vi
$ export VISUAL=/bin/vi
You can access command in the history buffer, edit the command with the vi editor, and execute the modiﬁed command by following these steps:
1. Verify that the built-in vi editor is enabled.
$ set -o | grep -w vi vi on
2. Type the history command to view the command history list.
3. Press Escape to access the command history list. Use the following keyboard keys to move the cursor through the command history.
- k – Moves the cursor up one line at a time
- j – Moves the cursor down one line at a time
- l – Moves the cursor to the right
- h – Moves the cursor to the left
4. Use the vi commands to edit any previously executed command.
5. To perform a modiﬁed command, press Return.
File Name Completion
To invoke ﬁle name completion, type the ls command followed by one or more characters of a ﬁle name, and then press the following keys in sequential order: Escape (Esc) and backslash (\).
If the shell ﬁnds a ﬁle name beginning with the speciﬁed characters, it prints the complete ﬁle name or ﬁle names to the command line. For example, to expand a ﬁle name beginning with the characters de in the /usr directory:
$ cd /usr $ ls de Press Esc and \
The shell completes the remainder of the ﬁle name, displaying:
$ ls demo/
You can request the shell to present all the possible alternatives of a partial ﬁle name from which you could then select. This action is invoked by pressing the following keys sequentially: Escape (Esc) and the equal (=) key.
To request that the shell present all ﬁle names beginning with the letter “g” in the/etc directory, type:
$ cd /etc $ cat g Press Esc, press the = key1) gconf/ 2) getty 3) gimp/ 4) gnome-vfs-2.0/ 5) gnome-vfs-mime-magic 6) gnopernicus-1.0/ 7) group 8) grpck 9) gss/ 10) gtk-2.0/ 11) gtk/ $ cat g
The cursor is positioned on top of the letter “g” at this point.