Linux 的体系结构

- 体系结构主要分为用户态(用户上层活动)和内核态
- 内核:本质是一段管理计算机硬件设备的程序
- 系统调用:内核的访问接口,是一种不能再简化的操作
- man 2 syscalls:查看所有系统调用
- man 2:表示查看的是系统调用
- man 3:表示查看公共库函数
- man 2 syscalls:查看所有系统调用
- 公用函数库:系统调用的组合拳
- Shell:命令解释器,可编程
- echo $SHELL:查看当前 Shell 版本
- cat /etc/shells:查看本机器支持的 Shell 版本
- chsh -s shell路径:切换 Shell
find – 查找特定文件
- 语法:find path [option] params
- 默认在当前目录及其子目录下查找
- find / -name “index.html”:意为全局查找
- 作用:在指定目录下查找文件
- 实例
查找当前用户 home 目录所有以某个字符开头的文件(忽略大小写)
[bee@localhost ~]$ find -name "index.html"
./demo/index.html
查找当前用户 home 目录所有以某个字符开头的文件
[bee@localhost /]$ find ~ -name "index*"
查找当前用户 home 目录所有以某个字符开头的文件(忽略大小写)
[bee@localhost /]$ find ~ -iname "index*"
man find:更多关于 find 指令的使用说明
grep – 检索文件内容
grep(Global Regular Expression print)
- 语法:grep [option] pattern file
- 作用:查找文件里符合条件的字符串,并只输出存在目标字符串所在的行内容
- option
- -v: 逆反模示, 只输出”不含” RE 字符串之句子.
-r: 递归模式, 可同时处理所有层级子目录里的文件.
-q: 静默模式, 不输出任何结果(stderr 除外. 常用以获取 return value, 符合为 true, 否则为 false .)
-i: 忽略大小写.
-w: 整词比对, 类似 \<word\> .
-n: 同时输出行号.
-c: 只输出符合比对的行数.
-l: 只输出符合比对的文件名称.
-o: 只输出符合 RE 的字符串. (gnu 新版独有, 不见得所有版本都支持.)
-E: 切换为 egrep .
- -v: 逆反模示, 只输出”不含” RE 字符串之句子.
- 实例
查找以 target 开头文件里包含 “moo” 字符串
[bee@localhost demo]$ grep "moo" target*
target2.java:moo dhasjdsb mooom
target.java:i love imooc
| – 管道操作符
可将指令连接起来,前一个指令的输出作为后一个指令的输入
[bee@localhost demo]$ find ~ | grep "target"
/home/bee/demo/target.java
/home/bee/demo/target2.java
使用管道注意的要点
- 只处理签一个命令正确输出,不处理错误输出
- 右边命令必须能够接收标准输入流,否则传递过程中数据会被抛弃
- 常用来接收管道数据的命令:sed,awk,grep,cut,head,top,less,more,wc,join,sort,split
awk – 对文件内容做统计
语法:awk [option] ‘cmd’ file
原理
- 一次读取一行文本,按输入分隔符进行切片,切成多个组成部分
- 将切片直接保存在内建的变量中,$1,$2…($0 表示行的全部,$1 表示第一列)
- 支持对单个切片的判断,也支持对所有切片循环判断,默认分隔符为空格
实例
打印 netstat.txt 文件中的第一行和第四行
[bee@localhost demo]$ cat netstat.txt
Proto Adresse locale Adresse distante État
TCP 0.0.0.0:135 nephyl-PC:0 LISTENING
TCP 0.0.0.0:445 nephyl-PC:0 LISTENING
TCP 0.0.0.0:554 nephyl-PC:0 LISTENING
TCP 0.0.0.0:2869 nephyl-PC:0 LISTENING
TCP 0.0.0.0:5357 nephyl-PC:0 LISTENING
TCP 0.0.0.0:7501 nephyl-PC:0 LISTENING
TCP 0.0.0.0:10243 nephyl-PC:0 LISTENING
TCP 0.0.0.0:49152 nephyl-PC:0 LISTENING
[bee@localhost demo]$ awk '{print $1,$4}' netstat.txt
Proto Adresse
TCP LISTENING
TCP LISTENING
TCP LISTENING
TCP LISTENING
TCP LISTENING
TCP LISTENING
TCP LISTENING
TCP LISTENING
查询 netstat.txt 文件中第一列为 UDP 并且第二列为 0.0.0.1 的记录,并全部打印
[bee@localhost demo]$ cat netstat.txt
Proto Adresse locale Adresse distante État
TCP 0.0.0.0:135 nephyl-PC:0 LISTENING
TCP 0.0.0.0:445 nephyl-PC:0 LISTENING
TCP 0.0.0.0:554 nephyl-PC:0 LISTENING
TCP 0.0.0.0:2869 nephyl-PC:0 LISTENING
UDP 0.0.0.1 nephyl-PC:0 LISTENING
TCP 0.0.0.0:7501 nephyl-PC:0 LISTENING
UDP 0.0.0.0 nephyl-PC:0 LISTENING
TCP 0.0.0.0:49152 nephyl-PC:0 LISTENING
[bee@localhost demo]$ awk '$1=="UDP" && $2=="0.0.0.1"{print $0}' netstat.txt
UDP 0.0.0.1 nephyl-PC:0 LISTENING
查询 netstat.txt 文件中第一列为 UDP 并且第二列为 0.0.0.1 的记录,并全部打印(包含表头)
[bee@localhost demo]$ awk '($1=="UDP" && $2=="0.0.0.1") || NR==1 {print $0}' netstat.txt
Proto Adresse locale Adresse distante État
UDP 0.0.0.1 nephyl-PC:0 LISTENING
NR 以换行作为分隔符,NR==1,意为第一行
以 逗号 为分隔符
[bee@localhost demo]$ cat test.txt
dadadsdsa,132321321
dsadsadsa,234312321
dasda,12121
dafa,1321321
[bee@localhost demo]$ awk -F ',' '{print $2}' test.txt
132321321
234312321
12121
1321321
-F:表示以什么符号作为分隔符
统计字符串出现的次数
awk '{enginearr[$1]++}END{for(i in enginearr)print i '\t' enginearr[i]}'

sed – 批量替换文本内容
sed(stream editor,流编辑器)
语法:sed [option] ‘sed command’ filename
适用于对文本的行内容进行处理
实例
将 Str 替换成 String
[bee@localhost demo]$ cat replace.java
Str a = "The beautiful girl's boy friend is Jack"
Str b = "The beautiful girl often chats with Jack and Jack is Jack".
Str c = "The beautiful girl loves Jack so much".
Integer bf = new integer(2);
[bee@localhost demo]$ sed 's/^Str/String/' replace.java
String a = "The beautiful girl's boy friend is Jack"
String b = "The beautiful girl often chats with Jack and Jack is Jack".
String c = "The beautiful girl loves Jack so much".
Integer bf = new integer(2);
sed 's/^Str/String/' replace.java
- s:要对字符串进行操作
- ^Str:表示对 Str 开头的字符串操作
- 第二个反斜杠和第三个反斜杠之间的是要替换的内容
- 此时 replace.java 文件内容并没有被修改,因为 sed 指令默认只将修改的内容输出到终端,若要更改到文件,需加 -i 指令
sed -i 's/^Str/String/' replace.java
将句尾 . 替换成 ;
[bee@localhost demo]$ cat replace.java
String a = "The beautiful girl's boy friend is Jack"
String b = "The beautiful girl often chats with Jack and Jack is Jack".
String c = "The beautiful girl loves Jack so much".
Integer bf = new integer(2);
[bee@localhost demo]$ sed -i 's/\.$/\;/' replace.java
[bee@localhost demo]$ cat replace.java
String a = "The beautiful girl's boy friend is Jack"
String b = "The beautiful girl often chats with Jack and Jack is Jack";
String c = "The beautiful girl loves Jack so much";
Integer bf = new integer(2);
sed -i 's/\.$/\;/' replace.java
- \.:表示转义为 .
- $:表示以什么结尾的内容
- \;:表示转义为 ;
将全文的 Jack 替换成 me
[bee@localhost demo]$ sed -i 's/\.$/\;/' replace.java
[bee@localhost demo]$ cat replace.java
String a = "The beautiful girl's boy friend is Jack"
String b = "The beautiful girl often chats with Jack and Jack is Jack";
String c = "The beautiful girl loves Jack so much";
Integer bf = new integer(2);
[bee@localhost demo]$ sed -i 's/Jack/me/g' replace.java
[bee@localhost demo]$ cat replace.java
String a = "The beautiful girl's boy friend is me"
String b = "The beautiful girl often chats with me and me is me";
String c = "The beautiful girl loves me so much";
Integer bf = new integer(2);
sed -i 's/Jack/me/g' replace.java
g:全文替换,否则每行只替换第一个满足条件的字符串
删除空行
[bee@localhost demo]$ cat replace.java
String a = "The beautiful girl's boy friend is me"
String b = "The beautiful girl often chats with me and me is me";
String c = "The beautiful girl loves me so much";
Integer bf = new integer(2);
[bee@localhost demo]$ sed -i '/^ *$/d' replace.java
[bee@localhost demo]$ cat replace.java
String a = "The beautiful girl's boy friend is me"
String b = "The beautiful girl often chats with me and me is me";
String c = "The beautiful girl loves me so much";
Integer bf = new integer(2);
sed -i '/^ *$/d' replace.java
d:表示删除符合条件的行
删除包含 Integer 的行
[bee@localhost demo]$ cat replace.java
String a = "The beautiful girl's boy friend is me"
String b = "The beautiful girl often chats with me and me is me";
String c = "The beautiful girl loves me so much";
Integer bf = new integer(2);
[bee@localhost demo]$ sed -i '/Integer/d' replace.java
[bee@localhost demo]$ cat replace.java
String a = "The beautiful girl's boy friend is me"
String b = "The beautiful girl often chats with me and me is me";
String c = "The beautiful girl loves me so much";
sed -i '/Integer/d' replace.java