文件操作之zip、bzip2、gzip、tar命令

原创

丁同学19902015-10-15 00:02:51博主文章分类:liunx基础著作权

文章标签linux tarlinux文件压缩linux压缩文件linux命名方式linux gzip文章分类运维阅读数2808

先讲些简单的概念,因为对于初学者对linux下压缩文件容易搞晕。因为我们常见压缩文件为.zip,.tar.zip,.bzip2,.tar.bz2,.gz,.Z,.tar等等。

首先要理解的是在linux系统存在“打包”和“压缩”两个概念。“打包”只是把一个或多个文档打包放到一个文档中而已,不存在压缩。“压缩”是通过压缩方法将文件压缩更小文件,简单的理解就是通过压缩方法将文件重新编码成占用空间更小的文件(并不是绝对的,对于大文件会更小,但对于很小文件需要记录压缩信息可能会导致压缩后文件更大)。

linux下常用打包命令:tar。为了易读性,一般将文件命名为.tar文件,所以.tar文件并不是压缩文件。

linux下常用压缩命令就很多了,如:zip,gzip,bzip2,compress等。那怎么区分呢?为了易于区分一般通过命名方式来区别。.zip为zip方式压缩文件,.gz为gzip方式压缩文件,.bz2为bzip2压缩文件,.Z为compress压缩文件。所以我们看到.tar.gz文件即为先打包后通过gzip压缩的文件,其他与此类似。

一、zip命令

1,常用参数:

-d:从压缩文件内删除指定的文件。

-g:将文件压缩后附加在既有的压缩文件之后,而非另行建立新的压缩文件。

-r:递 归处理,将指定目录下的所有文件和子目录一并处理。

-<压缩效率>   压 缩效率是一个介于1-9的 数值。

2,实例

#将test.txt文件压缩为test.zip文件

[root@localhost test]# zip test.ziptest.txt
adding: test.txt (deflated 35%)
 
 

#压缩率为1,最高压缩率,当然也是最慢的压缩方法

[root@localhost test]# zip -1 test.ziptest.txt
adding: test.txt(deflated 35%)
 

#压缩率依然为35,因为压缩文件为文本文件压缩率基本不变

#-r递归压缩子目录

[root@localhost test]# zip -r test.zip./test/
adding: test/ (stored 0%)
adding: test/test/ (stored 0%)
adding: test/test.txt (deflated 35%)
 
 

#-d 删除已有zip文件中文件

[root@localhost test]# zip -r test.zip test
adding: test/ (stored 0%)
adding: test/test/ (stored 0%)
adding: test/test.txt (deflated 35%)
[root@localhost test]# zip -d test.ziptest/test.txt
deleting: test/test.txt
 

#-m向已有zip文件增加压缩文件

[root@localhost test]# zip -m test.zip./test/test.txt
adding: test/test.txt (deflated 35%)
 
 

#-x排除制定文件不压缩

[root@localhost test]# zip -r test.zip test-x ./test/test.txt
updating: test/ (stored 0%)
updating: test/test/ (stored 0%)
updating: test/test2.txt (deflated 63%)
 
 

二、unzip命令:解压缩命令,即zip反操作命令。

1,常用参数

-t   检查压缩文件是否正确。,但不解压。

-v   执行是时显示详细的信息。或查看压缩文件目录,但不解压。

-n   解压缩时不要覆盖原有的文件。

-o   不必先询问用户,unzip执 行后覆盖原有文件,即强制覆盖。

-j   不处理压缩文件中原有的目录路径。

-P<密码>   使用zip的密码选项。

2,实例

#解压缩文件test.zip

[root@localhost test]# unzip test.zip
Archive: test.zip
creating: test/
creating: test/test/
inflating: test/test2.txt
 
 

#-v查看压缩文件目录及文件信息,并不解压

[root@localhost test]# unzip -v test.zip
Archive: test.zip
Length Method Size Cmpr Date Time CRC-32 Name
-------- ------ ------- ---- --------------- -------- ----
0 Stored 0 0% 10-08-2015 22:41 00000000 test/
0 Stored 0 0% 10-08-2015 22:24 00000000 test/test/
2117 Defl:N 781 63% 10-08-2015 22:41 ef2699cd test/test2.txt
-------- ------- --- -------
2117 781 63% 3 files
 
 

三、gzip命令:gzip [选项] 压缩(解压缩)的文件名

1,选项参数

-d:将压缩文件解压,gzip命令是通过-d来实现解压缩的。

-r:同样是递归压缩。

-t:测试,检查文件完整性。

-v:显示压缩详细过程信息。

-<数字>:指定压缩率(速度)。

2,实例

#gzip是单个压缩文件的,-r递归压缩

[root@localhost test]# gzip -r test
[root@localhost test]# ls test
test test2.txt.gz test.txt.gz
[root@localhost test]# ls ./test/test
test.txt.gz
[root@localhost test]# gzip test
gzip: test is a directory – ignored
[root@localhost test]# tree test
test
├── test
│ └── test.txt.gz
├── test2.txt.gz
└── test.txt.gz
 
 

#解压文件

[root@localhost test]# gzip -rd test
[root@localhost test]# tree test
test
├── test
│ └── test.txt
├── test2.txt
└── test.txt 1 directory, 3 files
 
 

#先使用tar命令打包,再压缩

[root@localhost test]# tar -cvf test.tartest
test/
test/test/
test/test/test.txt
test/test2.txt
test/test.txt
[root@localhost test]# ls
test test.tar test.txt
[root@localhost test]# gzip test.tar
 
 

#-<数字>自定义压缩率,默认为6

[root@localhost test]# gzip -v -1 test.tar
test.tar: 88.4% -- replaced with test.tar.gz
[root@localhost test]# gzip -d test.tar.gz
[root@localhost test]# gzip -v -9 test.tar
test.tar: 89.7% -- replaced with test.tar.gz
 
 

四、bzip2命令:命令与gzip类似。

1,实例

#bzip2不能压缩目录,也不在类似-r的可选项参数。

[root@localhost test]# bzip2 test
bzip2: Input file test is a directory.
[root@localhost test]# bzip2 test.txt
[root@localhost test]# ls
test test.tar test.txt.bz2
 
 

#同样可以通过-d实现解压缩

[root@localhost test]# bzip2 -dtest.txt.bz2
[root@localhost test]# bzip2 test.tar
[root@localhost test]# bzip2 -vdtest.tar.bz2
test.tar.bz2: done
[root@localhost test]# ls
test test.tar test.txt
 
 

#通过bzcat命令可以直接读压缩文件信息

[root@localhost test]# ls
test test.tar.bz2 test.txt.bz2
[root@localhost test]# bzcat test.txt.bz2
test
test2
test12
 
 

五、tar 命令:tar实际是一个打包命令,但是它可以调用压缩bzip2,gzip来实现压缩。

1,选项参数

tar选项可以分为二部分。

第一部分:主选项

-c:--create 创建新文档,即打包文件。

-x:--extract(提取),从文档中释放文件,即将打包文件拆包。

-t:  --list 列出文档中的内容,即查看打包内的内容。

以上三个为必选,但仅能存在一个,不可能同时打包又拆包。

第二部分:辅助选项(常用)

-f:--file=ARCHIVE用户指定打包后的文件或解包后的文件名。后面接的一定是文件名。

-z:即使用gzip压缩/解压。调用gzip来实现解/压缩,一般格式为.tar.gz或.tgz。

-j:即调用bzip2来解/压缩,一般格式为.tar.bz2

-v:   --verbose显示解/压缩过程信息。

-p:preserve-permissions保留权限,即使用原来属性。

--exclude FILE:在压缩过程中,将FILE排除在外,即对FILE不打包。

2,实例

#最简单的打包命令,必须包含-c(打包),-f指定打包后文件。

[root@localhost test]# tar -cf test.tartest
[root@localhost test]# ls
f1 f2 test test.tar
 
 

#打包并且使用bzip2压缩文件

[root@localhost test]# tar -cjvftest.tar.bz2 test
test/
test/test.tar.bz2
test/test/
test/test/test/
test/test/test/test.txt
test/test/test2.txt
test/test/test.txt
test/test.txt.bz2
[root@localhost test]# ls
f1 f2 test test.tar.bz2
 
 

#解压缩bzip2压缩包

[root@localhost test]# tar -xjvftest.tar.bz2
test/
test/test.tar.bz2
test/test/
test/test/test/
test/test/test/test.txt
test/test/test2.txt
test/test/test.txt
test/test.txt.bz2
 
 

#打包绝对路径文件:会提示将“/”移除

[root@localhost test]# tar -cvf passwd.tar/etc/passwd
tar: Removing leading `/' from member names
/etc/passwd
 

#查看打包文件,可以看到根(/)符号去掉了。

[root@localhost test]# tar -tf passwd.tar
etc/passwd
 
 

3,tar还有很多很有用但又不常用的选项

默认情况下tar在拆包是无法显示将拆包后文件指定到其他目录,这是就可以用到-C+dir命令了。如下:

[root@localhost test]# tree
.
├── f1
├── f2
└── test 1 directory, 2 files
[root@localhost test]# tar -cvf f.tar f1 f2
f1
f2
[root@localhost test]# tar -xf f.tar./test/
tar: ./test: Not found in archive
tar: Exiting with failure status due toprevious errors
[root@localhost test]# tar -xf f.tar -C./test/
[root@localhost test]# tree
.
├── f1
├── f2
├── f.tar
└── test
├── f1
└── f2 1 directory, 5 files
 
 

-d: --diff,--compare 找出归档文件和文件系统的不同之处

-r:--append将文件附加到打包文件之后。

-u: --update 只打包文档中的新文件

-A:--catenate 将tar文件附加到归档文件之后

--delete:将文件从打包文件中删除

[root@localhost test]# tar -cvf f1.tar f1
f1
[root@localhost test]# tar -cvf f2.tar f2
f2
[root@localhost test]# tar -tvf f.tar
-rw-r----- root/root 34 2015-10-08 04:05 f2
-rw-r----- root/root 34 2015-10-08 04:05 f1
[root@localhost test]# tar -A f1.tar -vff.tar
[root@localhost test]# tar -tvf f.tar
-rw-r----- root/root 34 2015-10-08 04:05 f2
-rw-r----- root/root 34 2015-10-08 04:05 f1
-rw-r----- root/root 34 2015-10-08 04:05 f1
[root@localhost test]# tar --delete f2 -vff.tar
[root@localhost test]# tar -tvf f.tar
-rw-r----- root/root 34 2015-10-08 04:05 f1
-rw-r----- root/root 34 2015-10-08 04:05 f1
[root@localhost test]# tar -r f2 -vf f.tar
f2
[root@localhost test]# tar -u f1 -vf f.tar
[root@localhost test]# tar -tvf f.tar
-rw-r----- root/root 34 2015-10-08 04:05 f1
-rw-r----- root/root 34 2015-10-08 04:05 f1
-rw-r----- root/root 34 2015-10-08 04:05 f2
[root@localhost test]# touch f3
[root@localhost test]# tar -u f3 -vf f.tar
f3
[root@localhost test]# tar -tvf f.tar
-rw-r----- root/root 34 2015-10-08 04:05 f1
-rw-r----- root/root 34 2015-10-08 04:05 f1
-rw-r----- root/root 34 2015-10-08 04:05 f2
-rw-r--r-- root/root 0 2015-10-09 02:48 f3
-N date:--newer=date 经常用于备份新文件。
[root@localhost test]# ll
total 48
-rw-r-----. 1 root root 34 Oct 8 04:05 f1
-rw-r--r--. 1 root root 10240 Oct 9 02:45 f1.tar
-rw-r-----. 1 root root 34 Oct 8 04:05 f2
-rw-r--r--. 1 root root 10240 Oct 9 02:45 f2.tar
-rw-r--r--. 1 root root 0 Oct 9 02:48 f3
-rw-r--r--. 1 root root 10240 Oct 9 02:48 f.tar
drwxr-xr-x. 2 root root 4096 Oct 9 01:46 test
[root@localhost test]# tar -N '2015/10/9'-cvf N.tar ./*
tar: Option --after-date: Treating date`2015/10/9' as 2015-10-09 00:00:00
tar: ./f1: file is unchanged; not dumped
./f1.tar
tar: ./f2: file is unchanged; not dumped
./f2.tar
./f3
./f.tar
./test/
./test/f2
./test/f1
[root@localhost test]# ls
f1 f1.tar f2 f2.tar f3 f.tar N.tar test
[root@localhost test]# tar -tvf N.tar
-rw-r--r-- root/root 10240 2015-10-09 02:45 ./f1.tar
-rw-r--r-- root/root 10240 2015-10-09 02:45 ./f2.tar
-rw-r--r-- root/root 0 2015-10-09 02:48 ./f3
-rw-r--r-- root/root 10240 2015-10-09 02:48 ./f.tar
drwxr-xr-x root/root 0 2015-10-09 01:46 ./test/
-rw-r----- root/root 34 2015-10-08 04:05 ./test/f2
-rw-r----- root/root 34 2015-10-08 04:05 ./test/f1
--mode=permissions
 

备份时,把加入备份文件中的文件属性修改为指定的属性,格式和chmod命令接受的格式相同

--group=group

备份时,把加入备份文件中的文件所属组设定成指定的组

--owner=owner

备份时,把把加入备份文件中的文件所有者设定成指定的用户

[root@localhost test]# tar --owner=test--group=ding --mode=764 -cf test.tar ./*
[root@localhost test]# ll test.tar
-rw-r--r--. 1 root root 81920 Oct 9 02:58 test.tar
[root@localhost test]# tar -tvf test.tar
-rwxrw-r-- test/ding 34 2015-10-08 04:05 ./f1
-rwxrw-r-- test/ding 10240 2015-10-09 02:45 ./f1.tar
-rwxrw-r-- test/ding 34 2015-10-08 04:05 ./f2
-rwxrw-r-- test/ding 10240 2015-10-09 02:45 ./f2.tar
-rwxrw-r-- test/ding 0 2015-10-09 02:48 ./f3
-rwxrw-r-- test/ding 10240 2015-10-09 02:48 ./f.tar
-rwxrw-r-- test/ding 40960 2015-10-09 02:52 ./N.tar
drwxrw-r-- test/ding 0 2015-10-09 01:46 ./test/
-rwxrw-r-- test/ding 34 2015-10-08 04:05 ./test/f2
-rwxrw-r-- test/ding 34 2015-10-08 04:05 ./test/f1
 
 

-k:--keep-old-files 解包时不覆盖已有的文件

-m:--modification-time 解包时不更改时间

-s –same-order :保持相同的顺序还原

还可以通过管道技术实现‘copy’功能。

[root@localhost test]# tar -cvf - ./* | tar-xvf - -C ./test/
./f1
./f2
./test/
./f1
./f2
./test/
[root@localhost test]# tree
.
├── f1
├── f2
└── test
├── f1
├── f2
└── test 2 directories, 4 files

[转帖]文件操作之zip、bzip2、gzip、tar命令的更多相关文章

  1. Java文件操作API功能与Windows DOS命令和Linux Shell 命令类比

    Java文件操作API功能与Windows DOS命令和Linux Shell 命令类比: Unix/Linux (Bash) Windows(MS-DOS) Java 进入目录 cd cd - 创建 ...

  2. linux中tar命令(打包、压缩、解压)、zip和unzip、rar多种压缩文件

    一.名词解释 打包:将一大堆文件或目录变成一个总的文件[tar命令] 压缩:将一个大的文件通过一些压缩算法变成一个小文件[gzip,bzip2等] Linux中很多压缩程序只能针对一个文件进行压缩,这 ...

  3. Linux中gz文件操作遇到的一些技巧和坑

    目录 不解压情况下获取gz超大文件的前/后几行? Perl读入gz文件操作? 不能直接通过wc -l 来统计gz文件的行数 前提是gz文件超大,如上百G,肯定不能直接解压来做. 不解压情况下获取gz超 ...

  4. shell 命令 文件(解)压缩 tar,zip, gzip,bzip2

    1.gzip / gunzip [ gzip data.c]  对文件进行压缩,生成 data.c.gz    同时删除了原文件    同时压缩两个文件     [gunzip  data.c.gz  ...

  5. Linux下文件的打包、解压缩指令——tar,gzip,bzip2,unzip,rar

    本文是笔者对鸟叔的Linux私房菜(基础学习篇) 第三版(中文网站)中关于 Linux 环境下打包和解压缩指令的内容以及日常操作过程中所接触的相关指令的总结和记录,以供备忘和分享.更多详细信息可直接参 ...

  6. Linux下文件的打包、解压缩指令——tar,gzip,bzip2

    本文是对 鸟叔的Linux私房菜(基础学习篇) 第三版 的学习笔记,原文可参考原书中文网站 鸟叔的Linux私房菜.更多详细信息可直接参考对应Linux命令的 man 帮助( 如 man tar). ...

  7. vi、wc、gzip、bzip2、tar、yum安装、dpek、用户信息操作等命令

    命令模式 输入"dd"即可将这一行删除 按下"p"即可粘贴 插入模式: a:从光标这个位置之后插入 A:在行尾插入 i:从光标之前插入 I:行首插入 o:在光标 ...

  8. 【.NET深呼吸】Zip文件操作(1):创建和读取zip文档

    .net的IO操作支持对zip文件的创建.读写和更新.使用起来也比较简单,.net的一向作风,东西都准备好了,至于如何使用,请看着办. 要对zip文件进行操作,主要用到以下三个类: 1.ZipFile ...

  9. 每天一个linux命令(文件上传下载文件操作):【转载】gzip命令

    减少文件大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间.gzip是在Linux系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用.gzip不仅可以用 ...

  10. ubuntu下zip文件操作

    转自 https://blog.csdn.net/hpu11/article/details/71524013 .zip $ zip -r myfile.zip ./* 将当前目录下的所有文件和文件夹 ...

随机推荐

  1. java生成企业公章图片源代码

    企业公章图片在电子签章业务中应用广泛,在电子签章应用过程中首先需要生成公章图片,然后再使用公章图片结合数字签名技术完成电子签,这样就实现了从可视化到不可篡改的数字化电子签章功能,以下是企业公章图片生成 ...

  2. ubuntu frame 个人开发心得

    引言 有一次我在树莓派上安装 Ubuntu Core 我给创新创业项目开发一个可视化 gui 看板,用于展示数据. 然后我就找到了我需要的工具 Ubuntu Frame 初次尝试使用 Ubuntu F ...

  3. windows 和 Linux 下 git status 结果不一致

    解决该问题 运行一下命令即可 git config core.autocrlf true 解释 git config core.autocrlf true 这个命令是在任何支持的操作系统上都可以运行的 ...

  4. CUDA C编程权威指南:1.1-CUDA基础知识点梳理

      主要整理了N多年前(2013年)学习CUDA的时候开始总结的知识点,好长时间不写CUDA代码了,现在LLM推理需要重新学习CUDA编程,看来出来混迟早要还的. 1.CUDA 解析:2007年,NV ...

  5. Navicat 携手华为云GaussDB,联合打造便捷高效的数据库开发和建模工具方案

    本文分享自华为云社区<Navicat 携手华为云GaussDB,联合打造便捷高效的数据库开发和建模工具方案>,作者: GaussDB 数据库 . 近日, Navicat Premium顺利 ...

  6. 玩转LiteOS组件:Openexif

    摘要:OpenExif是用于访问Exif格式的JPEG图像文件的面向对象的库. 本文分享自华为云社区<LiteOS组件尝鲜-玩转Openexif>,作者: W922 . 基本信息 Exif ...

  7. Axure 弹出框遮罩层灯箱效果

    灯箱效果:比较适合Web, 效果不太理想,还是自己做个比较合适 弹出效果:就是弹框 推动效果:A 元件显示的时候,把 B元件往下.右移动.如Web左侧菜二级单效果

  8. Hyper-V CentOS Linux 硬盘扩容

    同一物理盘进行扩容(非挂载),首先在虚机内将硬盘空间扩大, Hyper-V 需要将检查点删除 查看物理卷和卷组,并将物理卷加入到卷组 #将剩余空间添加到逻辑卷 /dev/centos/root lve ...

  9. 编码器-解码器 | 基于 Transformers 的编码器-解码器模型

    基于 transformer 的编码器-解码器模型是 表征学习 和 模型架构 这两个领域多年研究成果的结晶.本文简要介绍了神经编码器-解码器模型的历史,更多背景知识,建议读者阅读由 Sebastion ...

  10. SpringMVC — 数据响应 / 获取请求参数

    SpringMVC的数据响应方式 页面跳转 直接返回字符串 通过ModelAndView对象返回 回写数据 直接返回字符串 返回对象或集合 页面跳转 方式一.返回带有前缀的字符串: 转发:forwar ...