Linux || 压缩类命令

问题引入

为什么官方网站提供压缩文件下载?

考虑到用户的网络,下载的时间。

为什么需要文件(文件夹)进行压缩?

纯文本的文件压缩可以节约非常多的空间。
图片和视频,音频,压缩不能节约非常多的空间。

你知道那些压缩软件?

压缩文件的格式:

Windows里:

    zip
    rar
    iso
    7zip

常见压缩软件:

    winrar --》.rar --》推荐
    好压
    7z

.zip格式 和.tar.gz格式的压缩文件可以在Linux和Windows里解压。


zip与unzip

[root@jack lianxi2]# yum install zip -y

[root@jack lianxi2]# zip chen.zip chen.txt
adding: chen.txt (stored 0%)

[root@jack lianxi2]# zip passwd.zip passwd
保留源文件进行压缩
adding: passwd (deflated 63%)

[root@jack lianxi2]# ls
backup_log.sh  chen.txt  guangxi   hunan        passwd
bigfile.sh     chen.zip  hejin.sh  li.sh  passwd.zip

[root@jack lianxi2]# ll -h
总用量 28K
-rw-r--r--. 1 root root  246 3月  18 20:42 backup_log.sh
-rw-r--r--  1 root root  141 8月  18 21:35 bigfile.sh
-rw-r--r--  1 root root    0 8月  21 00:14 chen.txt
-rw-r--r--  1 root root  176 8月  21 00:14 chen.zip
drwxr-xr-x  3 root root   17 8月  18 20:32 guangxi
-rw-r--r--  1 root root  303 8月  18 21:10 hejin.sh
drwxr-xr-x  3 root root   23 8月  20 23:26 hunan
-rw-r--r--  1 root root  133 8月  20 16:54 li.sh
-rw-r--r--  1 root root 2.5K 8月  21 00:13 passwd
-rw-r--r--  1 root root 1.1K 8月  21 00:14 passwd.zip

[root@jack backup]# unzip chen
Archive:  chen.zip
extracting: chen.txt      
     
[root@jack backup]# ls
chen.txt  chen.zip

[root@jack lianxi2]# zip -r backup.zip backup
# -r 是对文件和文件夹进行压缩
  adding: backup/ (stored 0%)
  adding: backup/chen.zip (stored 0%)
  adding: backup/chen.txt (stored 0%)

[root@jack lianxi2]# ls
backup        chen.txt  hejin.sh  passwd
backup.zip     chen.zip  hunan     passwd.zip

gzip与gunzip

[root@jack backup]# gzip passwd
# gzip 是直接在源文件上打压缩包的

[root@jack backup]# ls
chen.txt  chen.zip  passwd.gz  passwd.zip

[root@jack backup]# gunzip passwd.gz

[root@jack backup]# ls
chen.txt  chen.zip  passwd  passwd.zip

[root@jack backup]# ls
chen.txt  chen.zip  passwd  passwd.zip

[root@jack backup]# zcat passwd.zip # --》查看压缩文件里的内容
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

xz与unxz

[root@jack backup]# xz passwd  # 打压缩

[root@jack backup]# ls
chen.txt  chen.zip  passwd.xz  passwd.zip

[root@jack backup]# unxz passwd.xz  #  解压缩

[root@jack backup]# ls
chen.txt  chen.zip  passwd  passwd.zip
# 100M --》xz --》30M

bzip2与bunzip2

[root@jack backup]# bzip2 passwd

[root@jack backup]# ls
chen.txt  chen.zip  passwd.bz2  passwd.zip

[root@jack backup]# unbzip2 passwd.bz2
-bash: unbzip2: 未找到命令

[root@jack backup]# bunzip2 passwd.bz2

[root@jack backup]# ls
chen.txt  chen.zip  passwd  passwd.zip

总结

    zip --》.zip --》unzip
    gzip --》.gz --》gunzip ---》推荐
    xz --》.xz --》unxz --》推荐
    bzip2 --》.bz2 --》bunzip2


tar

tar 是制作归档文件,解压归档文件。

格式:tar [选项]... 归档文件 源文件或目录。

常用命令选项

-c:创建.tar格式的包文件 create 
-x:解开.tar格式的包文件 extract
-v:输出详细信息
-f:表示使用归档文件 file
-t:列表查看包内的文件 list

归档文件:理解为一个打包的文件,可以包含很多个文件--》没压缩。

压缩文件:可以是在归档文件的基础上,进行压缩。

[root@jack chen]# tar cf sc.tar he liu song.txt zhang.txt

[root@jack chen]# ls
he  liu  sc.tar  song.txt  zhang.txt

[root@jack chen]# tar tf sc.tar
he/
liu/
song.txt
zhang.txt

[root@jack chen]# gzip sc.tar

[root@jack chen]# ls
he  liu  sc.tar.gz  song.txt  zhang.txt

tar 
 -z --》调用gzip对归档文件进行压缩
 -j --》bzip2
 -J --》xz

[root@jack chen]# tar czf sc2.tar.gz he liu zhang.txt song.txt

[root@jack chen]# ls
he  liu  sc2.tar.gz  sc.tar.gz  song.txt  zhang.txt

[root@jack chen]# tar cjf sc3.tar.bz2 song.txt zhang.txt

[root@jack chen]# ls
he  liu  sc2.tar.gz  sc3.tar.bz2  sc.tar.gz  song.txt  zhang.txt

[root@jack chen]# tar cJf sc4.tar.xz liu he song.txt

[root@jack chen]# ls
he   sc2.tar.gz   sc4.tar.xz  song.txt
liu  sc3.tar.bz2  sc.tar.gz   zhang.txt

# file
[root@jack chen]# file sc2.tar.gz
sc2.tar.gz: gzip compressed data, last modified: Sat Aug 20 17:13:44 2022, from Unix, original size 10240

[root@jack chen]# file sc3.tar.bz2
sc3.tar.bz2: bzip2 compressed data, block size = 900k

[root@jack chen]# file sc4.tar.xz
sc4.tar.xz: XZ compressed data

创建压缩文件的例子

[root@jack chen]# touch huangjuan.txt

[root@jack chen]# ls
huangjuan.txt

[root@jack chen]# tar -czf huangjuan.tar.gz huangjuan.txt

[root@jack chen]# ls
huangjuan.tar.gz  huangjuan.txt

[root@jack chen]# file huangjuan.tar.gz
huangjuan.tar.gz: gzip compressed data, last modified: Sat Aug 20 17:22:01 2022, from Unix, original size 10240

[root@jack chen]# tar -cjf huangjuan.tar.bz2 huangjuan.txt

[root@jack chen]# ls
huangjuan.tar.bz2  huangjuan.tar.gz  huangjuan.txt

[root@jack chen]# tar -cJf huangjuan.tar.xz huangjuan.txt

[root@jack chen]# ls
huangjuan.tar.bz2  huangjuan.tar.gz  huangjuan.tar.xz  huangjuan.txt

[root@jack chen]# file huangjuan.tar.bz2
huangjuan.tar.bz2: bzip2 compressed data, block size = 900k

[root@jack chen]# file huangjuan.tar.xz
huangjuan.tar.xz: XZ compressed data

# 查看压缩文件里的内容
[root@jack chen]# tar -tf huangjuan.tar.gz
huangjuan.txt

[root@jack chen]# tar -tf huangjuan.tar.xz
huangjuan.txt

[root@jack chen]# tar -tf huangjuan.tar.bz2
huangjuan.txt

[root@jack chen]# tar czfv huangjuan.tar.gz huangjuan.txt boot
# 后面打包的压缩文件会替换原来的压缩包里的内容

# 解压文件
[root@jack chen]# tar xf huangjuan.tar.gz

[root@jack chen]# ls
boot               huangjuan.tar.gz  huangjuan.txt
huangjuan.tar.bz2  huangjuan.tar.xz

绝对路径:打压缩 --》指定路径

新建/backup 文件夹

mkdir -p /backup

将/boot目录和/etc/passwd文件,打包压缩存放到/backup目录下,叫boot_passwd.tar.gz。

tar czf /backup/boot_passwd.tar.gz /etc/passwd /boot

[root@jack backup]# tar czf /backup/boot_passwd.tar.gz /etc/passwd /boottar: 从成员名中删除开头的“/”

tar: 从硬连接目标中删除开头的“/” -->为什么要在打压缩包的时候,将绝对路径的/删除
这样解压的时候就会把压缩包里的文件放在当前目录下。

mkdidr -p /lianxi/tar

cd /lianxi/tar

cp /etc/hosts /etc/passwd .

cp -r /boot .

[root@jack tar]# tar czf boot.tar.gz hosts passwd boot

[root@jack tar]# tar tf boot.tar.gz

[root@jack tar]# tar cjf host_passwd.tar.bz2 hosts passwd

[root@jack tar]# ls
boot  boot.tar.gz  host_passwd.tar.bz2  hosts  passwd

[root@jack tar]# tar tf host_passwd.tar.bz2 
tar cJf boot_pw_log.tar.xz /boot /etc/passwd /var/log

mkdir /bak -p

cp /lianxi/boot_pw_log.tar.xz /bak

tar xf  boot_pw_log.tar.xz

--exclude 排除

     将/boot目录下的除grub2目录以外的所有文件都备份到/bak目录下叫no-grub2.tar.gz。

[root@jack zhang]# tar -zcf /zhang/boot.tar.gz /boot

[root@jack zhang]# tar --exclude=/boot/grub2 -zcf  /zhang/no-grub2-boot.tar.gz   /boot

[root@jack zhang]# tar --exclude=/boot/{grub2,efi} -zcf /zhang/no-efi-boot.tar.gz  /boot

[root@jack zhang]# ls
boot.tar.gz  no-efi-boot.tar.gz  no-grub2-boot.tar.gz
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韩未零

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值