1.wc统计文件的行数,空格和tab隔开的字符串个数,字符个数(包括回车和空格)
格式:wc [选项] 文件名
示例:
[root@localhost test]# ls
1.txt 2.txt dir1 dir2
[root@localhost test]# cat 2.txt
a
bb b
c c cc
[root@localhost test]# wc 2.txt
3 6 14 2.txt #3行,6个字符串,14个字符
[root@localhost test]#
#####注释############字符包括3个回车和3个空格
[root@localhost test]# cat -A 2.txt
a$
bb b$
c c cc$
也可以通过选项查看:
[root@localhost test]# wc -l 2.txt
3 2.txt
[root@localhost test]# wc -w 2.txt
6 2.txt
[root@localhost test]# wc -c 2.txt
14 2.txt
2.grep 过滤文本中你感兴趣的内容。格式:grep [选项]... 表达式 [文件名]或标准输入。注意:表达式不能放在文件名的后面!grep的参数有很多,先写出这几个。
-i忽略大小写 grep -i "root" passwd
-n显示行号
-v反向匹配 grep -v "^#" passwd
-o只显示匹配的内容
-r递归匹配,可以跟文件夹
-w只匹配单个完整的单词
-f以文件作为匹配的条件,过滤相同的内容。file 根据模式文件,处理两个文件相同内容 把第一个文件作为匹配条件 。grep -f a b
[root@localhost mnt]# vim passwd
[root@localhost mnt]# grep -i "root" passwd #在最后加入了一行ROOT,-i不区分大小写
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
ROOT
---------------
[root@localhost mnt]# grep -n "root" passwd #显示行号
1:root:x:0:0:root:/root:/bin/bash #在第1行
10:operator:x:11:0:operator:/root:/sbin/nologin #在第10行
----------------
[root@localhost test]# cat 2.txt
a
bb b
c c cc
[root@localhost test]# grep -v "^c" 2.txt # 过滤不以c开头的
a
bb b
---------------
[root@localhost test]# cat 2.txt
a
bb b
c c cc
[root@localhost test]# grep -o bb 2.txt #只显示bb
bb
----------------
[root@localhost test]# cat dir1/a.txt
#这是注释
a
bb
ccc
dd dd
[root@localhost test]# grep -r bb dir1 # -r递归匹配,可以跟文件夹
dir1/a.txt:bb
---------------
[root@localhost test]# cat 1.txt
a
b
ccc
cccdd
ddd
ee
d de1
[root@localhost test]# grep -w ccc 1.txt #cccdd不会匹配到,只能匹配一个完整的ccc
ccc
--------------
[root@localhost test]# cat 1.txt
a
b
ccc
cccdd
[root@localhost test]# cat 2.txt
a
bb b
c c cc
[root@localhost test]# grep -f 1.txt 2.txt #把1.txt作为匹配条件,a,b。 2.txt中匹配到了a bb b
a
bb b
例子:grep "^$" passwd 过滤空行。grep -v "^$" passwd 过滤非空行。grep -v "^#" passwd 不是以#号开头的行。grep -o root passwd 只显示root。grep -r "root" /etc 匹配/etc下所有包含root字符串的文件。
3.paste,左右合并文件内容。cat上下合并内容。
标准输出重定向:本来输出在屏幕上的内容,改变了输出方向,输出到了文件中。
格式:paste [选项] 文件1 文件2 > 左右合并为文件3
cat不光可以查看文件。还可以上下合并文件内容。
[root@localhost test]# cat 1.txt
a
b
c
d
[root@localhost test]# cat 2.txt
1
2
3
4
[root@localhost test]# cat 1.txt 2.txt #在屏幕上显示合并的结果,并没有合并后的文件
a
b
c
d
1
2
3
4[root@localhost test]# cat 1.txt 2.txt > 3.txt #将1.txt和2.txt合并的结果保存3.txt中。
[root@localhost test]# cat 3.txt
a
b
c
d
1
2
3
4paste左右合并文件内容。
[root@localhost test]# paste 1.txt 2.txt > 4.txt
[root@localhost test]# paste 1.txt 2.txt > 4.txt -d":" # 左右合并,用:隔开
[root@localhost test]# paste 4.txt
a:1
b:2
c:3
d:4
4.split拆分
-b指定多少字节拆分,spilt -b 500M bigfile myfile 按500M拆分bigfile文件,并取名为myfile
5.gzip压缩。gzip -d 解压。bzip2压缩,bzip2 -d解压。zip压缩,unzip解压
gzip压缩率高于bzip2
[root@localhost test]# ls
1.txt 2.txt 4.txt dir1 dir2
[root@localhost test]# gzip 1.txt #压缩1.txt
[root@localhost test]# ls
1.txt.gz 2.txt 4.txt dir1 dir2
[root@localhost test]# gzip -d 1.txt.gz #解压1.txt.gz
[root@localhost test]# ls
1.txt 2.txt 4.txt dir1 dir2
[root@localhost test]# bzip2 2.txt #压缩2.txt
[root@localhost test]# ls
1.txt 2.txt.bz2 4.txt dir1 dir2
[root@localhost test]# bzip2 -d 2.txt.bz2 #解压2.txt.bz2
[root@localhost test]# ls
1.txt 2.txt 4.txt dir1 dir2
6.tar压缩
tar 选项 自定义压缩包名字,要压缩的文件。只要出现tar一定要加f。说明:选项中如果加-那么f一定在最后-cvzf,如果没有-,f的位置可以任意cfz。
-c建立归档文件
-f代表使用归档,加了-必须把f放在最后,不加-选项的顺序随便
-x解压缩
-C指定解压的文件夹
-j调用bzip2压缩
z调用gzip压缩
-p保留权限
-v显示过程
-t不解包的情况下查看里面的文件里列表
tar cvfz 自定义名字 * tar xf 自定义的名字 : 解压
[root@localhost test]# ls
1.txt 2.txt 4.txt dir1 dir2
[root@localhost test]# tar cvfz myTar 1.txt #这里自定义的名字必须在要压缩的文件前面!
1.txt
[root@localhost test]# ls
1.txt 2.txt 4.txt dir1 dir2 myTar[root@localhost test]# rm 1.txt
rm:是否删除普通文件 "1.txt"?y
[root@localhost test]# ls
2.txt 4.txt dir1 dir2 myTar
[root@localhost test]# tar xf myTar #解压
[root@localhost test]# ls
1.txt 2.txt 4.txt dir1 dir2 myTar
7.vi编辑器。vim是vi的升级版本。
vim 如果文件名存在就是修改文件,如果不存在就是创建一个文件并修改。退出vi编辑器:esc 英文冒号 q退出。!强制,w保存。
esc:wq保存并退出。esc:q!强制退出。esc:wq!强制保存并退出。
!vim,打开上次打开的文件
三种命令模式:
默认模式:1.光标的移动。2.复制。3.删除。4.粘贴。5.其他操作
输入模式:在命令模式按i键可以进入输入模式。编辑你的文件
末行模式:保存,查找替换,设置默认的参数等
命令模式到输入模式按i,I,o,O,a,A.esc退出输入模式到命令模式
命令模式到末行模式按:。esc退出末行模式到命令模式。
命令模式的基础快捷方式:
光标的跳转:数字 G:跳转到数字行。G最后一行。1G第一行。gg第一行。w单词间跳转。5w跳过5个单词
y复制。yy复制一行。yw复制一个单词。10yy复制10行。p在光标下一行粘贴。P在光标上一行粘贴。u是撤回。ctl+r取消撤回。
dd 剪切,删除。10dd 剪切10行。dw剪切一个单词。
ZZ保存并退出
^跳转到行首。0跳转到行首。$跳转到行尾。end跳转到行尾
输入模式:
x剪切一个字符。~大小写转化。r替换字符。R替换模式,可以一直替换。J合并两行。d$从当前删到行尾。
末行模式:
文件中的指定内容执行保存查找或替换等操作。使 Vim 切换到编辑模式的方法是在命令模式状态下按“:”(英文冒号)键,此时 Vim 窗口的左下方出现一个“:”符号,这是就可以输入相关指令进行操作了。