1、vi
1.1命令模式下
光标移动
h | 左 |
l | 右 |
j | 下 |
k | 上 |
数字+ h/j/k/l:移动多少格
w | 跳到下一个单词的开头 |
e | 跳到下一个单词的末尾 |
b | 跳到上一个单词的开头 |
数字+w/e/b:跳多少个单词
pgDn、Ctrl +f | 向下翻页 |
pgUP、Ctrl+b | 向上翻页 |
一行之间
跳到行首 | home,0,^(0、home:跳到最前面 ^:略过首行空格) |
跳到行尾 | end,$ |
在行间
gg | 跳到首行 |
G | 跳到尾行 |
数字(行号)+gg:跳到指定行 (添加行号:末行模式下,加 set nu 取消行号:set nonu)
删除 :
x,delete | 删除光标所在字符 |
dd | 删除整行 |
数字+dd | 删除当前行开始后的几行 |
D、d+$ | 行间之间删除光标之后的 |
:d+^ | 行间之间删除光标之前 |
复制:
yy | 复制光标所在行 |
数字+yy | 复制光标及光标以下的几行 |
p | 粘贴光标所在下一行 |
P | 粘贴光标所在上一行 |
查找:
/要查找的内容 | 从上到下(光标所在位置) |
?要查找的内容 | 从下往上 (光标所在位置) |
替换:
r | 替换光标所在字符 |
R | 整体替换字符;esc退出。 |
u | 撤销一次编辑操作 |
数字+u | 撤销多次操作 |
Ctrl+r | 撤销多次操作 |
. | 重复上一次操作。 |
可视化:
v | 按照字符选择 |
V | 按照矩形选择 |
Ctrl+v | 按照字符选择,可以上下左右调整区域长度和宽度) |
1.2从命令模式到编辑模式
大写 | 小写 | |
i、I | 光标前 | 行首 |
a、A | 光标后 | 行尾 |
o、O | 光标上一行 | 光标下一行 |
1.3 末行模式
: wq | 保存并退出 |
:w /文件路径/文件名 | |
:e /文件路径/文件名 | 打开一个新的文件编辑 |
: r /文件路径/文件名 | 读入一个文件内容 |
: s /old/new/ | 替换光标所在行的第一个匹配的字段 |
: s /old/new/g | 替换光标所在行的所有的字段 |
: 行号1,行号2 s /old/new/ | 行1到行2的第一个old替换成new |
: 行号1,行号2 s /old/new/g | 行1到行2的所有old替换成new%s /old/new/gc |
: % s /old/new/ | 替换全文所有行的第一个匹配字段 |
: %s /old/new/g | 替换所有匹配字段 |
: %s /old/new/gc | c:提示用户是否替换 |
: %s /old/new/gic | 忽略大小写,替换所有old |
:!+shell 命令 | “在文档中执行shell命令” |
1.4 打开文件
vim
vim+行号
vim+/关键字
1.5关闭文件
ZZ,命令模式关闭文件
2、压缩&解压缩
(1) zip&unzip
zip 压缩文件名(test.zip)要被压缩的的文件名字
zip -m test1.zip test2.txt 把test2添加到test1.zip中
zip -d test1.zip test2.txt 把test2从test1.zip中删掉
[root@demo6 ~]# zip -d file3.zip file4
zip test1.zip *.txt -x test10.txt 除了test10以外,将所有.txt的压缩
[root@demo6 ~]# zip test.zip *.txt -x test5.txt
adding: test0.txt (stored 0%)
adding: test1.txt (stored 0%)
adding: test2.txt (stored 0%)
adding: test3.txt (stored 0%)
adding: test4.txt (stored 0%)
zip -r/dir 递归压缩
unzip test1.zip
unzip test1.zip-d /压缩目录
unzip -v test1.zip 不解压,查看压缩内容
(2)gzip&gunzip
gzip 被压缩的文件名:压缩后,源文件不保留
[root@demo6 ~]# gzip err
[root@demo6 ~]# ls
abc err.gz fle1 TEST test3.txt test.zip
anaconda-ks.cfg erro install.log test0.txt test4.txt time=date"+"%H.log
app file1 install.log.syslog test1 test5 time.sh
ceu file3 tes test1.txt test5.txt
eername file3.zip test test2.txt test6
gzip -c gzip >test.txt test.txt.gz : 压缩后,源文件保留
[root@demo6 ~]# ls
abc err.gz fle1 TEST test3.txt test.zip
anaconda-ks.cfg erro install.log test0.txt test4.txt time=date"+"%H.log
app file1 install.log.syslog test1 test5 time.sh
ceu file3 tes test1.txt test5.txt
eername file3.zip test test2.txt test6
[root@demo6 ~]# gzip -c >test0.txt test0.txt.gz
gzip -r 目录 :压缩了目录下的所有内容。
[root@demo6 ~]# gzip -r app
[root@demo6 ~]# ls
abc err.gz fle1 TEST test3.txt test.zip
anaconda-ks.cfg erro install.log test0.txt test4.txt time=date"+"%H.log
app file1 install.log.syslog test1 test5 time.sh
ceu file3 tes test1.txt test5.txt
eername file3.zip test test2.txt test6
gunzip test.txt.gz :解压文件
[root@demo6 ~]# ls
abc err.gz fle1 TEST test3.txt test.zip
anaconda-ks.cfg erro install.log test0.txt test4.txt time=date"+"%H.log
app file1 install.log.syslog test1 test5 time.sh
ceu file3 tes test1.txt test5.txt
eername file3.zip test test2.txt test6
[root@demo6 ~]# gunzip err.gz
[root@demo6 ~]# ls
abc err fle1 TEST test3.txt test.zip
anaconda-ks.cfg erro install.log test0.txt test4.txt time=date"+"%H.log
app file1 install.log.syslog test1 test5 time.sh
ceu file3 tes test1.txt test5.txt
eername file3.zip test test2.txt test6
gunzip -c test.txt.gz >/root/test.txt 解压缩到特定目录,并且保留源文件
不解压查看:
zless test5.txt.gz
[root@demo6 ~]# ls
abc err fle1 TEST test3.txt test.zip
anaconda-ks.cfg erro install.log test0.txt test4.txt time=date"+"%H.log
app file1.gz install.log.syslog test1 test5 time.sh
ceu file3 tes test1.txt test5.txt
eername file3.zip test test2.txt test6
[root@demo6 ~]# zless file1.gz
zcat test5.txt.gz
[root@demo6 ~]# ls
abc err fle1 TEST test3.txt test.zip
anaconda-ks.cfg erro install.log test0.txt test4.txt time=date"+"%H.log
app file1.gz install.log.syslog test1 test5 time.sh
ceu file3 tes test1.txt test5.txt
eername file3.zip test test2.txt test6
[root@demo6 ~]# zcat file1.gz
anaconda-ks.cfg
ceu
eername
err
erro
file1
file3
file4
fle1
install.log
install.log.syslog
test1
(3)bzip2&bunzip2
bzip2 被压缩的文件名:压缩后,源文件不保留
[root@demo6 ~]# bzip2 fle1
[root@demo6 ~]# ls
abc err fle1.bz2 TEST test3.txt test.zip
anaconda-ks.cfg erro install.log test0.txt test4.txt time=date"+"%H.log
app file1.gz install.log.syslog test1 test5 time.sh
ceu file3 tes test1.txt test5.txt
eername file3.zip test test2.txt test6
bzip2 -c test.txt >test.txt.bz2 : 压缩后,源文件保留
[root@demo6 ~]# bzip2 tes
[root@demo6 ~]# ls
abc err fle1.bz2 TEST test3.txt test.zip
anaconda-ks.cfg erro install.log test0.txt test4.txt time=date"+"%H.log
app file1.gz install.log.syslog test1 test5 time.sh
ceu file3 tes.bz2 test1.txt test5.txt
eername file3.zip test test2.txt test6
bunzip2 test.txt.bz2 :解压文件
[root@demo6 ~]# bunzip2 fle1.bz2
[root@demo6 ~]# ls
abc err fle1 TEST test3.txt test.zip
anaconda-ks.cfg erro install.log test0.txt test4.txt time=date"+"%H.log
app file1.gz install.log.syslog test1 test5 time.sh
ceu file3 tes.bz2 test1.txt test5.txt
eername file3.zip test test2.txt test6
bunzip2 == gzip -d
不解压查看:
bzcat test5.txt.gz
[root@demo6 ~]# bzcat tes.bz2
(4)xz&unxz
xz 被压缩的文件名:压缩后,源文件不保留
[root@demo6 ~]# ls
abc err fle1 TEST test3.txt test.zip
anaconda-ks.cfg erro install.log test0.txt test4.txt time=date"+"%H.log
app file1.gz install.log.syslog test1 test5 time.sh
ceu file3 tes.bz2 test1.txt test5.txt
eername file3.zip test test2.txt test6
[root@demo6 ~]# xz fle1
[root@demo6 ~]# ls
abc err fle1.xz TEST test3.txt test.zip
anaconda-ks.cfg erro install.log test0.txt test4.txt time=date"+"%H.log
app file1.gz install.log.syslog test1 test5 time.sh
ceu file3 tes.bz2 test1.txt test5.txt
eername file3.zip test test2.txt test6
xzcat test.txt.xz :查看压缩后的文件内容
[root@demo6 ~]# ls
abc err fle1.xz TEST test3.txt test.zip
anaconda-ks.cfg erro install.log test0.txt test4.txt time=date"+"%H.log
app file1.gz install.log.syslog test1 test5 time.sh
ceu file3 tes.bz2 test1.txt test5.txt
eername file3.zip test test2.txt test6
[root@demo6 ~]# xzcat fle1.xz
anaconda-ks.cfg
file1
file3
fle1
install.log
install.log.syslog
test1
xz dir/* :压缩目录
unxz test.txt.xz
xz -d test.txt.xz
xz -d dir/* :解压目录
作业:
练习:完成以下任务
1、新建系统组mariadb, 新建系统用户mariadb, 属于mariadb组,要求其没有家目录,且shell为/sbin/nologin;尝试root切换至用户,查看其命令提示符。
2、新建GID为5000的组nebulaedu,新建用户gentoo,要求其家目录为/users/gentoo,密码同用户名;
3、新建用户fedora,其家目录为/users/fedora,密码同用户名;
4、新建用户www, 其家目录为/users/www;删除www用户,但保留其家目录;
5、为用户gentoo和fedora新增附加组nebulaedu;
6、复制目录/var/log至/tmp/目录,修改/tmp/log及其内部的所有文件--的属组为nebulaedu,并让属组对目录本身拥有写权限
研发部开发人员David和Peter属于组A,行政部人员Jack和Mike属于组B;
1.建立目录“/project_a”,该目录里面的文件只能由研发部开发人员读取、增加、删除、修改以及执行,其他用
户不能对该目录进行任何的访问操作;并要求在此目录下创建的文件研发组内成员可以互相访问
[root@demo6 /]# mkdir project_a
[root@demo6 /]# ll
total 86
dr-xr-xr-x. 2 root root 4096 Jul 22 21:47 bin
dr-xr-xr-x. 5 root root 1024 Jul 22 21:49 boot
drwxr-xr-x. 19 root root 3780 Jul 31 10:08 dev
drwxr-xr-x. 62 root root 4096 Jul 31 11:14 etc
drwxr-xr-x. 10 root root 4096 Jul 31 10:43 home
dr-xr-xr-x. 8 root root 4096 Jul 22 21:46 lib
dr-xr-xr-x. 7 root root 12288 Jul 22 21:47 lib64
drwx------. 2 root root 16384 Jul 22 21:42 lost+found
dr-xr-xr-x. 7 root root 4096 May 23 2016 media
drwxr-xr-x. 2 root root 4096 Sep 23 2011 mnt
drwxr-xr-x. 2 root root 4096 Sep 23 2011 opt
dr-xr-xr-x. 93 root root 0 Jul 31 10:07 proc
drwxr-xr-x. 2 root root 4096 Jul 31 12:40 project_a
dr-xr-x---. 5 root root 4096 Jul 31 12:36 root
dr-xr-xr-x. 2 root root 4096 Jul 22 21:49 sbin
drwxr-xr-x. 7 root root 0 Jul 31 10:07 selinux
drwxr-xr-x. 2 root root 4096 Sep 23 2011 srv
drwxr-xr-x 13 root root 0 Jul 31 10:07 sys
drwxrwxrwt. 4 root root 4096 Jul 31 12:01 tmp
drwxr-xr-x. 13 root root 4096 Jul 22 21:43 usr
drwxr-xr-x. 18 root root 4096 Jul 22 21:49 var
[root@demo6 /]# chown :A project_a
[root@demo6 /]# ll
total 86
dr-xr-xr-x. 2 root root 4096 Jul 22 21:47 bin
dr-xr-xr-x. 5 root root 1024 Jul 22 21:49 boot
drwxr-xr-x. 19 root root 3780 Jul 31 10:08 dev
drwxr-xr-x. 62 root root 4096 Jul 31 11:14 etc
drwxr-xr-x. 10 root root 4096 Jul 31 10:43 home
dr-xr-xr-x. 8 root root 4096 Jul 22 21:46 lib
dr-xr-xr-x. 7 root root 12288 Jul 22 21:47 lib64
drwx------. 2 root root 16384 Jul 22 21:42 lost+found
dr-xr-xr-x. 7 root root 4096 May 23 2016 media
drwxr-xr-x. 2 root root 4096 Sep 23 2011 mnt
drwxr-xr-x. 2 root root 4096 Sep 23 2011 opt
dr-xr-xr-x. 93 root root 0 Jul 31 10:07 proc
drwxr-xr-x. 2 root A 4096 Jul 31 12:40 project_a
dr-xr-x---. 5 root root 4096 Jul 31 12:36 root
dr-xr-xr-x. 2 root root 4096 Jul 22 21:49 sbin
drwxr-xr-x. 7 root root 0 Jul 31 10:07 selinux
drwxr-xr-x. 2 root root 4096 Sep 23 2011 srv
drwxr-xr-x 13 root root 0 Jul 31 10:07 sys
drwxrwxrwt. 4 root root 4096 Jul 31 12:01 tmp
drwxr-xr-x. 13 root root 4096 Jul 22 21:43 usr
drwxr-xr-x. 18 root root 4096 Jul 22 21:49 var
[root@demo6 /]# chmod 070 project_a
[root@demo6 /]# ll
total 86
dr-xr-xr-x. 2 root root 4096 Jul 22 21:47 bin
dr-xr-xr-x. 5 root root 1024 Jul 22 21:49 boot
drwxr-xr-x. 19 root root 3780 Jul 31 10:08 dev
drwxr-xr-x. 62 root root 4096 Jul 31 11:14 etc
drwxr-xr-x. 10 root root 4096 Jul 31 10:43 home
dr-xr-xr-x. 8 root root 4096 Jul 22 21:46 lib
dr-xr-xr-x. 7 root root 12288 Jul 22 21:47 lib64
drwx------. 2 root root 16384 Jul 22 21:42 lost+found
dr-xr-xr-x. 7 root root 4096 May 23 2016 media
drwxr-xr-x. 2 root root 4096 Sep 23 2011 mnt
drwxr-xr-x. 2 root root 4096 Sep 23 2011 opt
dr-xr-xr-x. 93 root root 0 Jul 31 10:07 proc
d---rwx---. 2 root A 4096 Jul 31 12:40 project_a
dr-xr-x---. 5 root root 4096 Jul 31 12:36 root
dr-xr-xr-x. 2 root root 4096 Jul 22 21:49 sbin
drwxr-xr-x. 7 root root 0 Jul 31 10:07 selinux
drwxr-xr-x. 2 root root 4096 Sep 23 2011 srv
drwxr-xr-x 13 root root 0 Jul 31 10:07 sys
drwxrwxrwt. 4 root root 4096 Jul 31 12:01 tmp
drwxr-xr-x. 13 root root 4096 Jul 22 21:43 usr
drwxr-xr-x. 18 root root 4096 Jul 22 21:49 var
2.建立目录“/project_b”,该目录里面的文件只能由行政部人员读取、增加、删除、修改以及执行,其他用户不
能对该目录进行任何的访问操作;要求在此目录下创建的文件行政部人员只能删除自己的文件,不得删除其他人员文件
[root@demo6 /]# mkdir project_b
[root@demo6 /]# chown :B project_b
[root@demo6 /]# ll
total 90
dr-xr-xr-x. 2 root root 4096 Jul 22 21:47 bin
dr-xr-xr-x. 5 root root 1024 Jul 22 21:49 boot
drwxr-xr-x. 19 root root 3780 Jul 31 10:08 dev
drwxr-xr-x. 62 root root 4096 Jul 31 11:14 etc
drwxr-xr-x. 10 root root 4096 Jul 31 10:43 home
dr-xr-xr-x. 8 root root 4096 Jul 22 21:46 lib
dr-xr-xr-x. 7 root root 12288 Jul 22 21:47 lib64
drwx------. 2 root root 16384 Jul 22 21:42 lost+found
dr-xr-xr-x. 7 root root 4096 May 23 2016 media
drwxr-xr-x. 2 root root 4096 Sep 23 2011 mnt
drwxr-xr-x. 2 root root 4096 Sep 23 2011 opt
dr-xr-xr-x. 93 root root 0 Jul 31 10:07 proc
d---rwx---. 2 root A 4096 Jul 31 12:40 project_a
drwxr-xr-x. 2 root B 4096 Jul 31 12:43 project_b
dr-xr-x---. 5 root root 4096 Jul 31 12:36 root
dr-xr-xr-x. 2 root root 4096 Jul 22 21:49 sbin
drwxr-xr-x. 7 root root 0 Jul 31 10:07 selinux
drwxr-xr-x. 2 root root 4096 Sep 23 2011 srv
drwxr-xr-x 13 root root 0 Jul 31 10:07 sys
drwxrwxrwt. 4 root root 4096 Jul 31 12:01 tmp
drwxr-xr-x. 13 root root 4096 Jul 22 21:43 usr
drwxr-xr-x. 18 root root 4096 Jul 22 21:49 var
[root@demo6 /]# chmod 1070 project_b
[root@demo6 /]# ll
total 90
dr-xr-xr-x. 2 root root 4096 Jul 22 21:47 bin
dr-xr-xr-x. 5 root root 1024 Jul 22 21:49 boot
drwxr-xr-x. 19 root root 3780 Jul 31 10:08 dev
drwxr-xr-x. 62 root root 4096 Jul 31 11:14 etc
drwxr-xr-x. 10 root root 4096 Jul 31 10:43 home
dr-xr-xr-x. 8 root root 4096 Jul 22 21:46 lib
dr-xr-xr-x. 7 root root 12288 Jul 22 21:47 lib64
drwx------. 2 root root 16384 Jul 22 21:42 lost+found
dr-xr-xr-x. 7 root root 4096 May 23 2016 media
drwxr-xr-x. 2 root root 4096 Sep 23 2011 mnt
drwxr-xr-x. 2 root root 4096 Sep 23 2011 opt
dr-xr-xr-x. 93 root root 0 Jul 31 10:07 proc
d---rwx---. 2 root A 4096 Jul 31 12:40 project_a
d---rwx--T. 2 root B 4096 Jul 31 12:43 project_b
dr-xr-x---. 5 root root 4096 Jul 31 12:36 root
dr-xr-xr-x. 2 root root 4096 Jul 22 21:49 sbin
drwxr-xr-x. 7 root root 0 Jul 31 10:07 selinux
drwxr-xr-x. 2 root root 4096 Sep 23 2011 srv
drwxr-xr-x 13 root root 0 Jul 31 10:07 sys
drwxrwxrwt. 4 root root 4096 Jul 31 12:01 tmp
drwxr-xr-x. 13 root root 4096 Jul 22 21:43 usr
drwxr-xr-x. 18 root root 4096 Jul 22 21:49 var