rhl10.htm
来自「linux的初学电子书」· HTM 代码 · 共 1,898 行 · 第 1/4 页
HTM
1,898 行
<PRE>
<FONT COLOR="#000080">cd test</FONT></PRE>
<P>If instead you decided that you want to cd into the tools subdirectory, you could have typed
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">cd to<tab></FONT></PRE>
<P>This would also give bash enough information to complete the command.
<BR>
<P>Whenever you press the Tab key while typing a command, bash will try to complete the command for you. If it can't complete the command, it will fill in as much as it can and then beep, notifying you that it needs more information. You can then enter
more characters and press the Tab key again, repeating this process until bash returns the desired command.
<BR>
<BR>
<A NAME="E69E167"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Wildcards</B></FONT></CENTER></H4>
<BR>
<P>Another way that bash makes typing commands easier is by enabling users to use wildcards in their commands. The bash shell supports three kinds of wildcards:
<BR>
<UL>
<UL>
<P>* matches any character and any number of characters.
<BR>
</UL></UL>
<UL>
<UL>
<P>? matches any single character.
<BR>
</UL></UL>
<UL>
<UL>
<P>[...] matches any single character contained within the brackets.
<BR>
</UL></UL>
<P>The * wildcard can be used in a manner similar to command-line completion. For example, assume the current directory contains the following files:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">News/ bin/ games/ mail/ samplefile test/</FONT></PRE>
<P>If you want to cd into the test directory, you could type cd test, or you could use command-line completion:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">cd t<tab></FONT></PRE>
<P>This causes bash to complete the command for you. Now there is a third way to do the same thing. Because only one file begins with the letter t, you could also change to the directory by using the * wildcard. You could enter the following command:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">cd t*</FONT></PRE>
<P>The * matches any character and any number of characters, so the shell will replace the t* with test (the only file in the directory that matches the wildcard pattern).
<BR>
<P>This will work reliably only if there is one file in the directory that starts with the letter t. If more than one file in the directory starts with the letter t, the shell will try to replace t* with the list of filenames in the directory that match
the wildcard pattern and the cd command will cd into the first directory in this list. This will end up being the file that comes first alphabetically, and may or may not be the intended file.
<BR>
<P>A more practical situation in which to use the * wildcard is when you want to execute the same command on multiple files that have similar filenames. For example, assume the current directory contains the following files:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">ch1.doc ch2.doc ch3.doc chimp config mail/ test/ tools/</FONT></PRE>
<P>If you wanted to print all of the files that have a .doc extension, you could do so easily by entering the following command:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">lpr *.doc</FONT></PRE>
<P>In this case, bash will replace *.doc with the names of all of the files in the directory that match that wildcard pattern. After bash performed this substitution, the command that would be processed would be:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">lpr ch1.doc ch2.doc ch3.doc</FONT></PRE>
<P>The lpr command would be invoked with the arguments of ch1.doc, ch2.doc, and ch3.doc.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>Given the directory contents used in the previous example, there are several ways to print all of the files that have a .doc extension. All of the following commands would also work:
<BR>lpr *doc
<BR>lpr *oc
<BR>lpr *c</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>The ? wildcard functions in an identical way to the * wildcard except that the ? wildcard only matches a single character. Using the same directory contents shown in the previous example, the ? wildcard could be used to print all of the files with the
.doc extension by entering the following command:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">lpr ch?.doc</FONT></PRE>
<P>The [...] wildcard enables you to specify certain characters or ranges of characters to match. To print all of the files in the example that have the .doc extension using the [...] wildcard, you would enter one of the following two commands:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">lpr ch[123].doc</FONT></PRE>
<P>Using a command to specify a range of characters, you would enter
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">lpr ch[1-3].doc</FONT></PRE>
<BR>
<A NAME="E69E168"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Command History</B></FONT></CENTER></H4>
<BR>
<P>bash also supports command history. This means that bash keeps track of a certain number of previous commands that have been entered into the shell. The number of commands is given by a shell variable called HISTSIZE. For more information on HISTSIZE,
see the section "bash Variables" later in this chapter.
<BR>
<P>bash stores the text of the previous commands in a history list. When you log into your account, the history list is initialized from a history file. The filename of the history file can be set using the HISTFILE bash variable. The default filename for
the history file is .bash_history. This file is usually located in your home directory. (Notice that the file begins with a period. This means that the file is hidden and will only appear in a directory listing if you use the -a or -A option of the ls
command.).
<BR>
<P>Just storing previous commands into a history file is not all that useful, so bash provides several ways of recalling them. The simplest way of using the history list is with the up- and down-arrow keys, which scroll through the commands that have been
previously entered.
<BR>
<P>Pressing the up-arrow key will cause the last command that was entered to appear on the command line. Pressing the up-arrow key again will put the command previous to that one on the command line, and so on. If you move up in the command buffer past the
command that you wanted, you can also move down the history list a command at a time by pressing the down-arrow key. (This is the same process used by the DOS doskey utility.)
<BR>
<P>The command displayed on the command line through the history list can be edited, if needed. bash supports a complex set of editing capabilities that are beyond the scope of this book, but there are simple ways of editing the command line for small and
easy changes. You can use the left and right arrow keys to move along the command line. You can insert text at any point in the command line, and can also delete text by using the Backspace or Delete key. Most users should find these simple editing
commands sufficient.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>The complex set of editing commands that bash offers are similar to the commands used in the emacs and vi text editors.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>Another method of using the history file is to display and edit the list using the history and fc (fix command) commands built into bash. The history command can be invoked using two different methods. The first method uses the command
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">history [n]</FONT></PRE>
<P>When the history command is used with no options, the entire contents of the history list are displayed. The list that is displayed on-screen might resemble the following sample list:
<BR>
<PRE>
<FONT COLOR="#000080">1 mkdir /usr/games/pool
2 cp XpoolTable-1.2.linux.tar.z /usr/games/pool
3 cd /usr/games/pool/
4 ls
5 gunzip XpoolTable-1.2.linux.tar.z
6 tar -xf XpoolTable-1.2.linux.tar
7 ls
8 cd Xpool
9 ls
10 xinit
11 exit
12 which zip
13 zip
14 more readme
15 vi readme
16 exit</FONT></PRE>
<P>Using the n with the history command causes the only last n lines in the history list to be shown. So, for example, history 5 shows only the last five commands.
<BR>
<P>The second method of invoking the history command is used to modify the contents of the history file, or the history list. The command has the following command-line syntax:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">history [-r|w|a|n] [filename]</FONT></PRE>
<P>In this form, the -r option tells the history command to read the contents of the history file and use them as the current history list. The -w option will cause the history command to write the current history list to the history file (overwriting what
is currently in the file). The -a option appends the current history list to the end of the history file. The -n option causes the lines that are in the history file to be read into the current history list.
<BR>
<P>All of the options for the second form of the history command can use the filename option as the name of the history file. If no filename is specified, the history command will use the value of the HISTFILE shell variable.
<BR>
<P>The fc command can be used in two different ways to edit the command history. In the first way, the fc command would be entered using the following command-line syntax:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">fc [-e editor_name] [-n] [-l] [-r] [first] [last]</FONT></PRE>
<P>where all options given in braces are optional. The -e editor name option is used to specify the text editor to be used for editing the commands. The first and last options are used to select a range of commands to take out of the history list. first
and last can refer either to the number of a command in the history list or to a string that fc will try to find in the history list.
<BR>
<P>The -n option is used to suppress command numbers when listing the history commands. The -r option lists the matched commands in reverse order. The -l command lists the matched commands to the screen. In all cases except when the -l command option is
used, the matching commands will be loaded into a text editor.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>The text editor used by fc is found by taking the value of editor name if the -e editor name option is used. If this option was not used, fc uses the editor specified by the variable FCEDIT. If this variable does not exist, fc will use the value of
the EDITOR variable. Finally, if none of these variables exists, the editor that will be chosen is vi, by default.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E69E169"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Aliases</B></FONT></CENTER></H4>
<BR>
<P>Another way that bash makes life easier for you is by supporting command aliases. Command aliases are commands that the user can specify. Alias commands are usually abbreviations of other commands, designed to save keystrokes.
<BR>
<P>For example, if you are entering the following command on a regular basis, you might be inclined to create an alias for it to save yourself some typing:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">cd /usr/X11R6/lib/X11/config</FONT></PRE>
<P>Instead of typing this command every time you wanted to go to the sample-configs directory, you could create an alias called goconfig that would cause the longer command to be executed. To set up an alias like this you must use the bash alias command.
To create the goconfig alias, enter the following command at the bash prompt:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">alias goconfig='cd /usr/X11R6/lib/X11/configs</FONT></PRE>
<P>Now, until you exit from bash, the goconfig command will cause the original, longer command to be executed as if you had just typed it.
<BR>
<P>If you decide after you have entered an alias that you did not need it, you can use the bash unalias command to delete the alias:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">unalias goconfig</FONT></PRE>
<P>There are a number of useful aliases that most users find helpful. These can be written in a file that you execute when you log in, to save you from typing them each time. Some aliases that you might want to define are the following:
<BR>
<UL>
<LI>alias ll='ls -l'
<BR>
<BR>
<LI>alias log='logout'
<BR>
<BR>
<LI>alias ls='ls -F'
<BR>
<BR>
</UL>
<P>If you are a DOS user and are used to using DOS file commands, you can use the alias command to define the following aliases so that Linux behaves like DOS:
<BR>
<UL>
<LI>alias dir='ls'
<BR>
<BR>
<LI>alias copy='cp'
<BR>
<BR>
<LI>alias rename='mv'
<BR>
<BR>
<LI>alias md='mkdir'
<BR>
<BR>
<LI>alias rd='rmdir'
<BR>
<BR>
</UL>
<BLOCKQUOTE>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?