欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

linux学习lesson20

程序员文章站 2022-05-31 16:17:44
...

目录

1 zip压缩工具

2 tar打包

3 打包并压缩


1 zip压缩工具

zip压缩包在WindowsLinux中都比较常用,它可以压缩目录和文件,压缩目录时,需要指定目录下的文件

  • 安装zip包
[[email protected] ~]# yum install -y zip
  • 创建目录和文件
[[email protected] dir6]# touch file1.txt
[[email protected] dir6]# mkdir dir123
[[email protected] dir6]# ls
dir123  file1.txt
[[email protected] dir6]# echo "hello world" >> file1.txt
  • 压缩文件
[[email protected] dir6]# zip file1.txt.zip file1.txt
  adding: file1.txt (stored 0%)
[[email protected] dir6]# ls
dir123  file1.txt  file1.txt.zip
  • 压缩目录
[[email protected] dir6]# zip -r dir123.zip dir123/
  adding: dir123/ (stored 0%)
  adding: dir123/file1.txt (stored 0%)
[[email protected] dir6]# ls
dir123  dir123.zip  file1.txt  file1.txt.zip

 

  • 安装unzip包
[[email protected] dir6]# yum install -y unzip
  • 解压文件
[[email protected] dir6]# unzip file1.txt.zip
Archive:  file1.txt.zip
replace file1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
extracting: file1.txt               
[[email protected] dir6]# ls
dir123  dir123.zip  file1.txt  file1.txt.zip
  • 指定解压文件到目录下
[[email protected] dir6]# unzip dir123.zip -d /tmp/
Archive:  dir123.zip
   creating: /tmp/dir123/
extracting: /tmp/dir123/file1.txt   
[[email protected] dir6]# ls /tmp/
dir123  test.txt.new  test.txt.new2
[[email protected] dir6]# tree dir123
dir123
└── file1.txt

0 directories, 1 file

 

2 tar打包

tar本身就是一个打包工具,可以把目录打包成一个文件,它把所有文件整合成一个大文件,方便复制或者移动。该命令的格式为tar [-zjxcvfpP] filename tar,它有多个选项

-z:表示同时用gzip压缩。

-j:表示同时用bzip2压缩。

-J:表示同时用xz压缩。

-x:表示解包或者解压缩。

-t:表示查看tar包里的文件。

-c:表示建立一个tar包或者压缩文件包。

-v:表示可视化。

-f:后面跟文件名(即-f filename,表示压缩后的文件名为filename,或者解压文件filename

需要注意的是,如果是多个参数组合的情况下,请把-f参数写到最后面。

-p:表示使用原文件的属性,压缩前什么属性压缩后还什么属性。(不常用)

-P:表示可以使用绝对路径。(不常用)

--exclude filename:表示在打包或压缩时,不要将filename文件包括在内。(不常用)

  • 打包目录
[[email protected] dir6]# tar -cvf dir123.tar dir123/
dir123/
dir123/file1.txt
[[email protected] dir6]# ls
dir123  dir123.tar  file1.txt
  • 打包目录和文件
[[email protected] dir6]# tar -cvf package.tar file1.txt dir123/
file1.txt
dir123/
dir123/file1.txt
[[email protected] dir6]# ls
dir123  dir123.tar  file1.txt  package.tar
  • 解包
[[email protected] dir6]# ls
dir123  dir123.tar  file1.txt  package.tar
[[email protected] dir6]# tar -xvf package.tar
file1.txt
dir123/
dir123/file1.txt
  • 查看包里的文件
[[email protected] dir6]# tar -tf package.tar
file1.txt
dir123/
dir123/file1.txt
  • 打包并将不需要的文件或目录过滤
[[email protected] dir6]# mkdir 123
[[email protected] dir6]# echo "1234556" >> 1.txt
[[email protected] dir6]# ls
123  1.txt  dir123  file1.txt
[[email protected] dir6]# tar -cvf package.tar --exclude 1.txt --exclude 2 123 dir123/ file1.txt
123/
dir123/
dir123/file1.txt
file1.txt
[[email protected] dir6]# ls
123  1.txt  dir123  file1.txt  package.tar


 

3 打包并压缩

  • tar.gz格式打包压缩
[[email protected] dir6]# tar -zcvf dir123.tar.gz 123/
123/
123/1.txt
123/2/
[[email protected] dir6]# ls
123  1.txt  dir123  dir123.tar.gz  file1.txt
  • tar.gz格式解压缩
[[email protected] dir6]# tar -zxvf dir123.tar.gz
123/
123/1.txt
123/2/
[[email protected] dir6]# ls
123  1.txt  dir123  dir123.tar.gz  file1.txt
  • tar.bz2格式打包压缩
[[email protected] dir6]# tar -jxvf dir.tar.bz2
dir123/
dir123/file1.txt
[[email protected] dir6]# ls
123  1.txt  dir123  dir123.tar.gz  dir.tar.bz2  file1.txt
  • tar.bz2格式解压缩
[[email protected] dir6]# tar -jxvf dir.tar.bz2
dir123/
dir123/file1.txt
[[email protected] dir6]# ls
123  1.txt  dir123  dir123.tar.gz  dir.tar.bz2  file1.txt
  • tar.xz格式打包压缩
[[email protected] dir6]# tar -Jcvf dir123.tar.xz dir123/
dir123/
dir123/file1.txt
[[email protected] dir6]# ls
123  1.txt  dir123  dir123.tar.gz  dir123.tar.xz  dir.tar.bz2  file1.txt
  • tar.xz格式解压缩
[[email protected] dir6]# tar -Jxvf dir123.tar.xz dir123/
dir123/
dir123/file1.txt
[[email protected] dir6]# ls
123  1.txt  dir123  dir123.tar.gz  dir123.tar.xz  dir.tar.bz2  file1.txt
  • 查看包的文件
[[email protected] dir6]# tar -tf dir123.tar.gz
123/
123/1.txt
123/2/
[[email protected] dir6]# tar -tf dir.tar.bz2
dir123/
dir123/file1.txt
[[email protected] dir6]# tar -tf dir123.tar.xz
dir123/
dir123/file1.txt

拓展学习:

http://ask.apelearn.com/question/5435