Linux常用指令(一)

Linux 常用指令(一)

  • tail
  • grep
  • cat

Linux下的常用指令,便于查看log和一些文件操作0 0。

tail

tail 命令是一个看log的有利工具.

Syntax:

tail [必要参数] [选择参数] [files]

1
2
3
4
5
6
7
8
Options Description
-c [+]num :Output the last num bytes of each file
-f :This option will cause tail will loop forever, checking for new data at the end of the file(s). When new data appears, it will be printed.
-n num :Output the last num lines, instead of the default (10).
--pid=pid 与-f合用,表示在进程ID,PID死掉之后结束.
-v :Always print headers.
-q : 不显示处理信息
-s :与-f合用,表示在每次反复的间隔休眠S秒

这个还蛮简单的,就最常用的tail -f ws* 和 tail -f cs* | greg tick

grep

Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户。.

Syntax:

grep [options] pattern [files]

1
2
3
4
5
6
7
8
9
10
11
12
13
Options Description
-c : This prints only a count of the lines that match a pattern //count
-h : Display the matched lines, but do not display the filenames.
-i : Ignores, case for matching
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern.
-e exp : Specifies expression with this option. Can use multiple times.--regexp=<范本样式> #指定字符串做为查找文件内容的样式。
-f file : Takes patterns from file, one per line.
-E : Treats pattern as an extended regular expression (ERE)
-w : Match whole word
-o : Print only the matched parts of a matching line,
with each such part on a separate output line.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
grep的规则表达式:

^ #锚定行的开始 如:'^grep'匹配所有以grep开头的行。

$ #锚定行的结束 如:'grep$'匹配所有以grep结尾的行。

. #匹配一个非换行符的字符 如:'gr.p'匹配gr后接一个任意字符,然后是p。

* #匹配零个或多个先前字符 如:'*grep'匹配所有一个或多个空格后紧跟grep的行。

.* #一起用代表任意字符。

[] #匹配一个指定范围内的字符,如'[Gg]rep'匹配Grep和grep。

[^] #匹配一个不在指定范围内的字符,如:'[^A-FH-Z]rep'匹配不包含A-R和T-Z的一个字母开头,紧跟rep的行。

\(..\) #标记匹配字符,如'\(love\)',love被标记为1。

\< #锚定单词的开始,如:'\<grep'匹配包含以grep开头的单词的行。

\> #锚定单词的结束,如'grep\>'匹配包含以grep结尾的单词的行。

x\{m\} #重复字符x,m次,如:'0\{5\}'匹配包含5个o的行。

x\{m,\} #重复字符x,至少m次,如:'o\{5,\}'匹配至少有5个o的行。

x\{m,n\} #重复字符x,至少m次,不多于n次,如:'o\{5,10\}'匹配5--10个o的行。

\w #匹配文字和数字字符,也就是[A-Za-z0-9],如:'G\w*p'匹配以G后跟零个或多个文字或数字字符,然后是p。

\W #\w的反置形式,匹配一个或多个非单词字符,如点号句号等。

\b #单词锁定符,如: '\bgrep\b'只匹配grep。

eg:

exp-1

exp-2

exp-3

more example

cat

cat命令的用途是连接文件或标准输入并打印。这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用。

Syntax:

cat [options] [files]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
-A, --show-all           等价于 -vET

-b, --number-nonblank 对非空输出行编号

-e 等价于 -vE

-E, --show-ends 在每行结束处显示 $

-n, --number 对输出的所有行编号,由1开始对所有输出的行数编号

-s, --squeeze-blank 有连续两行以上的空白行,就代换为一行的空白行

-t 与 -vT 等价

-T, --show-tabs 将跳格字符显示为 ^I

-u (被忽略)

-v, --show-nonprinting 使用 ^ 和 M- 引用,除了 LFD 和 TAB 之外

eg:

exp-4

exp-5

exp-6

------ 本文结束 ------