如果你想拥有你从未拥有过的东西,那么你必须去做你从未做过的事情
一、打包压缩和解压缩
麒麟操作系统常用的打包和压缩的命令有两种:tar和zip
1、tar打包压缩
打包:tar 最初主要用于将多个文件和目录打包成一个文件,方便文件的存储和传输。它不会对文件内容进行压缩,只是简单地将它们整合在一起。
压缩:tar 可以与其他压缩工具(如 gzip、bzip2 等)结合使用,实现打包和压缩的功能。这样可以有效地减小文件占用的磁盘空间,便于文件的备份和共享。
tar命令:
语法: tar zcvf 名称.tar.gz file file1 file2
tar zcvf (框) 西瓜 菠萝 甘蔗
z :使用zip进行压缩
c :create 创建
v :verbose 显示压缩过程 可以省略此参数
f :file 文件
tf :查看压缩包里的文件
xf :解压
-c :指定解压的目录
扩展:
--exclude=file 排除文件
--exclude-from=file #排除文件中断文件
压缩的原因:
1)节省磁盘空间
2)打包-->传送到备份服务器
3)节省网络流程
4)减少磁盘IO
什么时候压缩?(压缩占用cpu非常高)
在业务的低谷期:流量监控
案例:将1.txt 压缩为1.tar.gz
[root@yunzhongziedu ~]# mkdir yunzhongzi
[root@yunzhongziedu ~]# cd yunzhongzi
[root@yunzhongziedu yunzhongzi]# touch {1..5}.txt
[root@yunzhongziedu yunzhongzi]# tar zcvf 1.tar.gz ./1.txt #压到默认地址下,现在默认地址在/root/yunzhongzi
./1.txt
[root@yunzhongziedu yunzhongzi]# ll
total 4
-rw-r--r-- 1 root root 111 Mar 14 16:29 1.tar.gz
案例2:打包多个文件
[root@yunzhongziedu yunzhongzi]# tar zcvf all.tar.gz ./{1..5}.txt
./1.txt
./2.txt
./3.txt
./4.txt
./5.txt
[root@yunzhongziedu yunzhongzi]# ll
total 8
-rw-r--r-- 1 root root 143 Mar 14 16:34 all.tar.gz
案例3:打包目录
[root@yunzhongziedu ~]# tar zcvf dir.tar.gz yunzhongzi
[root@yunzhongziedu ~]# ls
dir.tar.gz yunzhongzi
案例4:查看压缩包里的文件名称
[root@yunzhongziedu ~]# tar tf yunzhongzi/1.tar.gz
./1.txt
案例5:指定压缩包的位置
[root@yunzhongziedu yunzhongzi]# tar zcvf /opt/test.tar.gz 1.txt 2.txt
1.txt
2.txt
[root@yunzhongziedu yunzhongzi]# ll /opt
total 23992
-rw-r--r-- 1 root root 116 Mar 14 16:39 test.tar.gz
压缩带路径的文件 /etc/passwd /etc/hosts
注意: 系统为了保护系统文件,默认会将/移除
建议在打包压缩时候,尽量到相对路径进行打包
[root@db01 ~]# tar zcvf test.tar.gz /etc/hosts /etc/passwd
tar: Removing leading '/' from member names
/etc/hosts
/etc/passwd
进入相对路径打包:
[root@db01 ~]# cd /etc/
[root@db01 etc]# ll
hosts passwd-rw-r--r--. 1 root root 158 Mar 6 15:18 hosts-rw-r--r--. 1 root root 1013 Mar 8 10:50 passwd
[root@db01 etc]# tar zcvf /root/hosts.tar.gz hosts passwd
hosts
passwd
案例7.解压缩xf #先删除再看(未删除直接覆盖)解压之后会覆盖
[root@db01 ~]# tar xf a.tar.gz
[root@db01 ~]# ll /opt/
total 4-rw-r--r-- 1 root root 136 Mar 14 10:12 test.tar.gz
[root@db01 ~]# tar xf /opt/test.tar.gz
[root@db01 ~]# ll
total 4-rw-r--r-- 1 root root 11 Mar 14 09:52 1.txt-rw-r--r-- 1 root root 0 Mar 14 09:45 2.txt
案例8.指定解压的目录
[root@db01 ~]# ll /opt/
total 4-rw-r--r-- 1 root root 136 Mar 14 10:12 test.tar.gz
[root@db01 ~]# rm -rf /tmp/*
[root@db01 ~]# ll /tmp/
total 0
[root@db01 ~]# tar xf /opt/test.tar.gz -C /tmp/
[root@db01 ~]# ll /tmp/
total 4-rw-r--r-- 1 root root 11 Mar 14 09:52 1.txt-rw-r--r-- 1 root root 0 Mar 14 09:45 2.txt
[root@db01 ~]# ll
total 0
[root@db01 ~]# ll /opt/
total 4-rw-r--r-- 1 root root 136 Mar 14 10:12 test.tar.gz
[root@db01 ~]# touch {1..5}.txt
[root@db01 ~]# ll
total 0-rw-r--r-- 1 root root 0 Mar 14 10:30 1.txt-rw-r--r-- 1 root root 0 Mar 14 10:30 2.txt-rw-r--r-- 1 root root 0 Mar 14 10:30 3.txt-rw-r--r-- 1 root root 0 Mar 14 10:30 4.txt-rw-r--r-- 1 root root 0 Mar 14 10:30 5.txt
案例9.排除1.txt 其他文件全部打包
[root@db01 ~]# tar zcvf all.tar.gz *.txt --exclude=1.txt
2.txt
3.txt
4.txt
5.txt
排除多个
[root@db01 ~]# tar zcvf all.tar.gz *.txt --exclude={1..2}.txt
3.txt
4.txt
5.txt
创建实验环境:
[root@db01 ~]# ll
-rw-r--r-- 1 root root 0 Mar 14 10:33 yunzhongzi.txt
[root@db01 ~]# #排除1.txt lidao.txt a.log
[root@db01 ~]# ls 1.txt lidao.txt a.log
1.txt a.log lidao.txt
[root@db01 ~]# ls 1.txt lidao.txt a.log > ex.txt
[root@db01 ~]# cat ex.txt
1.txt
a.log
lidao.txt
[root@db01 ~]# tar zcvf all.tar.gz *.txt --exclude-from=ex.txt
2.txt
3.txt
4.txt
5.txt
code.txt
ex.txt
yunzhongzi.txt
2、zip压缩
zip压缩有以下两个特点
1、压缩:zip 是一个广泛使用的文件压缩工具,它可以将多个文件和目录压缩成一个.zip文件,并且在压缩过程中可以设置压缩级别等参数,以平衡压缩比和压缩速度。与 tar 不同的是,zip 本身就具备压缩功能,不需要与其他压缩工具配合。
2、加密:zip 还支持对压缩文件进行加密,保护文件内容的隐私。这在需要保护敏感信息时非常有用
语法格式:
1.打包
[root@yunzhongziedu yunzhongzi]# zip a.zip 1.txt
2.解压
[root@yunzhongziedu ~]# unzip a.zip
Archive: a.zip
replace 1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
extracting: 1.txt
3.指定解压的位置
[root@yunzhongziedu ~]# unzip a.zip -d /tmp/
Archive: a.zip
extracting: /tmp/1.txt
#注zip命令需安装
二、文件权限
权限用来赋予给用户的
[root@yunzhongziedu ~]# ll -i
total 16
33577780 -rw-r--r-- 1 root root 0 Mar 14 17:14 1.txt
第三列:rw-r--r--9位文件权限
作用:决定了用户对于文件的极限
三位为一组 完整权限rwx
前三位:属主 #哪个用户创建的文件,文件的主人 权限 rw- 可读写
中三位:属组 #哪个小组内成员对于文件的权限 r-- 只读
后三位:其他位置 #陌生人 既不是主人也不在小组内 r-- 只读
rwx含义:
r read 读 # 查看 cat less vim
w write 写 # vim sed echo > >>
x excute 可执行 #普通文件没有x权限,脚本才给x执行权限
- 空的权限
注意:只有命令才可以执行,如果文件中存放着命令称为shell脚本普通文件中是字符串,不可以执行
系统默认在创建用户的时候,会自动创建出和用户名一样的组
比如:创建一个yunzhongzi的用户,则系统会自动创建一个yunzhongzi组,默认用户属于yunzhongzi组
文件的默认权限:rw-r--r--
目录的默认权限:rwxr-xr-x
rwx对于文件和目录的权限
三、软硬链接
硬链接
硬链接特点:使用相对较少
1.具有相同Inode号码的文件互为硬连接
2.删除一个入口,另外一个入口还可以进入
3.删除一个硬链接不影响其他的文件
4.所有的inode号码都没了,文件被删除
5.创建硬链接直接用ln
6.目录不能创建硬链接,只有文件才可以建立硬链接
7.硬链接不能跨越文件系统,软连接可以跨文件系统(跨磁盘)
8.目录默认2个硬链接,文件1个硬链接
创建一个硬链接
[root@yunzhongziedu ~]# ln 1.txt a.txt
[root@yunzhongziedu ~]# ll -i
total 0
33577780 -rw-r--r-- 2 root root 0 Mar 14 18:58 1.txt
33577780 -rw-r--r-- 2 root root 0 Mar 14 18:58 a.txt
会发现它们两个文件的Inode号是一样的
软链接
软链接特点:
1.软链接的inode号码不同
2.类似window快捷方式
3.软链接可以跨文件系统
4.创建软链接 ln-s
5.删除源文件,链接文件失效,变红底白字
6.删除链接文件不影响源文件
7.目录可以作软链接
注意:创建软硬链接的时候都要用绝对路径
[root@yunzhongziedu ~]# ln -s 1.txt a.txt
[root@yunzhongziedu ~]# ll -i
total 0
33577780 -rw-r--r-- 1 root root 0 Mar 14 18:58 1.txt
33575330 lrwxrwxrwx 1 root root 5 Mar 14 19:08 a.txt -> 1.txt
软链接的作用:
1.保持文件路径不变,解决磁盘不够用的问题
2.代码上线
3.去软件版本号
软链接和硬链接有什么区别呢?
软硬链接的区别?
1.硬链接的inode相同,软连接的inode不同
2.目录不支持硬链接,只能作软连接,文件软硬连接都可以
3.硬链接不可以跨文件系统,软链接可以
4.目录硬链接默认2个,文件默认1个
5.删除硬链接不影响源文件,硬链接个数为0,则文件被删除
6.删除软连接不影响源文件,删除源文件,则文件被删除
7.软硬链接是普通文件,可以直接使用rm删除
8.inode号都为0,并且文件没有被进程调用,文件才真正被删除
9.创建方式:硬链接:ln 软连接:ln -s
小结
今天重点:
1.tar命令
1)tar zcvf all.tar.gz file1 file2 /etc/hosts # 打包
2)tar zcvf /opt/all.tar.gz file # 压缩包放到指定的路径
3)tar tf all.tar.gz # 查看压缩包
4)tar xf all.tar.gz # 默认解压到当前
5)tar xf all.tar.gz -C /tmp/ # 指定解压到/tmp目录下
扩展: --exclude=file
--exclude-from=包文件名的文件
2.zip命令
1)zip all.zip file
zip /etc/all.zip file1 file2
2)unzip all.zip # 解压
3)unzip all.zip -d /opt/ # 指定解压的位置
3.文件权限
rw-r--r-- 1 root root xx xx file
9位权限位
3位一组
前三位 # 主人 rw- 可读写 最高权限
中三位 # 属组 r-- 只读 只能看
后三位 # 其他 r-- 只读 只能看
4.软硬链接 使用绝对路径
1)创建硬链接 相当于多个藏宝图 超市多个门
ln 源文件 目标文件
ln a.txt b.txt
2)创建软链接 类似windows快捷方式
ln -s a.txt b.txt
面试题: 硬链接和软链接的区别
1.inode不同
2.创建方式不同
3.目录不能硬 文件可以软硬
4.硬链接不能跨 软的可以跨
5.删除硬不影响,删除软源有影响,链接没影响
6.目录默认2硬链接 文件默认1个
7.inode号为0,并且没有被进程调用,文件真正被删除
5.stat 查看文件的详细信息
6.查找30天前的文件,并删除
find /data -type f -mtime +30|xargs rm
find /data -type f -mtime +30 -exec rm {} \;
rm -f `find /data/ -type f -mtime +30`
明日预告:打包压缩,解压,文件权限,满满的干货,敬请期待!!!
想成为大佬,就要从小白开始,从0开始,一点一点的积累,慢慢成长,明天你就是大佬!!