文件的归档和压缩

1、语法格式

tar 参数 文件或目录

2、常用 参数

-x 从压缩的文件中提取文件
-z 支持gzip解压文件
-v 显示操作过程
-t 显示压缩文件的内容
-c 建立新的归档
-r 追加文件至归档结尾
-j 支持bzip2解压文件
-C 切换到指定目录
-f 指定压缩文件

3、命令使用

3.1、对某个目录进行打包操作,显示打包过程

[root@fishman-160 ~]# tar -cvf nginx.tar.gz nginx-1.25.1
nginx-1.25.1/
nginx-1.25.1/auto/
nginx-1.25.1/auto/cc/
nginx-1.25.1/auto/cc/acc
...
root@fishman-160 ~]# ls
公共  视频  文档  音乐  a是windows打开正常linux打开乱码.txt  anaconda-ks.cfg                           initial-setup-ks.cfg                            nework        nginx-1.25.1.tar.gz  nohup.out  password
模板  图片  下载  桌面  aa.txt                               b在linux编辑的文档熬windows下没有换行.sh  libncurses5-32bit-6.4.20230701-16.2.x86_64.rpm  nginx-1.25.1  nginx.tar.gz         passwd     test

查看tar包里面的内容(不解压)

[root@fishman-160 ~]# tar -tf nginx.tar
./nginx-1.25.1/
./nginx-1.25.1/auto/
./nginx-1.25.1/auto/cc/
./nginx-1.25.1/auto/cc/acc
./nginx-1.25.1/auto/cc/bcc

注意:

1、在使用绝对路径名归档文件时,将默认从文件名中删除该路径中前面的 / 符号。这将有助于避免可能造成重要文件被覆盖的错误。

2、在对文件进行归档时,必须保证TAR命令的用户有能够读取这些文件的权限;对无相应权限用户,归档时将忽略用户没有读权限的文件,并且将忽略用户没有读和执行权限的目录

[root@fishman-160 ~]# tar cvf nginx.tar /root/nginx-1.25.1 #压缩时候绝对路径,/符号
tar: 从成员名中删除开头的“/”
/root/nginx-1.25.1/ 
/root/nginx-1.25.1/auto/
/root/nginx-1.25.1/auto/cc/
/root/nginx-1.25.1/auto/cc/acc
/root/nginx-1.25.1/auto/cc/bcc
/root/nginx-1.25.1/auto/cc/ccc
/root/nginx-1.25.1/auto/cc/clang

[root@fishman-160 ~]# tar -tf nginx.tar #查看tar包中的文件路径,/已经被剔除
root/nginx-1.25.1/
root/nginx-1.25.1/auto/
root/nginx-1.25.1/auto/cc/
root/nginx-1.25.1/auto/cc/acc
root/nginx-1.25.1/auto/cc/bcc
root/nginx-1.25.1/auto/cc/ccc
root/nginx-1.25.1/auto/cc/clang
root/nginx-1.25.1/auto/cc/conf
root/nginx-1.25.1/auto/cc/gcc
root/nginx-1.25.1/auto/cc/icc

3.2、将当前工作目录内所有以 .bak 为后缀的文件打包,不进行压缩

[root@fishman-160 yum.repos.d]# tar -cvf bak.tar ./*.bak
./CentOS7-local.repo.bak
./CentOS-Stream-AppStream.repo.bak
./CentOS-Stream-BaseOS.repo.bak

3.3、将当前工作目录内的所有以 .db 为后缀的文件打包,不进行压缩,并删除原始文件

[root@fishman-160 ~]# touch {1..5}file.db #创建.db文件
[root@fishman-160 ~]# ls
1file.db  4file.db  模板  文档  桌面                                 anaconda-ks.cfg                           libncurses5-32bit-6.4.20230701-16.2.x86_64.rpm  nginx-1.25.1.tar.gz  passwd
2file.db  5file.db  视频  下载  a是windows打开正常linux打开乱码.txt  b在linux编辑的文档熬windows下没有换行.sh  nework                                          nginx.tar.gz         password
3file.db  公共      图片  音乐  aa.txt                               initial-setup-ks.cfg                      nginx-1.25.1                                    nohup.out            test

[root@fishman-160 ~]# tar -cvf db.tar ./*.db --remove {1..5}.db #打包所有.db文件,并删除源文件
./1file.db
./2file.db
./3file.db
./4file.db
./5file.db
tar: 1.db:无法 stat: 没有那个文件或目录
tar: 2.db:无法 stat: 没有那个文件或目录
tar: 3.db:无法 stat: 没有那个文件或目录
tar: 4.db:无法 stat: 没有那个文件或目录
tar: 5.db:无法 stat: 没有那个文件或目录
tar: 由于前次错误,将以上次的错误状态退出
[root@fishman-160 ~]# ls
公共  图片  音乐                                 aa.txt                                    db.tar                                          nework               nginx.tar.gz  password
模板  文档  桌面                                 anaconda-ks.cfg                           initial-setup-ks.cfg                            nginx-1.25.1         nohup.out     test
视频  下载  a是windows打开正常linux打开乱码.txt  b在linux编辑的文档熬windows下没有换行.sh  libncurses5-32bit-6.4.20230701-16.2.x86_64.rpm  nginx-1.25.1.tar.gz  passwd
[root@fishman-160 ~]# tar -tf db.tar
./1file.db
./2file.db
./3file.db
./4file.db
./5file.db

3.4、同时打包两个目录或文件

[root@fishman-160 ~]# tar -cvf 2file.tar /root/nework/ ./nginx-1.25.1
tar: 从成员名中删除开头的“/”
/root/nework/
/root/nework/1.txt
.......

3.5、解压某个压缩包到指定工作目录

[root@fishman-160 ~]# tar -zxvf nginx-1.25.1.tar.gz -C /home/kc

3.6、追加新文件到指定压缩包

[root@fishman-160 ~]# tar -rvf 2file.tar db.tar #把db.tar追加到了2file.tar中
db.tar

4、压缩与解压缩

4.1、打包成bz2的压缩包

[root@fishman-160 ~]# tar -jcvf nginx.tar.bz2 nginx-1.25.1
# j(小写的j)表示使用bzip2进行压缩
nginx-1.25.1/
nginx-1.25.1/auto/
nginx-1.25.1/auto/cc/
nginx-1.25.1/auto/cc/acc
nginx-1.25.1/auto/cc/bcc
nginx-1.25.1/auto/cc/ccc
...

4.2、打包成xz的压缩包

[root@fishman-160 ~]# tar -Jcvf nginx.tar.xz nginx-1.25.1    
#J(大写的j)代表使用xz进行压缩 
nginx-1.25.1/
nginx-1.25.1/auto/
nginx-1.25.1/auto/cc/
nginx-1.25.1/auto/cc/acc
nginx-1.25.1/auto/cc/bcc
nginx-1.25.1/auto/cc/ccc
nginx-1.25.1/auto/cc/clan
...

4.3、打包成gz的压缩包

[root@fishman-160 ~]# tar -zcvf nginx.tar.gz nginx-1.25.1
...

4.4、压缩比的对比

[root@fishman-160 ~]# du -h nginx.tar*
24M	nginx.tar
4.7M	nginx.tar.bz2
6.5M	nginx.tar.gz
2.9M	nginx.tar.xz #压缩比率最高,压缩时间最长

[root@fishman-160 ~]# du -sh nginx-1.25.1 #du-sh查看源文件(目录)
25M	nginx-1.25.1

 

posted @   墨香清梦  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示