The Linux Command Line-WILLIAM(3)-常用操作

488 阅读3分钟

1 Linux中提高工作效率的方法

1.1 历史命令- history

首先来看两条命令:

  • clear —Clear the screen.

  • history —Display the contents of the history list.

默认bash存储500条历史命令,使用grep来筛选历史命令:

[me@linuxbox ~]$ history | grep /usr/
...
88  ls -l /usr/bin > ls-output.txt
...

结果中第一列的88是历史命令中的行号,可以使用历史命令行号来进行历史扩展(history expansion)

[me@linuxbox ~]$ !88

bash将会扩展(expand)!88为历史命令列表中第八十八行的命令

bash也提供在历史命令列表中渐进的搜索命令。即当我们输入字符时,我们可以告诉bash以输入内容来搜索历史记录列表,每个输入的其他字符进一步完善搜索结果。启用渐进搜索可以使用CTRL-R 快捷键,然后输入文本来搜索命令,当发现命令的时候可以按下回车键来执行命令或者使用CTRL-J来拷贝命令到新的命令行,当想要发现符合的命令结果时使用CTRL-R来向上移动历史列表。CTRL-G 或者 CTRL-C快捷键可以退出查找。

[me@linuxbox ~]$ #press CTRL-R
(reverse-i-search)`': 
(reverse-i-search)`/usr/bin': ls -l /usr/bin > ls-output.tx #input/usr/bin
[me@linuxbox ~]$ ls -l /usr/bin > ls-output.txt #essing CTRL-J

1.2 命令行编辑快捷键

Table 8-1: Cursor Movement Commands

Key Action
CTRL -A Move cursor to the beginning of the line.
CTRL-E Move cursor to the end of the line.
CTRL-F Move cursor forward one character; same as the right arrow key.
CTRL-B Move cursor backward one character; same as the left arrow key.
ALT-F Move cursor forward one word.
ALT-B Move cursor backward one word.
CTRL-L Clear the screen and move the cursor to the top left corner. The clear command does the same thing.

Table 8-2: Text Editing Commands

Key Action
CTRL-D Delete the character at the cursor location.
CTRL-T Transpose (exchange) the character at the cursor location with the one preceding it.
ALT-T Transpose the word at the cursor location with the one pre ceding it.
ALT-L Convert the characters from the cursor location to the end of the word to lowercase.
ALT-U Convert the characters from the cursor location to the end of the word to uppercase

剪切和粘贴文本 Cutting and Pasting (Killing and Yanking) Text

Table 8-3: Cut and Paste Commands

Key Action
CTRL-K Kill text from the cursor location to the end of line.
CTRL-U Kill text from the cursor location to the beginning of the line.
ALT-D Kill text from the cursor location to the end of the current word.
ALT-BACKSPACE Kill text from the cursor location to the beginning of the cur rent word. If the cursor is at the beginning of a word, kill the previous word.
CTRL-Y Yank text from the kill-ring and insert it at the cursor location

Table 8-5: History Commands

Key Action
CTRL-P Move to the previous history entry. Same action as the up arrow.
CTRL-N Move to the next history entry. Same action as the down arrow.
ALT-< Move to the beginning (top) of the history list.
ALT-> Move to the end (bottom) of the history list; i.e., the current command line.
CTRL-R Reverse incremental search. Searches incrementally from the current command line up the history list.
ALT-P Reverse search, non-incremental. With this key, type the search string and press ENTER before the search is performed.
ALT-N Forward search, non-incremental.
CTRL-O Execute the current item in the history list and advance to the next one. This is handy if you are trying to re-execute a sequence of commands in the history list

Table 8-6: History Expansion Commands

Sequence Action
!! Repeat the last command. It is probably easier to press the up arrow and ENTER.
!number Repeat history list item number.
!string Repeat last history list item starting with string.
!?string Repeat last history list item containing string

1.3 补全(Completion)

按下TAB键来补全命令,文件名,变量(以美元符号$作为开始),用户名(以波浪号~作为开始),主机名(以@作为开始,只有在/etc/hosts文件中的主机名才可以被补全)。在补全时侯有时会出现提示音响起的情况,这是因为匹配到了多种情况,这种情况下只能进一步给予shell更多的线索来匹配结果。

Table 8-4: Completion Commands

Key Action
ALT-? Display list of possible completions. On most systems you can also do this by pressing the TAB key a second time, which is much easier.
ALT-* Insert all possible completions. This is useful when you want to use more than one possible match