7.11 本章讲解知识总结
1)重点命令:vi/vim/echo/cat/more/less/head/tail/grep/tr
2)普通命令:alias/unalias
3)重要知识:拷贝直接覆盖知识、多行文本追加知识、重定向符号知识*****
1. vi/vim****纯文本编辑器
-
vi相当于记事本
vim想当于notp++
模式切换示意图 (考试题)
image.png
image.png
2.vim命令
-
普通模式:
:set su 显示行号
移动光标:
行尾 $
行首 0
文件尾部 G
文件开头 gg
到指定行数字+gg
普通模式:搜索与替换操作
向下搜索 /+内容
继续搜按n,反向搜N
向上搜索?+内容
普通模式:替换
:%s/A/B/g——A是被替换名,B是替换名,/是分隔符(可用其他字符替换)
s是替换,g表示全局(所有的内容全部替换)
指定行号替换:n1,n2s/A/B/g
image.png
普通模式:删除
删除单行 dd (剪切)
删除多行 ndd n用任意数字代替
撤销 u
普通模式:复制
单行复制 yy
多行复制 nyy n用任意数字代替
粘贴 yyp
进入编辑模式:
i 插入
A 追加行尾插入
o 下一行开头插入
O 上一行开头插入
a 光标下一个字符插入
命令行命令
image.png
3. echo显示输出文本内容

4. cat查看内容文件
cat -n 显示行号 tac 和cat相反倒着显示
5. more和less
more分页显示文件内容
less分页显示文件内容
6. head和tail
- head显示文件内容头部
默认显示前10行,显示前N行,-n+数字,n可以省略
head -n5 test.txt - tail 显示文件内容尾部
默认显示后10行,显示后N行,-n+数字,n可以省略
-f是跟踪显示文件尾部的信息
固定知识
Linux中所有字符都尽量加双引号
Linux中一切皆文件,Windows用扩展名区分文件
1、标准输出
0、标准输入
2、错误输出
1. tr 替换删除命令 translate or delete characters
替换 删除 字符

- Linux里严格区分大小写。
[root@oldgirl ~]# cat test.txt
Welcome to oldboy training.
we are excellent.
[root@oldgirl ~]# tr "w" "9" < test.txt
Welcome to oldboy training.
9e are excellent.
[root@oldgirl ~]# tr w 9 < test.txt
Welcome to oldboy training.
9e are excellent.
所有字符都尽量加双引号。
2.grep :筛选 - print lines matching a pattern
打印 行 匹配 模式

- [root@oldgirl ~]# cat test.txt
Welcome to oldboy training.
we are excellent.
[root@oldgirl ~]# grep -i "w" test.txt
Welcome to oldboy training.
we are excellent.
[root@oldgirl ~]# grep "w" test.txt
we are excellent.
[root@oldgirl ~]# grep -iv "w" test.txt - [root@oldgirl ~]# grep "oldboy" test.txt
Welcome to oldboy training.
oldboy1
[root@oldgirl ~]# grep -o "oldboy" test.txt
oldboy
oldboy
[root@oldgirl ~]# ifconfig|grep "10.0.0.201"
inet 10.0.0.201 netmask 255.255.255.0 broadcast 10.0.0.255
[root@oldgirl ~]#
[root@oldgirl ~]#
[root@oldgirl ~]# ifconfig|grep -o "10.0.0.201"
10.0.0.201
[root@oldgirl ~]# ifconfig|grep -o "10.0.0.201"
10.0.0.201
[root@oldgirl ~]# grep -oi "oldboy" test.txt
oldboy
oldboy
[root@oldgirl ~]#
[root@oldgirl ~]# grep -oiw "oldboy" test.txt
oldboy
[root@oldgirl ~]# grep -E "to|are" test.txt
Welcome to oldboy training.
we are excellent.
[root@oldgirl ~]# egrep "to|are" test.txt
Welcome to oldboy training.
we are excellent.
3. 重定向符号的核心知识*****

t>或1>标准输出重定向,箭头方向就是数据流向,
把左边的数据流向到右边,会清空右边之前的数据。
清空前备份:
[root@oldgirl ~]# cp test.txt{,.ori}
[root@oldgirl ~]# cp test.txt test.txt.ori
清空文件:
[root@oldgirl ~]# >test.txt
[root@oldgirl ~]# cat test.txtecho "I am studying linux." >/data/oldboy.txt
“>>或1>>”追加输出重定向,内容追加到文件尾部
[root@oldgirl ~]# echo "I am studying linux." >>/data/oldboy.txt
[root@oldgirl ~]# cat /data/oldboy.txt
I am studying linux.
I am studying linux.
I am studying linux.
<或0<标准输入重定向,箭头方向就是数据流向,
standard input, writing to standard output.
标准 输入 写 到 标准 输出
[root@oldboyedu ~]# tr "am" "01" </data/oldboy.txt
I 01 studying linux.
I 01 studying linux.
I 01 studying linux.
I 01 studying linux..
<<或0<<追加输入重定向,箭头方向就是数据流向,2> 标准错误输出重定向,箭头方向就是数据流向,把左边的【报错】输出到右边(覆盖)。
2>> 标准错误追加输出重定向,箭头方向就是数据流向,把左边的【报错】输出到右边(追加)。固定定义:
数字1 标准输出(standard output)
数字0 标准输入(standard input)
数字2 错误输出(error output)[root@oldboyedu ~]# echo "I am studying linux." 1>/data/oldboy.txt
[root@oldboyedu ~]# cat /data/oldboy.txt
I am studying linux.
[root@oldboyedu ~]# echo "I am studying linux.." 1>>/data/oldboy.txt
[root@oldboyedu ~]# cat /data/oldboy.txt
I am studying linux.
I am studying linux..
[root@oldboyedu ~]# tr "am" "01" 0</data/oldboy.txt
I 01 studying linux.
I 01 studying linux..此外还有一个特殊重定向用法:将标准错误重定向到标准输出,即标准错误和标准输出一样从定向到文件中,这个功能有3种实现命令方法。
方法1:echo "I am oldboy" 1>>oldboy.txt 2>>oldboy.txt
方法2:echo "I am oldboy" &>>oldboy.txt
方法3:echo "I am oldboy" >>oldboy.txt 2>&1考题:在/data目录下创建oldboy.txt,并增加"I am studying linux."一行内容。
该题有多种解题方法,下面来一一分析讲解。
方法1:
[root@oldgirl ~]# ls -ld /data
ls: cannot access /data: No such file or directory
[root@oldgirl ~]# mkdir /data -p
[root@oldgirl ~]# ls -ld /data
drwxr-xr-x. 2 root root 6 Mar 13 10:20 /data
[root@oldgirl ~]# vim /data/oldboy.txt
I am studying linux.
[root@oldgirl ~]# cat /data/oldboy.txt
I am studying linux.
方法2:
[root@oldgirl ~]# mkdir -p /data
[root@oldgirl ~]# echo "I am studying linux." >/data/oldboy.txt
[root@oldgirl ~]# cat /data/oldboy.txt
I am studying linux.
方法3:
cat>/data/oldboy.txt <<EOF
I am studying linux.
I am studying linux.
I am studying linux.
EOF
"<==EOF成对出现,后面这个顶格。"



