西河刘卡车医 发表于 2024-9-29 07:15:49

Linux常用打包压缩命令

压缩命令应用场景tar大部门利用tar即可。创建,检察,解压,解压到指定目次gzip一般共同其他命令利用zip+unzip一般用于解压zip格式的压缩包 1. tar 打包压缩命令

1.1 命令详解

【功能阐明】
在Linux体系里,tar是将多个文件打包在一起,并且可以实现解压打包的文件的命令。是体系管理员最常用的命令之一,tar命令不但可以实现对多个文件进行打包,还可以对多个文件打包后进行压缩。
打包是指将一大堆文件或目次变成一个总的文件,压缩则是将一个大的文件通过一些压缩算法变成一个小文件。
【语法格式】
tar
tar [选项]   [文件或目录]
【选项阐明】
参数选项解释阐明z通过gzip压缩或解压c创建新的tar包v显示详细的tar命令执行过程f指定压缩文件的名字t不解压检察tar包的内容p保持文件的原有属性P(大写)以绝对路径打包,伤害参数j通过bzip2命令压缩或解压x解开tar包C指定解压的目次路径–exclude=PATTERN打包时排除不需要处理的文件或目次-X file从指定文件读取不需要处理的文件或目次列表-N date仅打包比指定日期新的文件,可用于增量打包备份-h打包软链接文件指向的真实源文件–hard-dereference打包硬链接文件 1.2 利用范例

   tar命令主要的用途是:


[*]创建压缩包
[*]检察压缩包
[*]解压压缩包
[*]解压压缩包到指定目次
tar命令选项与阐明创建 zcf/zcvftar zcf 压缩包 被压缩的文件/目次检察 tf/ztftar tf /tmp/etc.tar.gz纵然不指定z选项,tar命令也会自动判定压缩包类型,自动调用gzip解压 xf/xvftar xf /tmp/etc.tar.gz # 案例1.打包当前的1.txt文件为test.tar.gz
# echo aaa > 1.txt
# ll
total 4
-rw-r--r-- 1 root root 4 Nov 10 12:19 1.txt
# cat 1.txt
aaa

# tar zcvf test.tar.gz 1.txt
1.txt
# ll
total 8
-rw-r--r-- 1 root root   4 Nov 10 12:19 1.txt
-rw-r--r-- 1 root root 118 Nov 10 12:20 test.tar.gz

# 案例2.同时打包多个文件
#打包/etc/hosts 1.txt /etc/passwd
# tar zcvf test.tar.gz /etc/hosts /etc/passwd 1.txt
tar: Removing leading `/' from member names
/etc/hosts
/etc/passwd
1.txt

# 注意: 打包建议到相对路径
# tar zcvf dir.tar.gz etc test
etc/
etc/hosts
etc/passwd
test/
# ll
total 12
-rw-r--r-- 1 root root   4 Nov 10 12:19 1.txt
-rw-r--r-- 1 root root 695 Nov 10 12:26 dir.tar.gz
drwxr-xr-x 2 root root33 Nov 10 12:24 etc
drwxr-xr-x 2 root root   6 Nov 10 12:26 test

# 案例3.解压test.tar.gz 到当前目录
# mv test.tar.gz /opt/
# cd /opt/
# tar xf test.tar.gz
# ll
total 8
-rw-r--r-- 1 root root   4 Nov 10 12:19 1.txt
drwxr-xr-x 2 root root33 Nov 10 12:28 etc
-rw-r--r-- 1 root root 675 Nov 10 12:22 test.tar.gz
# ll etc/
total 8
-rw-r--r-- 1 root root158 Nov3 10:25 hosts
-rw-r--r-- 1 root root 1017 Oct 102008 passwd

# 案例4.指定解压到某个位置-C
# tar xf 1.tar.gz -C /opt/
# 案例5.压缩包在/root下 想在/opt下解压
# ll
total 4
-rw-r--r-- 1 root root 695 Nov 10 12:26 dir.tar.gz
# cd /opt/
# rm -rf *
# ll
total 0

# ll
total 4
-rw-r--r-- 1 root root 695 Nov 10 12:26 dir.tar.gz
# cd /opt/
# rm -rf *
# ll
total 0
# ll
total 0
# tar xf /root/dir.tar.gz
# ll
total 0
drwxr-xr-x 2 root root 33 Nov 10 12:24 etc
drwxr-xr-x 2 root root6 Nov 10 12:26 test
2. gzip 压缩或解压文件

2.1 命令详情

【功能阐明】
gzip命令用于将一个大的文件通过压缩算法 (Lempel-Ziv coding (LZ77))变成一个小的文件。gzip命令不能直接压缩目次,因此目次需要先用tar打包成一个文件,然后tar再调用gzip进行压缩。
【语法格式】
gzip
gzip   [选项] [文件]
【选项阐明】
参数选项解释阐明-d解开压缩文件-v显示指令执行的过程-l列出压缩文件的内容信息-c将内容输出到标准输出,不改变原始文件-r对目次下的所有文件递归进行压缩操纵-n (n这里代指数字,范围1~9)指定压缩率,默认为6,值越大压缩率越高-t测试,检查压缩文件是否完备 2.2 利用范例

# ls
1.txt2.txt3.html4.html
# gzip *.txt
# ls
1.txt.gz2.txt.gz3.html4.html
# ".gz"后缀是gzip命令自动添加的。
# gzip命令的缺点是压缩后源文件不见了,它的特性是压缩、解压都会自动删除源文件。

# 不解压显示每个压缩文件的信息。使用-l参数
# 压缩率为0是因为,源文件为空文件。
# gzip -l *.txt.gz
         compressed      uncompressedratio uncompressed_name
               26                   0   0.0% 1.txt
               26                   0   0.0% 2.txt
                122               1642999.4% 5.txt
                174               1642999.1% (totals)

# 实际解压文件
# gzip -dv *.txt.gz
1.txt.gz:         0.0% -- replaced with 1.txt
2.txt.gz:         0.0% -- replaced with 2.txt
5.txt.gz:      99.4% -- replaced with 5.txt
# -d 解压缩;-v 显示过程
# ls
1.txt2.txt3.html4.html5.txt
# 查看解压后的结果,gz文件不存在了,只剩下txt文件。
3. zip 打包和压缩文件

3.1 命令详解

【功能阐明】
zip压缩格式是 Windows 与 Linux等多平台通用的压缩格式。和gzip命令相比,zip命令压缩文件不但不会删除源文件,而且还可以压缩目次。
【语法格式】
zip
zip   [选项] [文件或目录]
【选项阐明】
参数选项解释阐明-r递归压缩,将指定目次下的所有文件和子目次一并压缩-x压缩文件时排除某个文件-q不显示压缩信息 3.2 利用范例

# 压缩文件1.txt和2.txt
# ls
1.txt2.txt3.html4.html5.txt
# zip 1-2.zip 1.txt 2.txt
adding: 1.txt (stored 0%)
adding: 2.txt (stored 0%)
# ls
1-2.zip1.txt2.txt3.html4.html5.txt
# 源文件依然存在

# 压缩目录
# zip -r test.zip test/
adding: test/ (stored 0%)
adding: test/2.txt (stored 0%)
adding: test/1.txt (stored 0%)
adding: test/3.html (stored 0%)
adding: test/1-2.zip (stored 0%)
adding: test/5.txt (deflated 99%)
adding: test/4.html (stored 0%)
# ls
testtest.zip

# 排除压缩,指定某些文件不进行压缩
# zip -r test.zip test/ -x test/5.txt
adding: test/ (stored 0%)
adding: test/2.txt (stored 0%)
adding: test/1.txt (stored 0%)
adding: test/3.html (stored 0%)
adding: test/1-2.zip (stored 0%)
adding: test/4.html (stored 0%)
4. unzip 解压zip文件

4.1 命令详解

【功能阐明】
unzip命令可以解压zip命令或其他压缩软件压缩的zip格式的文件。
【语法格式】
unzip
unzip [选项] [压缩文件]
【选项阐明】
参数选项解释阐明-l不解压显示压缩包内容-d指定解压目次-o解压时不提示是否覆盖文件-v解压时显示详细信息 4.2 利用范例

# 查看压缩文件信息
# ls
testtest.zip
# unzip -l test.zip
Archive:test.zip
Length      Date    Time    Name
------------------- -----   ----
      001-04-2024 13:35   test/
      012-29-2023 12:55   test/2.txt
      012-29-2023 12:55   test/1.txt
      001-04-2024 11:17   test/3.html
      29801-04-2024 13:35   test/1-2.zip
      001-04-2024 11:17   test/4.html
---------                     -------
      298                     6 files

# 直接进行源路径解压,会提示是否覆盖源文件
# unzip test.zip
Archive:test.zip
replace test/2.txt? es, o, ll, one, ename: y
extracting: test/2.txt            
......         
replace test/4.html? es, o, ll, one, ename: y
extracting: test/4.html            
# ls
testtest.zip

# 解压时不进行提示是否覆盖
# unzip -o test.zip
Archive:test.zip
extracting: test/2.txt            
extracting: test/1.txt            
extracting: test/3.html            
extracting: test/1-2.zip            
extracting: test/4.html

# 指定解压目录解压文件
# unzip -d test/ test.zip
Archive:test.zip
   creating: test/test/
extracting: test/test/2.txt         
extracting: test/test/1.txt         
extracting: test/test/3.html      
extracting: test/test/1-2.zip      
extracting: test/test/4.html
# tree
.
├── test
│   ├── 1-2.zip
│   ├── 1.txt
│   ├── 2.txt
│   ├── 3.html
│   ├── 4.html
│   ├── 5.txt
│   └── test
│       ├── 1-2.zip
│       ├── 1.txt
│       ├── 2.txt
│       ├── 3.html
│       └── 4.html
└── test.zip

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Linux常用打包压缩命令