文件操作之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. Spring 中循环依赖的处理

    Spring 提供了十分强大的依赖注入功能,使得我们不再需要手动去管理对象的依赖项.然而,在实际的使用场景中,可能会遇到类似下面的依赖异常: Exception encountered during ...

  2. 如何解决windos系统关闭nginx进程之后仍然可以访问?

    1.停止Nginx服务的四种方法 从容停止服务 这种方法较stop相比就比较温和一些了,需要进程完成当前工作后再停止. nginx -s quit 立即停止服务 这种方法比较强硬,无论进程是否在工作, ...

  3. Apache Hudi在信息服务行业构建流批一体的实践

    个人介绍 李昂 高级数据研发工程师 Apache Doris & Hudi Contributor 业务背景 部门成立早期, 为了应对业务的快速增长, 数仓架构采用了最直接的Lambda架构 ...

  4. 2023-09-13:用go语言,给定一个整数数组 nums 和一个正整数 k, 找出是否有可能把这个数组分成 k 个非空子集,其总和都相等。 输入: nums = [4, 3, 2, 3, 5,

    2023-09-13:用go语言,给定一个整数数组 nums 和一个正整数 k, 找出是否有可能把这个数组分成 k 个非空子集,其总和都相等. 输入: nums = [4, 3, 2, 3, 5, 2 ...

  5. AI辅助宫颈癌筛查技术全球居首,守护者的力量来源是?

    宫颈癌,是常见的妇科恶性肿瘤.宫颈癌发病率在妇科恶性肿瘤中仅次于乳腺癌,但同时也是医学界公认的病因明确,预防有疫苗.且早期治愈率高的病症!病理形态学诊断被医学界公认为疾病诊断的"金标准&qu ...

  6. 带你了解VXLAN网络中报文的转发机制

    摘要:本节以集中式VXLAN网络(手工方式建立VXLAN隧道)为例,分别介绍相同子网内.不同子网间是如何进行通信的.在了解转发机制的前提下,我们先来看下VXLAN网关有哪些种类. VXLAN二层网关与 ...

  7. html5鼠标拖动排序及resize实现方案分析及实践

    对列表进行拖动排序,尺寸改变.之前一般会使用jQuery-UI.其通过mousedown.mousemove.mouseup这三个事件来实现页面元素被鼠标拖拽的效果.vue-drag-resize v ...

  8. 火山引擎 ByteHouse 与白鲸开源完成兼容性认证,加速数据价值释放

    更多技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群 数据作为新型生产要素,已快速融入生产.分配.流通.消费和社会服务管理等各环节,深刻改变着生产方式.生活方式和治理方 ...

  9. PPT 模仿力,看到好的设计随意为我所用

    PPT 模仿力,看到好的设计随意为我所用 网上搜索一些作品 Q1: 这一页的设计亮点在哪? Q2: 我能不能用在PPT里面? Q3: 我能不能用PPT模仿出来? 举例 思源黑体

  10. Jenkins Blue Ocean

    介绍 Blue Ocean 是 pipeline 的可视化UI.同时兼容经典的自由模式的 job.Jenkins Pipeline 从头开始设计,但仍与自由式作业兼容,Blue Ocean 减少了经典 ...