复习:
#
修改主机名为
haha
vim
/etc/passwd
haha
![](https://img-blog.csdnimg.cn/direct/bf4581f83e764009bdf99f9ca2f38fef.png)
#
上图的文件与目录创建
[root@localhost ~]
# mkdir -pv /yasuo/dir1
mkdir
:
已创建目录
'/yasuo'
mkdir
:
已创建目录
'/yasuo/dir1'
[root@localhost ~]
# cp /etc/passwd /yasuo/
[root@localhost ~]
# cp /etc/ssh/ssh_config /yasuo/
[root@localhost ~]
# cp /etc/ssh/ssh_config /yasuo/sshd
[root@localhost ~]
# cp /etc/hostname /yasuo/
[root@localhost ~]
# cp /etc/hostname /yasuo/hostname02
[root@localhost ~]
# cp /yasuo/hostname* /yasuo/passwd /yasuo/ssh* /yasuo/dir1
[root@localhost ~]
# tree /yasuo
#
将
/yasuo
目录拷贝
3
份
cp
-r
/yasuo /yasuo1
cp
-r
/yasuo /yasuo2
cp
-r
/yasuo /yasuo3
压缩和解压缩:
#
一、压缩的原理
文件的打包和压缩
为什么要学习? 文件过大,需要尽量的节省空间
为什么要压缩? 因为要网络传输,文件过大会导致网络的拥堵
压缩后,一定会比压缩之前小? 不一定
压缩之后比源文件大?为什么? 如 1 压缩后 1*1 显然文件变得更大
压缩的原理:
1
+
1
+
1
+
1
+
1
+
1
+
1
....
+
1 100
个
1
那个更省空间
磁盘存放
1010101010001
但是
1
一个
1
哪个更占空间? 文件本身很小,压缩之后所占空间反而会变大
所以小文件压缩的意义不大,一般只压缩大文件
自己的总结:压缩的原理,就是通过算法之类的东西,将现在已知的内容换成可以表达一样的意思,但是所占的空间一般会变得更小,但是对应一些简单的数据,通过压缩后占据的空间可能更大,所以压缩也是有选择性的
#
二、压缩的扩展名
常见的压缩的扩展名
,linux
里面是怎么判断文件类型的?
压缩文件,在
Linux
里不是一种文件类型,只是一种压缩文件格式
那压缩文件为什么要给他取个扩展名?
在
win
中取个扩展名,
win
就知道使用什么程序取打开它,那在
linux
里面取的扩展名,视为方便使用对应的解压程
序解压该文件,那解压是谁去解压的?是用户来操作的,看到
.zip
结尾的文件就知道要用
zip
程序解压
#
压缩、解压缩:只针对普通文件压缩,不支持对目录文件压缩
.zip
压缩指令:
zip, unzip
.gz
压缩指令:
gzip, gunzip
.bz2
压缩指令:
bzip2, bunzip2
.xz
压缩指令: xz, unxz 在linux中常见的三种
这三种指令 +要压缩的文件 默认压缩到当前目录
.lz
压缩指令:
xz
--format
=
lzma
--compress --format
=
lzma
--decompress0 (了解就行)
========================
gzip bzip2
xz
==========================
bzip2
对谁压缩
gzip
对谁压缩
xz
对谁压缩
[root@haha yasuo]
# ll
总用量
20
drwxr-xr-x.
2
root root
84 4
月
13 09
:16 dir1
-rw-r--r--
.
1
root root
1 4
月
13 09
:15 hostname
-rw-r--r--
.
1
root root
1 4
月
13 09
:15 hostname02
-rw-r--r--
.
1
root root
2106 4
月
13 09
:13 passwd
-rw-r--r--
.
1
root root
1921 4
月
13 09
:13 ssh_config
-rw-r--r--
.
1
root root
1921 4
月
13 09
:14 sshd
drwxr-xr-x.
2
root root
84 4
月
13 09
:20 yasuo
#
一、默认不保留源文件 压缩到当前工作目录
[root@haha yasuo]
# gzip hostname
[root@haha yasuo]
# bzip2 hostname02
[root@haha yasuo]
# xz passwd
[root@haha yasuo]
# ll
总用量
20
drwxr-xr-x.
2
root root
84 4
月
13 09
:16 dir1
-rw-r--r--
.
1
root root
37 4
月
13 09
:15 hostname02.bz2
-rw-r--r--
.
1
root root
30 4
月
13 09
:15 hostname.gz
-rw-r--r--
.
1
root root
928 4
月
13 09
:13 passwd.xz
-rw-r--r--
.
1
root root
1921 4
月
13 09
:13 ssh_config
-rw-r--r--
.
1
root root
1921 4
月
13 09
:14 sshd
drwxr-xr-x.
2
root root
84 4
月
13 09
:20 yasuo
[root@haha yasuo]
#
#
二、不保留源文件 解压缩到当前工作目录
[root@haha yasuo]
# cp ssh_config sd.txt
[root@haha yasuo]
# ll
总用量
24
drwxr-xr-x.
2
root root
84 4
月
13 09
:16 dir1
-rw-r--r--
.
1
root root
37 4
月
13 09
:15 hostname02.bz2
-rw-r--r--
.
1
root root
30 4
月
13 09
:15 hostname.gz
-rw-r--r--
.
1
root root
928 4
月
13 09
:13 passwd.xz
-rw-r--r--
.
1
root root
1921 4
月
13 09
:31 sd.txt
-rw-r--r--
.
1
root root
1921 4
月
13 09
:13 ssh_config
-rw-r--r--
.
1
root root
1921 4
月
13 09
:14 sshd
drwxr-xr-x.
2
root root
84 4
月
13 09
:20 yasuo
[root@haha yasuo]
# gunzip hostname.gz
[root@haha yasuo]
# bunzip2 hostname02.bz2
[root@haha yasuo]
# unxz passwd.xz
[root@haha yasuo]
# ll
总用量
24
drwxr-xr-x.
2
root root
84 4
月
13 09
:16 dir1
-rw-r--r--
.
1
root root
1 4
月
13 09
:15 hostname
-rw-r--r--
.
1
root root
1 4
月
13 09
:15 hostname02
-rw-r--r--
.
1
root root
2106 4
月
13 09
:13 passwd
-rw-r--r--
.
1
root root
1921 4
月
13 09
:31 sd.txt
-rw-r--r--
.
1
root root
1921 4
月
13 09
:13 ssh_config
-rw-r--r--
.
1
root root
1921 4
月
13 09
:14 sshd
drwxr-xr-x.
2
root root
84 4
月
13 09
:20 yasuo
#
三、保留源文件 压缩到指定工作目录
[root@haha yasuo]
# gzip sd.txt -c > /jieya/sd.txt.gz
[root@haha yasuo]
# bzip2 ssh_config -c > /jieya/ssh_config.bz2
[root@haha yasuo]
# xz ssh_config -c > /jieya/ssh_config1.xz
110
#
四、保留源文件 解压缩到指定工作目录
[root@haha yasuo]
# gunzip /jieya/sd.txt.gz -c > /jieya/sd
[root@haha yasuo]
# bunzip2 /jieya/ssh_config.bz2 -c > /yasuo/ssh_config
[root@haha yasuo]
# unxz /jieya/ssh_config1.xz -c > /yasuo/ssh_config1
压缩到其他路径:(好像默认了保存源文件)
三个 都是 要压缩的文件 -c > 要压缩到的路径
以上是哪种常见的压缩方式,都是在后面直接给 要压缩的文件 ,
如果要加压缩到的路径,在后面加就行了,这都是不保存源文件的。
如果要保存源文件 在压缩的文件和压缩到的位置中间加上一个 >
解压缩也是一样的
=============
特殊
的
zip
============
与前面三个不同 zip 可以压缩目录,并且压缩的格式和之前的不一样
zip
压缩后的文件名称
.zip
对谁压缩
如果是要明确压缩后文件的目录:
zip
/路径/
压缩后的文件名称
.zip
对谁压缩
#
一、
zip
压缩文件到当前工作目录
[root@haha yasuo]
# zip hostname.zip hostname hostname02
adding: hostname (stored
0
%)
adding: hostname02 (stored
0
%)
#
二、
zip
压缩文件到指定工作目录
[root@haha yasuo]
# zip /jieya/sshd.zip /yasuo/sshd
adding: yasuo/sshd (deflated
51
%)
#
三、解压缩文件到指定工作目录
#
如果
/jieya
目录不存在,那么解压后的文件的
jieya2
文件类型会自动变成目录
mkdir
/jieya2
[root@haha yasuo]
# unzip hostname.zip -d /jieya2
Archive: hostname.zip
extracting: /jieya2/hostname
extracting: /jieya2/hostname02
[root@haha yasuo]
# tree /jieya2
/jieya2
├── hostname
└── hostname02
#
四、
zip
解压文件到当前工作目录
unzip hostname.zip
#
拓展:
xz gzip bzip2
不支持对目录文件压缩
[root@haha yasuo]
# gunzip dir1/
gzip: dir1/ is a directory
--
ignored
[root@haha yasuo]
# gunzip dir1/ -r
[root@haha yasuo]
# ll dir1
总用量
20
-rw-r--r--
.
1
root root
1 4
月
13 09
:16 hostname
-rw-r--r--
.
1
root root
1 4
月
13 09
:16 hostname02
-rw-r--r--
.
1
root root
2106 4
月
13 09
:16 passwd
-rw-r--r--
.
1
root root
1921 4
月
13 09
:16 ssh_config
-rw-r--r--
.
1
root root
1921 4
月
13 09
:16 sshd
[root@haha yasuo]
# xz dir1/
xz
:
dir1/
:是目录,跳过
[root@haha yasuo]
# xz dir1/
xz
:请尝试执行
“xz
--help
”
来获取更多信息。
[root@haha yasuo]
# bzip2 dir1/
bzip2: Input file dir1/ is a directory.
#注意:xz bzip2 gzip 不支持对目录本身压缩
#注意:zip支持对目录本身压缩
#zip
压缩目录
dir1/
[root@haha yasuo]
# zip dir1.zip dir1/ -r -r 以递归的形式压缩
adding: dir1/ (stored
0
%)
adding: dir1/hostname (stored
0
%)
adding: dir1/hostname02 (stored
0
%)
adding: dir1/passwd (deflated
59
%)
adding: dir1/ssh_config (deflated
51
%)
adding: dir1/sshd (deflated
51
%)
[root@haha yasuo]
# ll dir1
总用量
20
-rw-r--r--
.
1
root root
1 4
月
13 09
:16 hostname
-rw-r--r--
.
1
root root
1 4
月
13 09
:16 hostname02
-rw-r--r--
.
1
root root
2106 4
月
13 09
:16 passwd
-rw-r--r--
.
1
root root
1921 4
月
13 09
:16 ssh_config
-rw-r--r--
.
1
root root
1921 4
月
13 09
:16 sshd
[root@haha yasuo]
# ll
总用量
44
drwxr-xr-x.
2
root root
84 4
月
13 10
:37 dir1
-rw-r--r--
.
1
root root
3653 4
月
13 10
:49 dir1.zip
一般在压缩文件时,在目标文件的后面都会给一个对应的后缀,这个后缀虽然不是一个强制性的,但是,一般都是以对应的压缩方式作为后缀,
一来呢,是可以在解压缩的时候明确知道要利用什么命令,
二来,方便压缩包的整理和识别,而且在上面前三种的压缩方式中,这个后缀是自带的,但是zip这里是自己设置的。
没有指定压缩文件的路径,一般默认压缩到当前目录
stored 0% 可能有两种原因 (压缩过程中会呈现出来的进度)
1.可能是文件中没有能够压缩的东西
2.也可能是这个文件并不能被压缩
tar 因为之前的都不能对目录进行压缩 所以通过这个可以对目录进行压缩
打包格式:
tar
打包过后的路径 对谁打包
解包格式:
tar
对谁解包
-C
解包后的路径(解包到哪)
常见:
-z
调用
gzip
程序,以
gzip
格式压缩或解压缩文件(
.tar.gz
)
-j
调用
bzip2
程序,以
bzip2
格式压缩或解压缩文件(
.tar.bz2
)
-J
使用
xz
压缩或解压缩文件(
.tar.xz
),
xz
的压缩率通常比
bzip2
更高
-C
(大写字母
C
) 和
-x选项一起使用,表示释放包时指定释放的目标路径
-c 打包
-v 是显示过程
-x 解包
-f 尽量写在最后,不然会发生报错
打包:
-czf 打包后的名字 被打包的文件 利用gzip 打包
-cvjf
打包后的名字 被打包的文件 利用bzip2 打包的时候显示过程
-xf 解开后面这个包 -C 到指定路径
在用tar打包时,默认源文件是保留的
资料:
-c
:创建归档(Create)。-x
:提取归档(Extract)。-z
:使用gzip进行压缩或解压缩。-j
:使用bzip2进行压缩或解压缩。-J
:使用xz进行压缩或解压缩。-v
:在处理文件时显示详细信息(Verbose)。-f
:指定归档文件的名称(File)。 一般写在选项的最后,防止报错-t
:列出归档内容(Table of contents)。-r
:向归档文件中追加文件(Append)。-u
:更新归档文件中的文件(Update)。
#
保留源文件打包
压缩后的路径 对谁压缩
[root@haha ~]
# tar -czvf /jieya/dir1.tar.gz /yasuo2/dir1
tar:
从成员名中删除开头的
“/”
/yasuo2/dir1/
/yasuo2/dir1/hostname
/yasuo2/dir1/hostname02
/yasuo2/dir1/passwd
/yasuo2/dir1/ssh_config
/yasuo2/dir1/sshd
[root@haha yasuo]
# tar -czf yasuo.tar.gz /yasuo/yasuo3
tar:
从成员名中删除开头的
“/”
[root@haha yasuo]
# tar -cjf yasuo.tar.bz2 /yasuo/yasuo3
tar:
从成员名中删除开头的
“/”
[root@haha yasuo]
# tar -cJf yasuo.tar.xz /yasuo/yasuo3
tar·
············································
上课敲的指令
··········································
tar:
从成员名中删除开头的
“/”
[root@haha yasuo]
# tar -xf yasuo.tar.gz
[root@haha yasuo]
# tar -xf yasuo.tar.bz2
[root@haha yasuo]
# tar -xf yasuo.tar.xz
#
指定路径解包解压缩
[root@haha yasuo]
# mkdir /jieya_tar/dir{1..5}
[root@haha yasuo]
# tar -xf yasuo.tar.gz -C /jieya_tar/dir1
[root@haha yasuo]
# tar -xf yasuo.tar.xz -C /jieya_tar/dir2
[root@haha yasuo]
# tar -xf yasuo.tar.bz2 -C /jieya_tar/dir3
#
注意
f
写最后,不然会报错
[root@localhost ~]
# cd /yasuo
[root@localhost yasuo]
# gzip hostname
[root@localhost yasuo]
# bzip2 hostname02
[root@localhost yasuo]
# xz passwd
[root@localhost yasuo]
# zip ssh.zip ssh
zip warning: name not matched:
ssh
zip error: Nothing to
do
! (ssh.zip)
[root@localhost yasuo]
# zip ssh.zip sshd
adding: sshd (deflated
51
%)
#
默认解压到当前工作目录
[root@localhost yasuo]
# gunzip hostname.gz
[root@localhost yasuo]
# bunzip2 hostname02.bz2
[root@localhost yasuo]
# unxz passwd.xz
[root@localhost yasuo]
# unzip ssh.zip
Archive:
ssh
.zip
replace sshd? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
inflating: sshd
#
压缩到指定目录
[root@localhost yasuo]
# gzip hostname -c > /yasuo/hostname.gz
[root@localhost yasuo]
# bzip2 hostname02 -c > /yasuo/hostname.bz2
[root@localhost yasuo]
# xz passwd -c > /yasuo/passwd.xz
#
解压到指定的目录
[root@localhost yasuo]
# gunzip hostname.gz -c > /jieya
-bash
: /jieya:
是一个目录
[root@localhost yasuo]
# gunzip hostname.gz -c > /jieya/hostname
[root@localhost yasuo]
# ll /jieya/hostname
-rw-r--r--
.
1
root root
5 4
月
13 18
:47 /jieya/hostname
[root@localhost yasuo]
# bunzip2 hostname.bz2 -c > /jieya/hostname02
[root@localhost yasuo]
# unxz passwd.xz -c >/jieya/passwd
[root@haha yasuo]
# unzip hostname.zip -d /jieya2
Archive: hostname.zip
extracting: /jieya2/hostname
extracting: /jieya2/hostname02
tar
-czvf
打包过后的路径 对谁打包
[root@haha ~]
# tar -czvf /jieya/dir1.tar.gz /yasuo2/dir1
tar:
从成员名中删除开头的
“/”
/yasuo2/dir1/
/yasuo2/dir1/hostname
/yasuo2/dir1/hostname02
/yasuo2/dir1/passwd
/yasuo2/dir1/ssh_config
/yasuo2/dir1/sshd
[root@haha ~]
# tar -cjf /jieya/dir2.tar.bz2 /yasuo2/dir2
tar:
从成员名中删除开头的
“/”
[root@haha ~]
# tar -cJf /jieya/dir3.tar.xz /yasuo2/dir3
tar:
从成员名中删除开头的
“/”
解包:
tar
对谁解包
-C
解包后的路径(解包到哪)
[root@haha ~]
# tar -xf /jieya/dir2.tar.bz2 -C /root/jietar
[root@haha ~]
# tar -xf /jieya/dir2.tar.bz2 -C /root/jietar2
[root@haha ~]
# tar -xf /jieya/dir3.tar.xz -C /root/jietar3
[root@haha ~]
# mkdir /root/root/jietar
[root@haha ~]
# mkdir /root/jietar2
[root@haha ~]
# mkdir /root/dirtar3
第七章,命令解释器-shell
shell: 操作系统的一个实用工具 (就是远程连接帮助对linux中的文件进行一些输入指令)
内置命令,在shell里自带的
外置命令,
在文件系统的某个目录下,有个与命令名称相同的文件
(就是在linux中的文件里有具体存放程序位置的命令)
查看内置和外置
type -a 只有一个路径出来就是外置命令,内置命令会直接出现一个 内建
环境变量: 怪异的东西
![](https://img-blog.csdnimg.cn/direct/fc1f2df80c8641b3b87ee01695e7bf2b.png)
; 用这个符号隔开的命令,两者独立执行,没有什么关联,就是可以帮助一次性输出多个命令
#
原样输出
[root@haha ~]
# echo 'shangke`date`'
shangke
`date`
“” 双引号是命令执行后输出
#
执行后,输出
[root@haha ~]
# echo "shangke`date`"
shangke2024
年
04
月
13
日 星期六
20
:14:10 CST
^ 开头
[^ ] 取反
[ ] 在过滤的时候是将含有中括号内的任意一种字符都过滤出来
grep ‘^ *#’ 以空格开头 *代表零个或多个空格,后接注释符#的行
* 作为空格后的 * 时, 前面也可以代表有零个空格
touch file{1.....100} 一次性创建多个文件
rm -rf file9* 以9开头的都被删了 包括9
rm -rf file8* 不包括8
\ 转义字符是将类似于 # 这类有特殊含义的字符,转换成字面上的意思
#`` 等价$() `date` =
$(date)
[root@haha ~]
# echo "shangke$(date)"
shangke2024
年
04
月
13
日 星期六
20
:15:01 CST
查别名 小名(别名):
alias 可以查全部的别名
type -a ll 查命令别名 ll 的原来的名字
是别名才能查,如果是本名则查出来的是内外置命令的判断
设置一个别名:
临时保存:alias 要设置的 = 设置成的别名
永久保存:要写配置文件
vim /root/.bashrc
进入到这个文件去添加别名
因为这个是添加在root下的,所以普通用户是用不了的,
想用的话要自己在普通用户设置
vim /home/redhat/.bashrc
要取消的话重新进入vim编译器里面删除就行了
source /home/redhat/.bashc 是一个立即应用更改后的程序,避免等待
这个好像是在vim里面,添加或者是删除后,进行一个更新的指令
root里面要退出账号才能生效
对于在root 内保存一个别名和使用都理解了,但是在普通用户中,在对应的vim 文件能写,但是不管是普通用户还是root用户都无法使用设置的别名,还待研究
#
局部生效的配置文件: 只对
root
用户生效
/root/.bashrc
vim
/root/.bashrc
alias
mulu
=
'ls -dl'
source
/root/.bashrc
logout
生效
#
只对
root
用户生效
/home/redhat/.bashrc
vim
/home/redhat/.bashrc
source
/home/redhat/.bashrc
#
全局生效的配置文件:
/etc/bashrc
vim
/etc/bashrc
source
/etc/bashrc