8.6 Useful Features of the Shell
As you probably noticed in the examples above, entering commands in
Bash can include a lot of typing. In the following, get to know some
features of the Bash that can make your work a lot easier and save a lot of
By default, Bash
remembers commands you have
entered. This feature is called history. You can
browse through commands that have been entered before, select one you
want to repeat and then execute it again. To do so, press ↑ repeatedly until the desired command appears at the
prompt. To move forward through the list of previously entered commands,
press ↓. For easier repetition of a certain
command from Bash history, just type the first letter of the command you
want to repeat and press PgUp.
You can now edit the selected command (for example, change the name
of a file or a path), before you execute the command by pressing Enter. To edit the command line, just move the cursor to
the desired position using the arrow keys and start typing.
You can also search for a certain command in the history. Press
Ctrl+R to start an incremental search function. showing the
Just type one or several letters from the command you are searching
for. Each character you enter narrows down the search. The corresponding
search result is shown on the right side of the colon whereas your input
appears on the left of the colon. To accept a search result, press
Esc. The prompt now changes to its normal
appearance and shows the command you chose. You can now edit the command
or directly execute it by pressing Enter.
Completing a filename or directory name to its full length after
typing its first letters is another helpful feature of Bash. To do so,
type the first letters then press Tab (Tabulator).
If the filename or path can be uniquely identified, it is completed at
once and the cursor moves to the end of the filename. You can then enter
the next option of the command, if necessary. If the filename or path
cannot be uniquely identified (because there are several filenames
starting with the same letters), the filename or path is only completed
up to the point where it is getting ambiguous again. You can then obtain
a list of them by pressing Tab a second time. After
this, you can enter the next letters of the file or path then try
completion again by pressing Tab. When completing
filenames and paths with the help of Tab, you can
simultaneously check whether the file or path you want to enter really
exists (and you can be sure of getting the spelling right).
- Wild Cards
You can replace one or more characters in a filename with a wild
card for pathname expansion. Wild cards are characters that can stand
for other characters. There are three different types of these in Bash:
Matches exactly one arbitrary character
Matches any number of characters
Matches one of the characters from the group specified inside
the square brackets, which is represented here by the string
8.6.1 Examples For Using History, Completion and Wildcards
The following examples illustrate how to make use of these convenient
features of Bash.
If you already did the example Section 8.3.1, Examples for Working with Files and Directories your shell buffer should be filled
with commands which you can retrieve using the history function.
Press ↑ repeatedly until cd
Press Enter to execute the command and to
switch to your home directory.
By default, your home directory contains two subdirectories
starting with the same letter, Documents and
Enter cd D and press Tab.
Nothing happens since Bash cannot identify to which one of the
subdirectories you want to change.
Press Tab again to see the list of possible
[email protected]:~> cd D Desktop/ Documents/ [email protected]:~> cd D
The prompt still shows your initial input. Type the next character
of the subdirectory you want to go to and press Tab
Bash now completes the path.
You can now execute the command with Enter.
Now suppose that your home directory contains a number of files with
various file extensions. It also holds several versions of one file which
you saved under different filenames myfile1.txt,
myfile2.txt etc. You want to search for certain files
according to their properties.
First, create some test files in your home directory:
Use the touch command you already know to
create several (empty) files with different file extensions, for
example .pdf, .xml and
You can do this consecutively (do not forget to use the Bash
history function) or with only one touch command:
simply add several filenames separated by a blank.
Create at least two files that have the same file extension, for
To create several
versions of one file,
This command creates five consecutively numbered files:
List the contents of your home directory. It should look similar
-rw-r--r-- 1 tux users 0 2006-07-14 13:34 foo.xml
-rw-r--r-- 1 tux users 0 2006-07-14 13:47 home.html
-rw-r--r-- 1 tux users 0 2006-07-14 13:47 index.html
-rw-r--r-- 1 tux users 0 2006-07-14 13:47 toc.html
-rw-r--r-- 1 tux users 0 2006-07-14 13:34 manual.pdf
-rw-r--r-- 1 tux users 0 2006-07-14 13:49 myfile1.txt
-rw-r--r-- 1 tux users 0 2006-07-14 13:49 myfile2.txt
-rw-r--r-- 1 tux users 0 2006-07-14 13:49 myfile3.txt
-rw-r--r-- 1 tux users 0 2006-07-14 13:49 myfile4.txt
-rw-r--r-- 1 tux users 0 2006-07-14 13:49 myfile5.txt
-rw-r--r-- 1 tux users 0 2006-07-14 13:32 tux.png
With the help of wild cards, select certain subsets of the files
according to various criteria:
To list all files with the .html extension,
ls -l *.html
To list all
ls -l myfile?.txt
Note that you can only use the ? wild card
here because the numbering of the files is single-digit. As soon as
you also had a file named myfile10.txt you would
have to use the * wild card to view all versions of
myfile.txt (or add another question mark, so your
string looks like myfile??.txt).
To remove, for example, version 1-3 and version 5 of
Check the result with
Of all myfile.txt versions only
myfile4.txt should be left.
Of course, you can also combine several wild cards in one command. In
the example above, rm myfile[1-3,5].* would lead to the
same result as rm myfile[1-3,5].txt because there are
only files with the extension .txt available.
NOTE: Using Wildcards in rm Commands
Wildcards in a rm command can be very useful but
also dangerous: you might delete more files from your directory than
intended. To see which files would be affected by the
rm, run your wildcard string with
ls instead of rm first.