locate
does not find files recently added to your system, run updatedb
as root to update the locate database.)
In the coming chapters, you learn to use additional commands. For now, I want you to become more familiar with how the shell itself works. So next I discuss features for recalling commands, completing commands, using variables, and creating aliases.
Recalling Commands Using Command History
Being able to repeat a command you ran earlier in a shell session can be convenient. Recalling a long and complex command line that you mistyped can save you some trouble. Fortunately, some shell features enable you to recall previous command lines, edit those lines, or complete a partially typed command line.
The shell history is a list of the commands that you have entered before. Using the history
command in a bash shell, you can view your previous commands. Then using various shell features, you can recall individual command lines from that list and change them however you please.
The rest of this section describes how to do command-line editing, how to complete parts of command lines, and how to recall and work with the history list.
Command-line editing
If you type something wrong on a command line, the bash shell ensures that you don't have to delete the entire line and start over. Likewise, you can recall a previous command line and change the elements to make a new command.
By default, the bash shell uses command-line editing that is based on the emacs text editor. (Type man emacs to read about it, if you care to do so.) If you are familiar with emacs, you probably already know most of the keystrokes described here.
TIP
If you prefer the vi
command for editing shell command lines, you can easily make that happen. Add the following line to the .bashrc
file in your home directory:
set -o vi
The next time you open a shell, you can use vi
commands to edit your command lines.
To do the editing, you can use a combination of control keys, meta keys, and arrow keys. For example, Ctrl+F means to hold down the Ctrl key, and type f. Alt+F means to hold down the Alt key, and type f. (Instead of the Alt key, your keyboard may use a Meta key or the Esc key. On a Windows keyboard, you can use the Windows key.)
To try out a bit of command-line editing, enter the following:
$ ls /usr/bin | sort -f | less
This command lists the contents of the /usr/bin
directory, sorts the contents in alphabetical order (regardless of case), and pipes the output to less
. The less
command displays the first page of output, after which you can go through the rest of the output a line (press Enter) or a page (press spacebar) at a time. Simply press q when you are finished. Now, suppose that you want to change /usr/bin
to /bin
. You can use the following steps to change the command:
1 Press the up arrow (↑) key. This displays the most recent command from your shell history.
2 Press Ctrl+A. This moves the cursor to the beginning of the command line.
3 Press Ctrl+F or the right arrow (→) key. Repeat this command a few times to position the cursor under the first slash (/).
4 Press Ctrl+D. Type this command four times to delete /usr from the line.
5 Press Enter. This executes the command line.
As you edit a command line, at any point you can type regular characters to add those characters to the command line. The characters appear at the location of your text cursor. You can use right → and left ← arrows to move the cursor from one end to the other on the command line. You can also press the up ↑ and down ↓ arrow keys to step through previous commands in the history list to select a command line for editing. (See the section “Command-line recall” for details on how to recall commands from the history list.) You can use many keystrokes to edit your command lines. Table 3.1 lists the keystrokes that you can use to move around the command line.
TABLE 3.1 Keystrokes for Navigating Command Lines
Keystroke | Full Name | Meaning |
Ctrl+F | Character forward | Go forward one character. |
Ctrl+B | Character backward | Go backward one character. |
Alt+F | Word forward | Go forward one word. |
Alt+B | Word backward | Go backward one word. |
Ctrl+A | Beginning of line | Go to the beginning of the current line. |
Ctrl+E | End of line | Go to the end of the line. |
Ctrl+L | Clear screen | Clear screen and leave line at the top of the screen. |
The keystrokes in Table 3.2 can be used to edit command lines.
TABLE 3.2 Keystrokes for Editing Command Lines
Keystroke | Full Name | Meaning |
Ctrl+D | Delete current | Delete the current character. |
Backspace | Delete previous | Delete the previous character. |
Ctrl+T | Transpose character | Switch positions of current and previous characters. |
Alt+T | Transpose words | Switch positions of current and previous words. |
Alt+U | Uppercase word | Change the current word to uppercase. |
Alt+L | Lowercase word | Change the current word to lowercase. |
Alt+C | Capitalize word | Change the current word to an initial capital. |
Ctrl+V | Insert special character | Add a special character. For example, to add a Tab character, press Ctrl+V+Tab. |