A Practical Guide to Linux Commands, Editors, and Shell Programming

Chapter 03 Command Line Utilities - 命令行工具 1/2

此章主要介绍最基本和最重要的Linux文本模式下的命令行工具.

1) 特殊字符 Special characters
+ 特殊字符对Shell来说, 有特殊的意义, 所以尽量不要让文件名中含有特殊字符:

& ; | * ? ‘ ” ` [ ] ( ) $ < > { } ^ # / % ! ~ +

若想知道特殊字符的英文读法, 请看这里.

+ 当然, 回车键 (RETURN), 空格键 (SPACE), TAB键也对Shell有特殊的意义, 也尽量不要用在文件名中.

+ 如果你非要在文件名中使用特殊字符, 也是可以的, 只是在引用时有些麻烦. 引用此文件时, 有2种方法:
++ 在每一个特殊字符前面都加一个反斜线 (backslash), 如你想使用**, 你必须输入 **. 当然, 若你想使用, 你需要输入\.
++ 使用一对单引号 (single quotation marks) 引用特殊字符. 若你想用**, 输入 ‘**’ 即可. 单引号中可以包含一般字符 (regular character), 如 ‘This is a special character:>’ .

2) 基本命令工具 Basic utilities
ls cat rm less more hostname

+ ls = LIst 显示文件名 (displays the names of files)
ls -al | less 逐屏显示当前目录的详细文件清单
+ cat = conCATenate 显示文本文件内容 (displays contents of text files)
concatenate的意思是一个一个连接在一起, 所以cat也可以同时显示多个文件的内容, 这里暂不讨论.
+ rm = ReMove 删除文件或目录 (remove files or directories)
默认情况下, rm直接删除指定的文件, 无需经过再次确认. 一个更安全的方法是使用选项-i (i = interactive 互动), 这让你有个互动式的再次确认的机会, 可以有效地防止误删文件, 如:
rm -i toollist
rm: remove regular file ‘toollist’? y
你可以通过在startup文件中创建alias, 来让rm在不输入-i的情况下, 默认采用以上的互动模式.

+ less = LESS is more 将文本文件一次只显示一个屏幕的内容 (displaying a text file one screen at a time)
less和more功能一样, 只有细微的差别, 一般配合管道 | (pipe) 一起使用.
+ tip: 当你输入文件名时, 只输入一部分文件名, 按一下Tab键, Shell会帮你自动完成文件名.
+ hostname = HOSTNAME 显示电脑主机名 (displays the system name)

3) 与文件操作相关的命令 Working with files
cp mv lpr grep head tail sort uniq diff file

+ cp = CoPy 复制文件和目录 (copy files and directories)
格式: cy source-file destination-file 如 cp memo memo.copy , cp命令也会覆盖同名目标文件, 也可使用互动选项-i.
+ mv = MoVe 移动/重命名文件 (move/rename a file)
格式: mv source-filename destination-filename 如将文件memo改名为memo.1216 mv memo memo.1216 , mv命令会覆盖同名目标文件, 也可使用互动选项-i. 注意: 复制文件cp和移动/重命名文件mv是不同的.
+ lpr = Line PRinter 打印文件 (print a file)
将文件report打印到名为mailroom的打印机: lpr -Pmailroom report . 另外, lpq 可查看打印队列, lprm 可取消打印.
+ grep = Global Regular Expression Print 查找字符串 (finds a string)
grep可以在一个或多个文件中查找指定的字符串, 如在文件memo中查找字符串credit: grep ‘credit’ memo 需要查找的字符串可以不用单引号, 但使用单引号可以允许你查找含有空格和特殊字符的内容. 在上例中, 含有discredit, creditor, accreditation的内容是符合查找规则的, 因为这些词包含了credit字符串, 使用选项-w (grep -w ‘credit’ memo) 可以让grep只显示符合含有完整单词credit的内容.

+ head = HEAD 显示文件的开始部分 (displays the beginning of a file)*
默认, head只显示文件的前10行, 你可以指定显示的行数, 如仅显示文件months的第一行 head -1 months
+ tail = TAIL 显示文件的结束部分 (displays the end of a file) *
与head的用法类似.
+ sort = SORT文本文件的每行排序并显示 (sort lines of text file) *
sort是按字母顺序显示文件内容.
+ uniq = UNIQue 移除文件重复的行 (duplicate lines from a file) *
使用 sort -u 可以达到同样的效果.

+ diff = DIFFerence 逐行比较文件 (compare files line by line) *
diff逐行比较文件并显示不同的地方.
使用选项 -u (unified output format) 可以更好地显示两文件间的差别: 此选项会把长的多行的文件分成块 (hunks), 每块的开始和结束都有@@ (two at signs), 同时, 一个文件(如A)被标识为减号 - (a minus sign), 而另一个文件(如B)被标识为加号 + (a plus sign), 显示结果时, 前面有减号的内容表示仅出现在A文件中, 前面有加号的内容表示仅出现在B文件中, 前面没有任何符号的内容表示两个文件都有. 请看下例:
diff -u colors.1 colors.2
— colors.1 Fri Dec 16 18:20:32 2007
+++ colors.2 Fri Dec 16 18:21:53 2007
@@ -1,6 +1,5 @@
red
+blue
green
yellow
-pink
-purple
orange

若从上例中看不出colors.1文件和colors.2文件分别有哪些内容, 说明你还没有弄懂, 建议自己弄个例子亲自试试.

+ file = FILE 确定文件的类型 (determine file type)
简单地说, file就是向你报告你指定的文件是什么类型的文件.

*此工具不会更改原文件的内容.

4) | (管道) 进程间的通信 (Pipe) Communications between processes
一个进程就是指一个命令的执行, 进程间的通信是Unix/Linux系统的特征之一. 管道 (pipe) 用一个竖条表示, | (vertical bar), 提供进程间通信的最简单的形式. 管道接收一个工具的输出, 把这个输出发送给另一个工具作为输入. (a pipe takes the output of one utility and sends that output as input to another utility.)
如, 打印months文件的最后十行 tail months | lpr

其它 Others
A directory is a folder that can hold files.
utilities less and more have subtle differences. (subtle = slight and delicate)