新的一周又开始了学习旅程,按照惯例还是先行测验一些上一周的学习水平。
1.简单回顾一下上次课堂内容关于磁盘管理的几个命令:1.1df命令1.2 du命令1.3/1.4 磁盘分区1.5/1.6 磁盘格式化1.7/1.8 磁盘挂载.
小知识点开机自动挂载的两种方式:
a. 在/etc/fstab 中配置:
[root@localhost ~]# cat /etc/fstab
# /etc/fstab
# Created by anaconda on Thu Jul 19 09:09:17 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=01d8fe8c-732c-4c8e-87e6-a81fe67aa49f / xfs defaults 0 0
UUID=5f98ed23-faaf-44d7-a031-903fd34e138e /boot xfs defaults 0 0
UUID=009c9b9f-c957-4cbf-8ab1-4ede84f9fa80 swap swap defaults 0 0
例:/dev/sdb1 分区名 /mnt 挂载点 ext4 分区格式 defaults 挂载参数 0 是否被dump备份 0是否自检
b. 在/etc/rc.local中配置:
[root@localhost ~]# cat /etc/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
例: /usr/bin/mount=/dev/sdb1 /mnt
2.LVM:LVM是logical volume manager的缩写(逻辑分区),方便对于对圈组、逻辑卷的大小进行调整,更进一步调整文件系统的大小。 优点:在使用很多硬盘的大系统中,使用LVM主要是方便管理、增加了系统的扩展性。
现在来看一下pv,vg,lv,pe之间的关系。
2.1 扩容操作:先简单的介绍一下步骤 pv->vg->lv->pe
2.1.1 首先我们用“[root@localhost ~]# pvcreate /dev/sdb1”创建一个物理卷可以用“pvs”查看物理卷,再用“vgcreate”创建卷组“vgcreate vg1 /dev/sdb1 /dev/sdb2,vg1是卷组的名字”这里也可以用“vgs”查看卷组,然后创建逻辑卷“[root@localhost ~]# lvcreate -L 100M -n lv1 vg1”(lvcreate创建逻辑卷,-L 100M指定大小,-n lv1 逻辑卷的名字,vg1从卷组里面调用出来)。
2.1.2 格式化成mkfs.ext4,适合做扩容“[root@localhost ~]# mkfs.ext4 /dev/vg1/lv1”,挂载逻辑卷”[root@localhost ~]# mount /dev/vg1/lv1 /mnt/ “#挂载到/mnt/下。
2.1.3 扩容逻辑卷:重新设置卷大小,先umount”[root@localhost ~]# lvresize -L 200M /dev/vg1/lv1“,检查磁盘错误 (ext4执行)”[root@localhost ~]# e2fsck -f /dev/vg1/lv1 “,更新逻辑卷信息,如果不更新,再挂载的时候无法更新新的大小(ext4执行)
”[root@localhost ~]# resize2fs /dev/vg1/lv1 “。
2.1.4 挂载,再查看文件大小”mount /dev/vg1/lv1 /mnt/“
2.2 缩减逻辑卷(xfs不支持)这里其实跟扩容就是倒过来做一遍。
2.2.1 先umount 不挂载”[root@localhost ~]# umount /mnt/“
2.2.2 检查磁盘错误”[root@localhost ~]# e2fsck -f /dev/vg1/lv1“
2.2.3更新逻辑卷信息”[root@localhost ~]# resize2fs /dev/vg1/lv1 100M“
2.2.4重新设置卷大小”[root@localhost ~]# lvresize -L 100M /dev/vg1/lv1“
2.2.5 接下来挂载”[root@localhost ~]# mount /dev/vg1/lv1 /mnt/“
2.3 xfs文件扩容
2.3.1 首先 更新逻辑卷信息,需取消挂载umount ”[root@localhost ~]# mkfs.xfs -f /dev/vg1/lv1“
2.3.2 扩容大小300M ”[root@localhost ~]# xfs_growfs -L 300M /dev/vg1/lv1“
2.3.3xfs文件系统需要执行 xfs_growfs /dev/vg1/lv1
2.3.4 扩展卷组:再重新设置卷大小 ”[root@localhost ~]# lvresize -L 100M /dev/vg1/lv1“
3 vim是 vi 的升级版,号称史上最好的文本编辑器。这里整理的是vim的基本知识,主要包括:普通模式基本操作 编辑模式基本操作 命令模式基本操作.
3.1vim安装:yum install -y vim-enhanced
3.2 普通模式基本操作(这个是重点)
3.2.1 使用h、l、k、j 代表左、右、上、下,如下图所示:
2.0、shift+6移动到本行行首;shift+4 移动到本行末尾。
3. gg 移动到第一行;G 移动到最后一行;nG移动到第n行(n代表数字)
1.x表示向后删除一个字符;X表示向前删除一个字符;nx表示向后删除n的字符(n代表数字)
2.dd 删除光标所在行;ndd 删除光标所在行之后的n行(n代表数字)
3.2.3 复制粘贴字符或者行
1.yy复制光标所在行;nyy 从光标所载行开始,向下复制n行;
2.小写 p 表示从光标所在行开始向后粘贴;大写 P 表示向前粘贴;
3.按 v 移动光标会选中指定文字,之后按 y 复制,按 p 粘贴;
3.3 进入编辑模式
快捷键 |
解释 |
i |
在当前字符插入 |
I |
在光标所在行的行首插入 |
a |
在光标后插入 |
A |
在光标所在行的末尾插入 |
o |
在当前行后插入新的一行 |
O |
在当前行前插入新的一行 |
3.4 vim命令模式
快捷键 |
解释 |
/word |
在光标之后查找关键词,如果存在多个匹配项,按n向后搜索 |
?word |
在光标之前查找关键词,如果存在多个匹配项,按n向前搜索 |
:1,$s/word1/word2/g |
(难点)将文档中的word1替换为word2,不加g则只替换每行的第一个word1 |
:m,ns/word1/word2/g |
(难点)将第m行和第n行之间的word1替换为word2,不加g则只替换每行的第一个word1 |
3.5打开/保存/退出/改变文件这几个标注的是用得比较多的。
快捷键 |
解释 |
:e [path to file] |
打开一个文件 |
:w |
存盘 |
:saveas [path to file] |
另存为 <path/to/file> |
:x、 ZZ 、:wq |
保存并退出 (:x表示仅在需要时保存;ZZ不需要输入冒号并回车) |
:X |
设置密码保存并退出,使用此命令后cat 该文件会显示乱码,再次打开需输入密码 |
:q! |
退出不保存 |
wq! |
强制保存退出 |
:qa! |
强行退出所有的正在编辑的文件,就算别的文件有更改。 |
:bn、 :bp |
你可以同时打开很多文件,使用这两个命令来切换下一个或上一个文件。 |
6.1 压缩打包介绍
6.2 gzip压缩工具
6.3 bzip2压缩工具
6.4 xz压缩工具
6.5 zip压缩工具
6.6 tar打包
6.7 打包并压缩
1 压缩打包介绍
1.1 压缩打包其实我们在windows中经常看到.rar格式的文件,而在linux中有自已的特有的压缩工具和格式。我们使用压缩文件,不仅能查看磁盘空间,而且在传输时还能节省网络带宽。在linux中有以下几种格式:.gz .bz2 .tar .tar.gz .tar.bz2 .tar.xz
2 gzip压缩工具
2.1gzip 的用法为:
gzip [-d#] filename #为1-9数字 -d在解压时使用
默认压缩级别为6
2.2 gzip压缩:现在我们实操一下这个命令,
2.2.1我们先在/home目录下创建一个1目录,
[root@localhost home]# mkdir 1
[root@localhost home]# ls
1 knightlai user1
2.2.2然后把/etc下面的配置文件拷贝一份到1目录下面。
[root@localhost home]# cp -r /etc/ /home/1
[root@localhost home]# ls
1 knightlai user1
2.2.3查看一下两个文件大小是否一致
[root@localhost home]# du -sh 1
30M 1
[root@localhost home]# du -sh /etc
30M /etc
2.2.4 现在压缩一下这个文件 ”-rw-r--r--. 1 root root 12288 Aug 2 09:01 aliases.db“
[root@localhost etc]# gzip aliases.db
[root@localhost etc]# ll
-rw-r--r--. 1 root root 0 Aug 2 09:01 AA.txt
-rw-r--r--. 1 root root 16 Aug 2 09:01 adjtime
-rw-r--r--. 1 root root 1518 Aug 2 09:01 aliases
-rw-r--r--. 1 root root 943 Aug 2 09:01 aliases.db.gz
2.2.5 现在可以查看这个解压文件内容”zcat aliases.db.gz“
[root@localhost etc]# zcat aliases.db.gz
rootwebmasternewsusenetroottoorrootsshdrootsquidrootsmmsprootshutdownrootsecuritypostmastersalesrootrpcuserrootradiusdrootquaggarootpostmasterrootpcaprootoperatorrootntprootnscdrootnocrootnfsnobodynewsnewsadminrootmysqlpostmastermarketingrootmailnullrootlprootingrespostmasterinforootidentroothostmasterrootgdmftpftpadmftpftp-adminrootftprootdovecotrootdesktoprootdecoderootdbusrootdaemonrootapacherootamandabackuprootadmrootabuselocalhost.localdomain
2.2.6 解压文件:gzip -d aliases.db.gz
[root@localhost etc]# gzip -d aliases.db.gz
[root@localhost etc]# ls
AA.txt csh.login grub2.cfg locale.conf passwd- resolv.conf.save sudo-ldap.conf
adjtime dbus-1 grub.d localtime pkcs11 rpc sysconfig
aliases default gshadow login.defs pki rpm sysctl.conf
aliases.db
注:gzip 不能压缩目录
3 bzip2压缩工具
3.1 bzip2压缩用法:
bzip2 [-dz]filename -d解压缩文件 -z压缩文件
压缩级别为1-9,默认级别是9 ,加或不加-z选项也可以压缩文件
3.2 压缩文件:bzip2 aliases.gzip2
[root@localhost etc]# bzip2 aliases.db
[root@localhost etc]# ls
AA.txt csh.login grub2.cfg locale.conf passwd- resolv.conf.save sudo-ldap.conf
adjtime dbus-1 grub.d localtime pkcs11 rpc sysconfig
aliases default gshadow login.defs pki rpm sysctl.conf
aliases.db.bz2
3.3也可以用bzcat查看压缩文件内容:bzcat aliases.db.bz2
[root@localhost etc]# bzcat aliases.db.bz2
rootwebmasternewsusenetroottoorrootsshdrootsquidrootsmmsprootshutdownrootsecuritypostmastersalesrootrpcuserrootradiusdrootquaggarootpostmasterrootpcaprootoperatorrootntprootnscdrootnocrootnfsnobodynewsnewsadminrootmysqlpostmastermarketingrootmailnullrootlprootingrespostmasterinforootidentroothostmasterrootgdmftpftpadmftpftp-adminrootftprootdovecotrootdesktoprootdecoderootdbusrootdaemonrootapacherootamandabackuprootadmrootabuselocalhost.l
3.4解压文件:bzip2 -d aliases.db.bz2
[root@localhost etc]# bzip2 -d aliases.db.bz2
[root@localhost etc]# ls
AA.txt csh.login grub2.cfg locale.conf passwd- resolv.conf.save sudo-ldap.conf
adjtime dbus-1 grub.d localtime pkcs11 rpc sysconfig
aliases default gshadow login.defs pki rpm sysctl.conf
aliases.db
4 xz压缩工具(这个命令用的不多)
4.1 xz命令的格式为:
xz[-dz]filename,-z 压缩 -d 解压
4.2 压缩文件:xz aliases.db
[root@localhost etc]# xz aliases.db
[root@localhost etc]# ls
AA.txt csh.login grub2.cfg locale.conf passwd- resolv.conf.save sudo-ldap.conf
adjtime dbus-1 grub.d localtime pkcs11 rpc sysconfig
aliases default gshadow login.defs pki rpm sysctl.conf
aliases.db.xz
4.3查看压缩文件内容:xzcat aliases.db.xz
[root@localhost etc]# xzcat aliases.db.xz
rootwebmasternewsusenetroottoorrootsshdrootsquidrootsmmsprootshutdownrootsecuritypostmastersalesrootrpcuserrootradiusdrootquaggarootpostmasterrootpcaprootoperatorrootntprootnscdrootnocrootnfsnobodynewsnewsadminrootmysqlpostmastermarketingrootmailnullrootlprootingrespostmasterinforootidentroothostmasterrootgdmftpftpadmftpftp-adminrootftprootdovecotrootdesktoprootdecoderootdbusrootdaemonrootapacherootamandabackuprootadmrootabuselocalhost.l
4.4 解压文件:unxz aliases.db.xz
[root@localhost etc]# unxz aliases.db.xz
[root@localhost etc]# ls
AA.txt csh.login grub2.cfg locale.conf passwd- resolv.conf.save sudo-ldap.conf
adjtime dbus-1 grub.d localtime pkcs11 rpc sysconfig
aliases default gshadow login.defs pki rpm sysctl.conf
aliases.db
注:不可以压缩目录
5zip压缩工具
5.1 zip压缩包在windows中我们经常可以看到,这个在linux中也比较常用。它不仅可以压缩目录和文件,压缩目录时,需要指定目录下的文件。
5.2 zip后面先跟目标文件名,然后跟要压缩的文件或目录。
[root@localhost etc]# zip 2.zip aliases.db
adding: aliases.db (deflated 93%)
[root@localhost etc]# ll
total 1048
-rw-r--r--. 1 root root 1084 Aug 2 10:46 2.zip
5.3解压文件:unzip 2.zip
[root@localhost etc]# unzip 2.zip
Archive: 2.zip
replace aliases.db? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
inflating: aliases.db
5.4 如果压缩整个目录的话,需要加上“-r”参数
[root@localhost /]# zip -r 1.zip /home/1
adding: home/1/etc/selinux/targeted/active/modules/100/logging/lang_ext (stored 0%)
adding: home/1/etc/selinux/targeted/active/modules/100/logrotate/ (stored 0%)
adding: home/1/etc/selinux/targeted/active/modules/100/logrotate/cil (stored 0%)
adding: home/1/etc/selinux/targeted/active/modules/100/logrotate/hll (deflated 9%)
adding: home/1/etc/selinux/targeted/active/modules/100/logrotate/lang_ext (stored 0%)
adding: home/1/etc/selinux/targeted/active/modules/100/logwatch/ (stored 0%)
adding: home/1/etc/selinux/targeted/active/modules/100/logwatch/cil (stored 0%)
[root@localhost /]# ls
1.zip
6 tar打包
6.1 tar本身是一个打包工具,可以把目录打包成一个文件,它把所有文件整合成一个大文件,方便复制和移动。
6.2 tar的用法,它有以下几个参数:
tar [zjxcvfpP]filename tar
-z 表示同时用gzip压缩
-j 表示同时用bzip2压缩
-J 表示同时用xz压缩
-x 表示解包或者解压缩
-t 表示查看tar包里的文件
-c 表示建立一个tar包或者压缩文件包
-v 表示可视化
-f 后面跟文件名,如果有多个参数,请把-f参数写在最后面
-p 表示使用原文件的属性(不常用)
-P 表示可以使用绝对路径(不常用)
--exclude filename:表示打包时不要将filename文件打包在内
6.3 打包文件:打包文件时原文件不会被删除
[root@localhost home]# tar -cvf 1.tar 1
1/etc/selinux/targeted/active/modules/100/logging/lang_ext
1/etc/selinux/targeted/active/modules/100/logrotate/
1/etc/selinux/targeted/active/modules/100/logrotate/cil
1/etc/selinux/targeted/active/modules/100/logrotate/hll
1/etc/selinux/targeted/active/modules/100/logrotate/lang_ext
1/etc/selinux/targeted/active/modules/100/logwatch/
1/etc/selinux/targeted/active/modules/100/logwatch/cil
1/etc/selinux/targeted/active/modules/100/logwatch/hll
看一下原文件还在
[root@localhost home]# ls
1 1.tar
6.4 查看打包文件内容列表: tar -tf 1.tar
[root@localhost home]# tar -tf 1.tar
1/etc/selinux/targeted/active/modules/100/logging/lang_ext
1/etc/selinux/targeted/active/modules/100/logrotate/
1/etc/selinux/targeted/active/modules/100/logrotate/cil
1/etc/selinux/targeted/active/modules/100/logrotate/hll
1/etc/selinux/targeted/active/modules/100/logrotate/lang_ext
6.5 解包:tar -xvf 1.tar
[root@localhost home]# tar -xvf 1.tar
1/etc/selinux/targeted/active/modules/100/logging/lang_ext
1/etc/selinux/targeted/active/modules/100/logrotate/
1/etc/selinux/targeted/active/modules/100/logrotate/cil
1/etc/selinux/targeted/active/modules/100/logrotate/hll
1/etc/selinux/targeted/active/modules/100/logrotate/lang_ext
1/etc/selinux/targeted/active/modules/100/logwatch/
7 打包并压缩
7.1 tar命令还有一个功能就是在打包时直接压缩,它支持gzip压缩,bzip压缩和xz压缩。
7.2 .1使用tar打包并用gzip压缩:
[root@localhost home]# tar -zcvf 1.tar.gz 1
1/etc/selinux/targeted/active/modules/100/logging/lang_ext
1/etc/selinux/targeted/active/modules/100/logrotate/
1/etc/selinux/targeted/active/modules/100/logrotate/cil
1/etc/selinux/targeted/active/modules/100/logrotate/hll
1/etc/selinux/targeted/active/modules/100/logrotate/lang_ext
1/etc/selinux/targeted/active/modules/100/logwatch/
查看一下打包出来的文件:
[root@localhost home]# ls
1 1.tar 1.tar.gz knightlai user1
7.2.2 这里也可以使用-tf选项查看打包中的内容列表:tar -tf 1.tar.gz
[root@localhost home]# tar -tf 1.tar.gz
1/etc/selinux/targeted/active/modules/100/logrotate/
1/etc/selinux/targeted/active/modules/100/logrotate/cil
1/etc/selinux/targeted/active/modules/100/logrotate/hll
1/etc/selinux/targeted/active/modules/100/logrotate/lang_ext
1/etc/selinux/targeted/active/modules/100/logwatch/
7..2.3解压.tar.gz里的内容:tar -zxvf 1.tar.gz
[root@localhost home]# tar -zxvf 1.tar.gz
1/etc/selinux/targeted/active/modules/100/logging/lang_ext
1/etc/selinux/targeted/active/modules/100/logrotate/
1/etc/selinux/targeted/active/modules/100/logrotate/cil
1/etc/selinux/targeted/active/modules/100/logrotate/hll
1/etc/selinux/targeted/active/modules/100/logrotate/lang_ext
1/etc/selinux/targeted/active/modules/100/logwatch/
[root@localhost home]# ls
1 1.tar 1.tar.gz knightlai user1
7.3我们再看一下用bzip2这种压缩方式
7.3.1 打包并用bzip2压缩是用这个命令:tar -jcvf 1.tar.bz2 1
[root@localhost home]# tar -jcvf 1.tar.bz2 1
1/etc/selinux/targeted/active/modules/100/logging/lang_ext
1/etc/selinux/targeted/active/modules/100/logrotate/
1/etc/selinux/targeted/active/modules/100/logrotate/cil
1/etc/selinux/targeted/active/modules/100/logrotate/hll
1/etc/selinux/targeted/active/modules/100/logrotate/lang_ext
1/etc/selinux/targeted/active/modules/100/logwatch/
[root@localhost home]# ls
1 1.tar 1.tar.bz2 1.tar.gz knightlai user1
7.3.2 这里也可以查看内容列表:tar -tf 1.tar.bz2
[root@localhost home]# tar -tf 1.tar.bz2
1/etc/selinux/targeted/active/modules/100/logging/lang_ext
1/etc/selinux/targeted/active/modules/100/logrotate/
1/etc/selinux/targeted/active/modules/100/logrotate/cil
1/etc/selinux/targeted/active/modules/100/logrotate/hll
1/etc/selinux/targeted/active/modules/100/logrotate/lang_ext
1/etc/selinux/targeted/active/modules/100/logwatch/
1/etc/selinux/targeted/active/modules/100/logwatch/cil
7.3.3 解压.tar.bz2里面的内容:tar -jxvf 1.tar.bz2
[root@localhost home]# tar -jxvf 1.tar.bz2
1/etc/selinux/targeted/active/modules/100/logging/lang_ext
1/etc/selinux/targeted/active/modules/100/logrotate/
1/etc/selinux/targeted/active/modules/100/logrotate/cil
1/etc/selinux/targeted/active/modules/100/logrotate/hll
1/etc/selinux/targeted/active/modules/100/logrotate/lang_ext
1/etc/selinux/targeted/active/modules/100/logwatch/
[root@localhost home]# ls
1 1.tar 1.tar.bz2 1.tar.gz knightlai user1