1.linux文本编辑工具vi,vim ————三种模式
vi === vim
vim的模式有三种,命令模式,输入模式,末行模式
1.1 命令模式:
vim 文件名
1.1.1 光标移动
h:左
l:右
j:下
k:上
数字 +h/l/j/k 移动多少格
w 跳到文件内容中下一个单词的开头
e 跳到文件内容中下一个单词的末尾
b 跳到文件内容中上一个单词的开头
数字 + w/e/b 调多少个单词
在一行中
行首:home,0,^
—— hone,0 调到一行的最前面, ^,略过首行空格跳到最前面
行尾:end,$
在行间
gg :跳到首行
Shift + g 或者G :跳到尾行
行号 + gg 跳到指定行
1.1.2 翻页移动
PgDO CTRL + F 向下翻页
pgUP CTRL + B 向上翻页
1.1.3 删除操作
x,delete: 删除光标所在的字符
dd :删除整行
数字 + dd 删除当前行开始后的几行
行间之间的删除 光标之后 D,d +$(连光标和光标之后的内容一起删除)
光标之前 d + ^
1.1.4 复制粘贴操作
yy:复制光标所在行
数字 + yy :复制光标及光标一下的几行
粘贴
p粘贴到光标处之后
P张贴到光标处之前
1.1.5 查找操作
/要查找的内容 ----上------> 下 光标所在位置
?要查找的内容 -----下------> 上 光标所在位置
1.1.6 替换操作
r : 替换光标所在字符
R:整行替换字符 ;ESC退出。
1.1.7 撤销操作
u:撤销一次操作
数字u:撤下多次操作
Ctrl+r 恢复上一次的操作操作
1.1.8 重复上一次操作
.
重复上一次操作
1.1.9 可视化
v:按照字符选择
V:安装矩形选择,(选一行)
(补充)ctrl +v (安装字符选,可以上下左右调整所选区域大小)
1.2 命令模式 ------>编辑模式(输入模式)
1.2.1 进入输入模式
i,I : i 光标前 ,I行首
a,A :a 光标后,A行尾
o,O :o 光标下一行,O光标上一行
1.3 末行模式
1.3.1 保存文件
:wq 保存退出
:w 保存
: w /文件路径/文件名 (把当前文件的内容另存为其他文件<可以是新文件>)
1.3.2 打开文件
:e /文件路径/文件名 打开一个新的文件 编辑
e ~/passwd1 ===> e /root/passwd1 ===>e ./paaswd1
r /文件路径/文件名 在光标处读入一个文件内容
r /etc/passwd
1.3.3 替换操作
s /old/new/ 替换光标所在行的第一个匹配的字段
s /old/now/g 替换光标所在行的所有的字段
行号1,行号2 s/old/new/ 指定行间所在行的第一个匹配的字段
行号1,行号2 s/old/new/g 指定行间所在行的所有字段
% s /old/new/ 全文所行的第一个匹配的字段
% s /old/new/g 全文所在行的所有字段
% s /old/new/gc c:提示用户是否替换
% s /old/new/gci i:忽略大小写
:! + shell命令 在文档中执行shell命令。
1.4 打开文件
vim
vim + 行号 打开文件,并定位于第几行
vim +/关键字 打开文件,定位于第一次被关键字匹配到的行首
上次文件保存时光标所在位置
1.5关闭文件
:q 退出
ZZ ,命令模式关闭文件
1.6 vim配置文件
在个人用户下面配置文件和在全局下配置文件
.vimrc 配置在用户的家目录下,/home/user1/
set nu 设置行号
set ai 有缩进
set nohlsearch 取消高亮部分
2. 进入vim /etc/vimrc 配置全局
2.压缩&解压缩
压缩原理:
在讲压缩工具之前,我们先来讲解一下压缩原理。目前我们使用的计算机系统是使用bytes单位计量的,但是实际上,计算机中最小的计量单位是bits,我们知道1 byte = 8 bits,加入我们在记录一个数字1的时候是如何来记录的?
我们之前讲过一个byte是8bits 我们可以理解成如下样子
一个空格代表一个bit,1byte就是8个空格子,当我们在存放数字1的时候,根据二级制计数,在这里存放的是00000001,7个0一个1,实际上前面7个0都是空的,但是由于要满足我们操作系统的存取方式,必须以8位为单位存储,所会造成有一些空间并没有填满。压缩就是将这些没有使用到的空间丢出来,让文件的占用空间变小,这就是压
缩技术。
解压缩技术就是将压缩完的数据还原成未压缩的状态。
压缩比就是指压缩后与压缩前的文件所占用磁盘空间的大小比值。
我们常见的网站数据传输一般都是使用的压缩技术,数据在网络传输过程中是使用的压缩数据,当压缩数据达到用户主机时,通过解压缩,再展示出来。
(1)zip,unzip 9(既归档又压缩,还可以压缩目录)
先做准备
[root@CentOS6 ~]# cd test
[root@CentOS6 test]# ll
total 0
[root@CentOS6 test]# for i in {1..8};do echo "test$i" > test$i.txt;done
[root@CentOS6 test]# ls
test1.txt test2.txt test3.txt test4.txt test5.txt test6.txt test7.txt test8.txt
[root@CentOS6 test]# mkdir dir1
[root@CentOS6 test]# cp /etc/fstab dir1/
zip 压缩文件名(test.zip) 要被压缩的文件名字
[root@CentOS6 test]# zip test1.zip test1.txt
adding: test1.txt (stored 0%)
[root@CentOS6 test]# ls
dir1 test1.txt test1.zip test2.txt test3.txt test4.txt test5.txt test6.txt test7.txt test8.txt
压缩率为最高压缩test2.txt
[root@CentOS6 test]# zip -9 test2.zip test2.txt
adding: test2.txt (stored 0%)
[root@CentOS6 test]# ls
dir1 test1.txt test1.zip test2.txt test2.zip test3.txt test4.txt test5.txt test6.txt test7.txt test8.txt
将目录dir1连同目录下的文件一起压缩
[root@CentOS6 test]# zip -r dir1.zip dir1
adding: dir1/ (stored 0%)
adding: dir1/fstab (deflated 56%)
[root@CentOS6 test]# ls
dir1 dir1.zip test1.txt test1.zip test2.txt test2.zip test3.txt test4.txt test5.txt test6.txt test7.txt test8.txt
[root@CentOS6 test]# cd dir1
[root@CentOS6 dir1]# ls
fstab
[root@CentOS6 test]# unzip -v dir1.zip
Archive: dir1.zip
Length Method Size Cmpr Date Time CRC-32 Name
-------- ------ ------- ---- ---------- ----- -------- ----
0 Stored 0 0% 07-31-2019 22:31 00000000 dir1/
899 Defl:N 394 56% 07-31-2019 22:31 ce46b6ee dir1/fstab
-------- ------- --- -------
899 394 56% 2 files
zip -m test1.zip test2.txt 把test2 添加到test1.zip
[root@CentOS6 test]# zip -m test1.zip test2.txt
adding: test2.txt (stored 0%)
[root@CentOS6 test]# ls
dir1 dir1.zip test1.txt test1.zip test2.zip test3.txt test4.txt test5.txt test6.txt test7.txt test8.txt
[root@CentOS6 test]# unzip -v test1.zip
Archive: test1.zip
Length Method Size Cmpr Date Time CRC-32 Name
-------- ------ ------- ---- ---------- ----- -------- ----
6 Stored 6 0% 07-31-2019 22:29 7c59371b test1.txt
6 Stored 6 0% 07-31-2019 22:29 577464d8 test2.txt
-------- ------- --- -------
12 12 0% 2 files
zip -d test1.zip test2.txt 把test2从test1.zip删除掉
[root@CentOS6 test]# zip -d test1.zip test2.txt
deleting: test2.txt
[root@CentOS6 test]# ls
dir1 dir1.zip test1.txt test1.zip test2.zip test3.txt test4.txt test5.txt test6.txt test7.txt test8.txt
zip test.zip *.txt -x test1.txt 除test1以外,将所有.txt的文件压缩
[root@CentOS6 test]# zip test.zip *.txt -x test1.txt
adding: test3.txt (stored 0%)
adding: test4.txt (stored 0%)
adding: test5.txt (stored 0%)
adding: test6.txt (stored 0%)
adding: test7.txt (stored 0%)
adding: test8.txt (stored 0%)
[root@CentOS6 test]# ls
dir1 dir1.zip test1.txt test1.zip test2.zip test3.txt test4.txt test5.txt test6.txt test7.txt test8.txt test.zip
[root@CentOS6 test]# unzip -v test.zip
Archive: test.zip
Length Method Size Cmpr Date Time CRC-32 Name
-------- ------ ------- ---- ---------- ----- -------- ----
6 Stored 6 0% 07-31-2019 22:29 4e6f5599 test3.txt
6 Stored 6 0% 07-31-2019 22:29 012ec35e test4.txt
6 Stored 6 0% 07-31-2019 22:29 1835f21f test5.txt
6 Stored 6 0% 07-31-2019 22:29 3318a1dc test6.txt
6 Stored 6 0% 07-31-2019 22:29 2a03909d test7.txt
6 Stored 6 0% 07-31-2019 22:29 ad9b8c52 test8.txt
-------- ------- --- -------
36 36 0% 6 files
zip -r /dir 递归压缩
unzip test1.zip
[root@CentOS6 test]# unzip test1.zip
Archive: test1.zip
replace test1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
extracting: test1.txt
[root@CentOS6 test]# ls
dir1 dir1.zip test1.txt test1.zip test2.zip test3.txt test4.txt test5.txt test6.txt test7.txt test8.txt test.zip
unzip test.zip -d /压缩目录
[root@CentOS6 test]# unzip test.zip -d dir1
Archive: test.zip
extracting: dir1/test3.txt
extracting: dir1/test4.txt
extracting: dir1/test5.txt
extracting: dir1/test6.txt
extracting: dir1/test7.txt
extracting: dir1/test8.txt
[root@CentOS6 test]# cd dir1
[root@CentOS6 dir1]# ls
fstab test3.txt test4.txt test5.txt test6.txt test7.txt test8.txt
unzip -v test1.zip 不解压,查看压缩内容。
(2)gzip,gunzip
gzip 被压缩的文件名 ,压缩后,源文件不保留
[root@CentOS6 test]# ls
dir1 dir1.zip test1.txt test1.zip test2.zip test3.txt test4.txt test5.txt test6.txt test7.txt test8.txt test.zip
[root@CentOS6 test]# gzip test1.txt
[root@CentOS6 test]# ls
dir1 dir1.zip test1.txt.gz test1.zip test2.zip test3.txt test4.txt test5.txt test6.txt test7.txt test8.txt test.zip
gzip -c test.txt > test.txt.gz 压缩,保留源文件
[root@CentOS6 test]# gzip -c test3.txt > test3.txt.gz
[root@CentOS6 test]# ls
dir1 dir1.zip test1.txt.gz test1.zip test2.txt.gz test2.zip test3.txt test3.txt.gz test4.txt test5.txt test6.txt test7.txt test8.txt test.zip
gzip -r 目录 压缩了目录下的所有内容。
gunzip test.txt.gz
gunzip -c test.txt.gz > /root/test.txt 解压缩到特定目录,并且保留源文件
[root@CentOS6 test]# gunzip -c test3.txt.gz > /tmp/test.txt
[root@CentOS6 test]# cat /tmp/test.txt
test3
[root@CentOS6 test]# ls
dir1 dir1.zip test1.txt test1.zip test2.txt.gz test2.zip test3.txt test3.txt.gz test4.txt test5.txt test6.txt test7.txt test8.txt test.zip
gunzip === gzip -d
不解压查看
[root@localhost test1]# zless test5.txt.gz
[root@localhost test1]# zless test5.txt.gz
(3)bzip2,bunzip2 (比gzip有更高的压缩率)
bzip2 被压缩的文件名 ,压缩后,源文件不保留
bzip2 -c test.txt > test.txt.bz2 压缩,保留源文件
bunzip2 test.txt.bz2
bunzip2 === bzip2 -d
不解压查看
[root@localhost test1]# bzcat test5.txt.bz2
(4)xz,unxz
xz 被压缩的文件名 ,压缩后,源文件不保留
xz dir/*
unxz test.txt.xz
xz -d test.txt.xz
xz -d dir/*
查看压缩后的文件内容
zcat zless test1.zip test5.txt.gz
bzcat test.txt.bz2
bzless test.txt.xz
xzcat xzca test.txt.xz
练习:完成以下任务
1、新建系统组mariadb, 新建系统用户mariadb, 属于mariadb组,要求其没有家目录,且shell为/sbin/nologin;尝试root切换至用户,查看其命令提示符;
[root@CentOS6 ~]# groupadd -r mariadb
[root@CentOS6 ~]# useradd -g mariadb -s /sbin/nologin -M mariadb
[root@CentOS6 ~]# id mariadb
uid=40008(mariadb) gid=499(mariadb) groups=499(mariadb)
[root@CentOS6 ~]# su mariadb
This account is currently not available.
[root@CentOS6 ~]# su - mariadb
su: warning: cannot change directory to /home/mariadb: No such file or directory
This account is currently not available.
2、新建GID为5000的组nebulaedu,新建用户gentoo,要求其家目录为/users/gentoo,密码同用户名;
[root@CentOS6 ~]# groupadd -g 5000 nebulaedu
[root@CentOS6 ~]# useradd -d /users/gentoo gentoo
[root@CentOS6 ~]# passwd gentoo
Changing password for user gentoo.
New password:
BAD PASSWORD: it is based on a dictionary word
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.
3、新建用户fedora,其家目录为/users/fedora,密码同用户名
[root@CentOS6 ~]# useradd -d /users/fedora fedora
[root@CentOS6 ~]# passwd fedora
Changing password for user fedora.
New password:
BAD PASSWORD: it is based on a dictionary word
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.
4、新建用户www, 其家目录为/users/www;删除www用户,但保留其家目录;
[root@CentOS6 ~]# useradd -d /users/www www
[root@CentOS6 ~]# userdel www
[root@CentOS6 ~]# ll -d /users/www
drwx------. 2 40011 40011 4096 Jul 31 18:49 /users/www
5、为用户gentoo和fedora新增附加组nebulaedu;
[root@CentOS6 ~]# usermod -G nebulaedu gentoo
[root@CentOS6 ~]# usermod -G nebulaedu fedora
[root@CentOS6 ~]# id gentoo
uid=40009(gentoo) gid=40009 groups=40009,5000(nebulaedu)
[root@CentOS6 ~]# id fedora
uid=40010(fedora) gid=40010(fedora) groups=40010(fedora),5000(nebulaedu)
6、复制目录/var/log至/tmp/目录,修改/tmp/log及其内部的所有文件的属组为nebulaedu,并让属组对目录本身拥有写权限
[root@CentOS6 log]# cp -r /var/log /tmp/
[root@CentOS6 log]# ll -d /tmp/log
drwxr-xr-x. 3 root root 4096 Jul 31 19:04 /tmp/log
[root@CentOS6 log]# chown -R :nebulaedu /tmp/log
[root@CentOS6 log]# ll /tmp/log
total 2040
-rw-------. 1 root nebulaedu 2368 Jul 31 19:04 anaconda.ifcfg.log
-rw-------. 1 root nebulaedu 22368 Jul 31 19:04 anaconda.log
-rw-------. 1 root nebulaedu 35539 Jul 31 19:04 anaconda.program.log
-rw-------. 1 root nebulaedu 173504 Jul 31 19:04 anaconda.storage.log
-rw-------. 1 root nebulaedu 148133 Jul 31 19:04 anaconda.syslog
-rw-------. 1 root nebulaedu 26433 Jul 31 19:04 anaconda.xlog
-rw-------. 1 root nebulaedu 27507 Jul 31 19:04 anaconda.yum.log
drwxr-x---. 2 root nebulaedu 4096 Jul 31 19:04 audit
-rw-r--r--. 1 root nebulaedu 2394 Jul 31 19:04 boot.log
-rw-------. 1 root nebulaedu 4224 Jul 31 19:04 btmp
-rw-------. 1 root nebulaedu 8490 Jul 31 19:04 cron
-rw-------. 1 root nebulaedu 16531 Jul 31 19:04 cron-20190728
-rw-r--r--. 1 root nebulaedu 87536 Jul 31 19:04 dmesg
-rw-r--r--. 1 root nebulaedu 87536 Jul 31 19:04 dmesg.old
-rw-r--r--. 1 root nebulaedu 179655 Jul 31 19:04 dracut.log
-rw-r--r--. 1 root nebulaedu 11683504 Jul 31 19:04 lastlog
-rw-------. 1 root nebulaedu 836 Jul 31 19:04 maillog
-rw-------. 1 root nebulaedu 3762 Jul 31 19:04 maillog-20190728
-rw-------. 1 root nebulaedu 193915 Jul 31 19:04 messages
-rw-------. 1 root nebulaedu 871101 Jul 31 19:04 messages-20190728
-rw-------. 1 root nebulaedu 32197 Jul 31 19:04 secure
-rw-------. 1 root nebulaedu 17104 Jul 31 19:04 secure-20190728
-rw-------. 1 root nebulaedu 0 Jul 31 19:04 spooler
-rw-------. 1 root nebulaedu 0 Jul 31 19:04 spooler-20190728
-rw-------. 1 root nebulaedu 0 Jul 31 19:04 tallylog
-rw-r--r--. 1 root nebulaedu 60288 Jul 31 19:04 wtmp
-rw-------. 1 root nebulaedu 1111 Jul 31 19:04 yum.log
研发部开发人员David和Peter属于组A,行政部人员Jack和Mike属于组B;
1.建立目录“/project_a”,该目录里面的文件只能由研发部开发人员读取、增加、删除、修改以及执行,其他用户不能对该目录进行任何的访问操作;并要求在此目录下创建的文件研发组内成员可以互相访问
[root@CentOS6 ~]# mkdir /project_a
[root@CentOS6 ~]# groupadd A
[root@CentOS6 ~]# groupadd B
[root@CentOS6 ~]# useradd -g A David
[root@CentOS6 ~]# useradd -g A Peter
[root@CentOS6 ~]# useradd -g B Jack
[root@CentOS6 ~]# useradd -g B Mike
[root@CentOS6 ~]# chown -R :A /project_a
[root@CentOS6 ~]# chmod 770 /project_a
[root@CentOS6 ~]# su David
[David@CentOS6 root]$ cd /project_a
[David@CentOS6 project_a]$ touch 456
[David@CentOS6 project_a]$ echo "asddfg" > 456
[David@CentOS6 project_a]$ cat 456
asddfg
[David@CentOS6 project_a]$ su Jack
Password:
[Jack@CentOS6 project_a]$ ll
ls: cannot open directory .: Permission denied
[Jack@CentOS6 project_a]$ cat 456
cat: 456: Permission denied
[Jack@CentOS6 project_a]$ su Peter
Password:
[Peter@CentOS6 project_a]$ ll
total 4
-rw-r--r--. 1 David A 7 Jul 31 20:21 456
[Peter@CentOS6 project_a]$ cat 456
asddfg
[Peter@CentOS6 project_a]$ touch 789
[Peter@CentOS6 project_a]$ echo "zxcvv" > 789
[Peter@CentOS6 project_a]$ su David
Password:
[David@CentOS6 project_a]$ cat 789
zxcvv
2.建立目录“/project_b”,该目录里面的文件只能由行政部人员读取、增加、删除、修改以及执行,其他用户不能对该目录进行任何的访问操作;要求在此目录下创建的文件行政部人员只能删除自己的文件,不得删除其他人员文件
[root@CentOS6 ~]# mkdir /project_b
[root@CentOS6 ~]# chown -R :B /project_b
[root@CentOS6 ~]# chmod 1770 /project_b
[root@CentOS6 project_b]# su Jack
[Jack@CentOS6 project_b]$ touch asd
[Jack@CentOS6 project_b]$ touch zxc
[David@CentOS6 project_b]$ cat asd
cat: asd: Permission denied
[David@CentOS6 project_b]$ rm asd
rm: cannot remove `asd': Permission denied.
[David@CentOS6 project_b]$ touch 123
touch: cannot touch `123': Permission denied
[David@CentOS6 project_b]$ su Jack
Password:
[Jack@CentOS6 project_b]$ rm zxc
[Jack@CentOS6 project_b]$ ls
asd
[Jack@CentOS6 project_b]$ su Mike
Password:
[Mike@CentOS6 project_b]$ touch 123
[Mike@CentOS6 project_b]$ su Jack
Password:
[Jack@CentOS6 project_b]$ rm 123
rm: remove write-protected regular empty file `123'? y
rm: cannot remove `123': Operation not permitted