文件的归档和压缩命令有很多,我们今天着重来介绍以下内容:
- tar命令进行文件的归档和压缩
- zip管理压缩文件
归档和压缩文件的好处:节约硬盘的资源 ,加快文件传输速率
我们先来看tar命令
1 . tar: 作用打包压缩文件
用法: tar 【参数】 file
参数:
参数 | 作用 |
---|---|
-c | create创建文件 |
-x | -extract 提取 解压还原文件 |
-v | –verbose显示执行详细过程 |
-f | –file指定备份文件 |
-t | –list 列出压缩包中包括哪些文件,不解包,查看包中的内容 |
-C | (大写)–directory 指定解压位置 |
单词:
extract 美 /ɪkˈstrækt/ 摘录,提取,取出
举例:
1) 把当前的路径下的文件打包,命名为loacl.tar
[root@itlaoxin ~]# mkdir test
[root@itlaoxin ~]# cd test/
[root@itlaoxin test]# touch a b c
[root@itlaoxin test]# ls
a b c
[root@itlaoxin test]# tar -cvf local.tar ./*
./a
./b
./c
[root@itlaoxin test]# ls
a b c local.tar
[root@itlaoxin test]#
解压缩:
[root@itlaoxin test]# ls
a b c local.tar
[root@itlaoxin test]# mkdir /root/test2
[root@itlaoxin test]# cp local.tar !$
cp local.tar /root/test2
[root@itlaoxin test]# cd !$
cd /root/test2
[root@itlaoxin test2]# ls
local.tar
[root@itlaoxin test2]# tar xvf local.tar
./a
./b
./c
[root@itlaoxin test2]# ls
a b c local.tar
[root@itlaoxin test2]#
2) 指定解压位置 -C
[root@itlaoxin test2]# ls
a b c local.tar
[root@itlaoxin test2]# tar xvf local.tar -C /opt/
./a
./b
./c
[root@itlaoxin test2]# cd !$
cd /opt/
[root@itlaoxin opt]# ls
a b c rh
[root@itlaoxin opt]#
3) 不解包查看tar包中的内容
[root@itlaoxin opt]# cd /root/test2
[root@itlaoxin test2]# ls
a b c local.tar
[root@itlaoxin test2]# tar tvf local.tar
-rw-r--r-- root/root 0 2022-06-08 16:37 ./a
-rw-r--r-- root/root 0 2022-06-08 16:37 ./b
-rw-r--r-- root/root 0 2022-06-08 16:37 ./c
[root@itlaoxin test2]#
2. tar 归档+压缩
语法: tar czvf newfile.tar.gz
常用参数:
-z, –gzip 以gzip方式压缩 扩展名: tar.gz
-j : 以bz2方式压缩的 扩展名:tar.bz2
-J : 以xz 方式压缩 扩展名:tar.xz
举例:
1.创建tar.gz的包
[root@zmedu16 ~]# tar cvf etc.tar /etc
[root@localhost test]# tar zcvf etc.tar.gz /etc #归档,注意备份的名字后缀
[root@localhost test]# tar zxvf etc.tar.gz #解压缩
2. 创建tar.bz2包
语法: #tar jcvf newfile.tar.bz2 SOURCE
[root@zmedu16 ~]# tar -jcvf etc.tar.bz2 /etc
[root@zmedu16 ~]# tar -jxvf etc.tar.bz2 /etc #解压缩
[root@zmedu16 ~]# tar jxvf etc.tar.bz2 -C /opt #解压到opt目录下
3. 创建.tar.xz的包
[root@zmedu16 ~]# tar -Jcvf etc.tar.xz /etc
[root@zmedu16 ~]# tar -xvf etc.tar.xz #tar.xz 这类包,解压缩
或:
[root@zmedu16 ~]# tar -Jxvf etc.tar.xz
三种压缩方式中, tar.gz tar.bz2是比较常用的
tar.xz 压缩比例最高,压缩时间最长,压缩完文件最小
3. zip 管理压缩文件
zip是压缩程序,unzip是解压程序
安装zip
[root@zmgaosh zip]# yum install zip
压缩:
[root@zmgaosh zip]# zip passwd.zip /etc/passwd
adding: etc/passwd (deflated 61%)
[root@zmgaosh zip]# ls
passwd.zip
解压缩:
[root@zmgaosh zip]# unzip passwd.zip
Archive: passwd.zip
inflating: etc/passwd
[root@zmgaosh zip]# ls
etc passwd.zip
如果要指定目标解压目录可以加参数-d
[root@zmgaosh zip]# unzip passwd.zip -d /opt/
小技巧
如何查看看压缩文件的内容
1. 使用vim直接查看压缩包的内容
[root@itlaoxin test2]# vim local.tar
" tar.vim version v29
" Browsing tarfile /root/test2/local.tar
" Select a file with cursor and press ENTER
./a
./b
./c
单词: browsing 美 /'brauziŋ/ 浏览
词组: browse through 浏览,翻阅
cursor 美 /'kɝsɚ/ 光标
you can move the cursor by using the mouse
你可以使用鼠标来移动光标
2. 使用file文件查看
[root@zmgaosh zip]# file passwd.zip
passwd.zip: Zip archive data, at least v2.0 to extract
总结
Linux下压缩解压命令还有很多,但最常见的还是tar命令和zip命令
比如:
1) ,gz的包:
解压:gunzip filename.gz
2).bz2 的包
bzip2 -d filename.bz2
3).tar.bz2的包
tar jxvf filename.tar.bz2
4).bz的包
bzip2 filename.bz
5).Z的包
uncompress filename.Z
6).rar的包
rar x filename.rar