Linux常用打包压缩命令

打印 上一主题 下一主题

主题 1058|帖子 1058|积分 3174

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

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

1.1 命令详解

【功能阐明】
在Linux体系里,tar是将多个文件打包在一起,并且可以实现解压打包的文件的命令。是体系管理员最常用的命令之一,tar命令不但可以实现对多个文件进行打包,还可以对多个文件打包后进行压缩。
打包是指将一大堆文件或目次变成一个总的文件,压缩则是将一个大的文件通过一些压缩算法变成一个小文件。
【语法格式】
  1. tar [option] [file]
  2. 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.打包当前的1.txt文件为test.tar.gz
  2. [root@localhost ~]# echo aaa > 1.txt
  3. [root@localhost ~]# ll
  4. total 4
  5. -rw-r--r-- 1 root root 4 Nov 10 12:19 1.txt
  6. [root@localhost ~]# cat 1.txt
  7. aaa
  8. [root@localhost ~]# tar zcvf test.tar.gz 1.txt
  9. 1.txt
  10. [root@localhost ~]# ll
  11. total 8
  12. -rw-r--r-- 1 root root   4 Nov 10 12:19 1.txt
  13. -rw-r--r-- 1 root root 118 Nov 10 12:20 test.tar.gz
  14. # 案例2.同时打包多个文件
  15. #打包/etc/hosts 1.txt /etc/passwd
  16. [root@localhost ~]# tar zcvf test.tar.gz /etc/hosts /etc/passwd 1.txt
  17. tar: Removing leading `/' from member names
  18. /etc/hosts
  19. /etc/passwd
  20. 1.txt
  21. # 注意: 打包建议到相对路径
  22. [root@localhost ~]# tar zcvf dir.tar.gz etc test
  23. etc/
  24. etc/hosts
  25. etc/passwd
  26. test/
  27. [root@localhost ~]# ll
  28. total 12
  29. -rw-r--r-- 1 root root   4 Nov 10 12:19 1.txt
  30. -rw-r--r-- 1 root root 695 Nov 10 12:26 dir.tar.gz
  31. drwxr-xr-x 2 root root  33 Nov 10 12:24 etc
  32. drwxr-xr-x 2 root root   6 Nov 10 12:26 test
  33. # 案例3.解压test.tar.gz 到当前目录
  34. [root@localhost ~]# mv test.tar.gz /opt/
  35. [root@localhost ~]# cd /opt/
  36. [root@localhost opt]# tar xf test.tar.gz
  37. [root@localhost opt]# ll
  38. total 8
  39. -rw-r--r-- 1 root root   4 Nov 10 12:19 1.txt
  40. drwxr-xr-x 2 root root  33 Nov 10 12:28 etc
  41. -rw-r--r-- 1 root root 675 Nov 10 12:22 test.tar.gz
  42. [root@localhost opt]# ll etc/
  43. total 8
  44. -rw-r--r-- 1 root root  158 Nov  3 10:25 hosts
  45. -rw-r--r-- 1 root root 1017 Oct 10  2008 passwd
  46. # 案例4.指定解压到某个位置-C
  47. [root@localhost ~]# tar xf 1.tar.gz -C /opt/
  48. # 案例5.压缩包在/root下 想在/opt下解压
  49. [root@localhost ~]# ll
  50. total 4
  51. -rw-r--r-- 1 root root 695 Nov 10 12:26 dir.tar.gz
  52. [root@localhost ~]# cd /opt/
  53. [root@localhost opt]# rm -rf *
  54. [root@localhost opt]# ll
  55. total 0
  56. [root@localhost ~]# ll
  57. total 4
  58. -rw-r--r-- 1 root root 695 Nov 10 12:26 dir.tar.gz
  59. [root@localhost ~]# cd /opt/
  60. [root@localhost opt]# rm -rf *
  61. [root@localhost opt]# ll
  62. total 0
  63. [root@localhost opt]# ll
  64. total 0
  65. [root@localhost opt]# tar xf /root/dir.tar.gz
  66. [root@localhost opt]# ll
  67. total 0
  68. drwxr-xr-x 2 root root 33 Nov 10 12:24 etc
  69. drwxr-xr-x 2 root root  6 Nov 10 12:26 test
复制代码
2. gzip 压缩或解压文件

2.1 命令详情

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

  1. [root@localhost test]# ls
  2. 1.txt  2.txt  3.html  4.html
  3. [root@localhost test]# gzip *.txt
  4. [root@localhost test]# ls
  5. 1.txt.gz  2.txt.gz  3.html  4.html
  6. # ".gz"后缀是gzip命令自动添加的。
  7. # gzip命令的缺点是压缩后源文件不见了,它的特性是压缩、解压都会自动删除源文件。
  8. # 不解压显示每个压缩文件的信息。使用-l参数
  9. # 压缩率为0是因为,源文件为空文件。
  10. [root@localhost test]# gzip -l *.txt.gz
  11.          compressed        uncompressed  ratio uncompressed_name
  12.                  26                   0   0.0% 1.txt
  13.                  26                   0   0.0% 2.txt
  14.                 122               16429  99.4% 5.txt
  15.                 174               16429  99.1% (totals)
  16. # 实际解压文件
  17. [root@localhost test]# gzip -dv *.txt.gz
  18. 1.txt.gz:         0.0% -- replaced with 1.txt
  19. 2.txt.gz:         0.0% -- replaced with 2.txt
  20. 5.txt.gz:        99.4% -- replaced with 5.txt
  21. # -d 解压缩;-v 显示过程
  22. [root@localhost test]# ls
  23. 1.txt  2.txt  3.html  4.html  5.txt
  24. # 查看解压后的结果,gz文件不存在了,只剩下txt文件。
复制代码
3. zip 打包和压缩文件

3.1 命令详解

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

  1. # 压缩文件1.txt和2.txt
  2. [root@localhost test]# ls
  3. 1.txt  2.txt  3.html  4.html  5.txt
  4. [root@localhost test]# zip 1-2.zip 1.txt 2.txt
  5.   adding: 1.txt (stored 0%)
  6.   adding: 2.txt (stored 0%)
  7. [root@localhost test]# ls
  8. 1-2.zip  1.txt  2.txt  3.html  4.html  5.txt
  9. # 源文件依然存在
  10. # 压缩目录
  11. [root@localhost ~]# zip -r test.zip test/
  12.   adding: test/ (stored 0%)
  13.   adding: test/2.txt (stored 0%)
  14.   adding: test/1.txt (stored 0%)
  15.   adding: test/3.html (stored 0%)
  16.   adding: test/1-2.zip (stored 0%)
  17.   adding: test/5.txt (deflated 99%)
  18.   adding: test/4.html (stored 0%)
  19. [root@localhost ~]# ls
  20. test  test.zip
  21. # 排除压缩,指定某些文件不进行压缩
  22. [root@localhost ~]# zip -r test.zip test/ -x test/5.txt
  23.   adding: test/ (stored 0%)
  24.   adding: test/2.txt (stored 0%)
  25.   adding: test/1.txt (stored 0%)
  26.   adding: test/3.html (stored 0%)
  27.   adding: test/1-2.zip (stored 0%)
  28.   adding: test/4.html (stored 0%)
复制代码
4. unzip 解压zip文件

4.1 命令详解

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

  1. # 查看压缩文件信息
  2. [root@localhost ~]# ls
  3. test  test.zip
  4. [root@localhost ~]# unzip -l test.zip
  5. Archive:  test.zip
  6.   Length      Date    Time    Name
  7. ---------  ---------- -----   ----
  8.         0  01-04-2024 13:35   test/
  9.         0  12-29-2023 12:55   test/2.txt
  10.         0  12-29-2023 12:55   test/1.txt
  11.         0  01-04-2024 11:17   test/3.html
  12.       298  01-04-2024 13:35   test/1-2.zip
  13.         0  01-04-2024 11:17   test/4.html
  14. ---------                     -------
  15.       298                     6 files
  16. # 直接进行源路径解压,会提示是否覆盖源文件
  17. [root@localhost ~]# unzip test.zip
  18. Archive:  test.zip
  19. replace test/2.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
  20. extracting: test/2.txt              
  21. ......           
  22. replace test/4.html? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
  23. extracting: test/4.html            
  24. [root@localhost ~]# ls
  25. test  test.zip
  26. # 解压时不进行提示是否覆盖
  27. [root@localhost ~]# unzip -o test.zip
  28. Archive:  test.zip
  29. extracting: test/2.txt              
  30. extracting: test/1.txt              
  31. extracting: test/3.html            
  32. extracting: test/1-2.zip            
  33. extracting: test/4.html
  34. # 指定解压目录解压文件
  35. [root@localhost ~]# unzip -d test/ test.zip
  36. Archive:  test.zip
  37.    creating: test/test/
  38. extracting: test/test/2.txt         
  39. extracting: test/test/1.txt         
  40. extracting: test/test/3.html        
  41. extracting: test/test/1-2.zip      
  42. extracting: test/test/4.html
  43. [root@localhost ~]# tree
  44. .
  45. ├── test
  46. │   ├── 1-2.zip
  47. │   ├── 1.txt
  48. │   ├── 2.txt
  49. │   ├── 3.html
  50. │   ├── 4.html
  51. │   ├── 5.txt
  52. │   └── test
  53. │       ├── 1-2.zip
  54. │       ├── 1.txt
  55. │       ├── 2.txt
  56. │       ├── 3.html
  57. │       └── 4.html
  58. └── test.zip
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

西河刘卡车医

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表