File Compression and Archiving in linux (linux 中文件的归档)
1. Compressing Files at the Shell Prompt
Red Hat Enterprise Linux provides the bzip2, gzip, and zip tools for compression from a shell prompt. The bzip2 compression tool is recommended because it provides the most compression and is found on most UNIX-like operating systems. The gzip compression tool can also be found on most UNIX-like operating systems. To transfer files between Linux and other operating system such as MS Windows, use zip because it is more compatible with the compression utilities available for Windows.
| Compression Tool | File Extension | Decompression Tool |
|---|---|---|
| bzip2 | .bz2 | bunzip2 |
| gzip | .gz | gunzip |
| zip | .zip | unzip |
Table 1-1. Compression Tools
By convention, files compressed with bzip2 are given the extension .bz2, files compressed with gzip are given the extension .gz, and files compressed with zip are given the extension .zip.
Files compressed with bzip2 are uncompressed with bunzip2, files compressed with gzip are uncompressed with gunzip, and files compressed with zip are uncompressed with unzip.
1.1. Bzip2 and Bunzip2
To use bzip2 to compress a file, enter the following command at a shell prompt:
bzip2 filename |
The file is compressed and saved as filename.bz2.
To expand the compressed file, enter the following command:
bunzip2 filename.bz2(bzcat 查看内容) |
The filename.bz2 compressed file is deleted and replaced with filename.
You can use bzip2 to compress multiple files and directories at the same time by listing them with a space between each one:
bzip2 filename.bz2 file1 file2 file3 /usr/work/school (亲测bzip2不能压缩文件夹) |
The above command compresses file1, file2, file3, and the contents of the /usr/work/school/ directory (assuming this directory exists) and places them in a file named filename.bz2.
![]() |
Tip |
|---|---|
|
For more information, enter man bzip2 and man bunzip2 at a shell prompt to read the man pages for bzip2 and bunzip2. |
1.2. Gzip and Gunzip
To use gzip to compress a file, enter the following command at a shell prompt:
gzip filename (只能用zcat查看内容) |
The file is compressed and saved as filename.gz.
To expand the compressed file, enter the following command:
gunzip filename.gz |
The filename.gz compressed file is deleted and replaced with filename.
You can use gzip to compress multiple files and directories at the same time by listing them with a space between each one:
gzip -r filename.gz file1 file2 file3 /usr/work/school (不能将前面的三个文件放到一个文件夹中,-r 参数是归档目录时必须要带的参数,gzip好像没有将多个文档归到一个文件中的功能,gbip2也没有,但是zip是有的) |
The above command compresses file1, file2, file3, and the contents of the /usr/work/school/ directory (assuming this directory exists) and places them in a file named filename.gz.(i tryed but it seems not work like that 'places them in a file named filename.gz' , just needed while the directores included)
![]() |
Tip |
|---|---|
|
For more information, enter man gzip and man gunzip at a shell prompt to read the man pages for gzip and gunzip. |
1.3. Zip and Unzip
To compress a file with zip, enter the following command:
zip -r filename.zip filesdir |
In this example, filename.zip represents the file you are creating and filesdir represents the directory you want to put in the new zip file. The -r option specifies that you want to include all files contained in the filesdir directory recursively.
To extract the contents of a zip file, enter the following command:
unzip filename.zip |
You can use zip to compress multiple files and directories at the same time by listing them with a space between each one:
zip -r filename.zip file1 file2 file3 /usr/work/school |
The above command compresses file1, file2, file3, and the contents of the /usr/work/school/ directory (assuming this directory exists) and places them in a file named filename.zip.
![]() |
Tip |
|---|---|
|
For more information, enter man zip and man unzip at a shell prompt to read the man pages for zip and unzip. |
2. Archiving Files at the Shell Prompt
A tar file is a collection of several files and/or directories in one file. This is a good way to create backups and archives.
Some of tar's options include:
-c — create a new archive
-f — when used with the -c option, use the filename specified for the creation of the tar file; when used with the -x option, unarchive the specified file
-t — show the list of files in the tar file
-v — show the progress of the files being archived
-x — extract files from an archive
-z — compress the tar file with gzip
-j — compress the tar file with bzip2
To create a tar file, enter:
tar -cvf filename.tar directory/file |
In this example, filename.tar represents the file you are creating and directory/file represents the directory and file you want to put in the archived file.
You can tar multiple files and directories at the same time by listing them with a space between each one:
tar -cvf filename.tar /home/mine/work /home/mine/school |
The above command places all the files in the work and the school subdirectories of /home/mine in a new file called filename.tar in the current directory.
To list the contents of a tar file, enter:
tar -tvf filename.tar |
To extract the contents of a tar file, enter:
tar -xvf filename.tar |
This command does not remove the tar file, but it places copies of its unarchived contents in the current working directory, preserving any directory structure that the archive file used. For example, if the tarfile contains a file called bar.txt within a directory called foo/, then extracting the archive file results in the creation of the directory foo/ in your current working directory with the file bar.txt inside of it.
Remember, the tar command does not compress the files by default. To create a tarred and bzipped compressed file, use the -j option:
tar -cjvf filename.tbz file |
tar files compressed with bzip2 are conventionally given the extension .tbz; however, sometimes users archive their files using the tar.bz2 extension.
The above command creates an archive file and then compresses it as the file filename.tbz. If you uncompress the filename.tbz file with the bunzip2 command, the filename.tbz file is removed and replaced with filename.tar.
You can also expand and unarchive a bzip tar file in one command:
tar -xjvf filename.tbz |
To create a tarred and gzipped compressed file, use the -z option:
tar -czvf filename.tgz file |
tar files compressed with gzip are conventionally given the extension .tgz.
This command creates the archive file filename.tar and compresses it as the file filename.tgz. (The file filename.tar is not saved.) If you uncompress the filename.tgz file with the gunzip command, the filename.tgz file is removed and replaced with filename.tar.
You can expand a gzip tar file in one command:
tar -xzvf filename.tgz |
![]() |
Tip |
|---|---|
|
Enter the command man tar for more information about the tar command. |
File Compression and Archiving in linux (linux 中文件的归档)的更多相关文章
- Linux系统中文件定位与查找
Linux系统中文件查找 关键词 文件查找 | find | locate 本文主要介绍有关文件查找的两个命令——find和locate,以及压缩打包的命令——compress, gzip,bzip2 ...
- linux系统中文件的几种类型
Linux系统是以文件的形式来进行管理的.Linux文件类型常见的有:普通文件.目录.字符设备文件.块设备文件.符号链接文件等,如果想了解这方面知识的弟兄,就进来了解了解. Linux系统不同于win ...
- Xshell6远程访问linux及Xftp6远程针对linux系统中文件操作(附图文详解)
1.首先我们需要先做好前期准备工作,需要到XManager6官网上将Xshell及Xftp下载并安装,安装过程一直下一步就好了.这里是其官网:http://www.xshellcn.com/.安装完成 ...
- linux shell 中文件编码查看及转换方法
参考: http://edyfox.codecarver.org/html/vim_fileencodings_detection.html 一.查看文件编码. 在打开文件的时候输入:set ...
- Linux入门培训教程 linux系统中文件I/O教程
linux 文件I/O教程 一,文件描述符 对内核而言,所以打开的文件都通过文件描述符引用.每个进程都有一些与之关联的文件描述符.文件描述符是一个非负整数.当打开一个现有文件或创建一个新文件时,内核向 ...
- linux系统中文件的权限
查看文件权限的语句: 在终端输入:ls -l xxx.xxx (xxx.xxx是文件名) 那么就会出现相类似的信息,主要都是这些:-rw-rw-r-- 一共有10位数 其中: 最前面那个 - 代表的是 ...
- Linux系统中文件行末尾出现^M的原因及解决办法
不同系统,有不同的换行符号: 在windows下的文本文件的每一行结尾,都有一个回车('\n')和换行('\r') 在linux下的文本文件的每一行结尾,只有一个回车('\n'); 在Mac下的文本文 ...
- Linux 文件系统类型 文件系统结构 与Windows文件系统的比较
摘自:http://blog.csdn.net/gelivable007/article/details/7249365 Linux 文件系统类型 磁盘文件系统.包括硬盘.CD-ROM.DVD.USB ...
- Linux 内核的文件 Cache 管理机制介绍
Linux 内核的文件 Cache 管理机制介绍 http://www.ibm.com/developerworks/cn/linux/l-cache/ 1 前言 自从诞生以来,Linux 就被不断完 ...
随机推荐
- NET Core CSharp初级篇 1-3面向对象
.NET Core CSharp初级篇 1-3 本节内容为面向对象初级教程 类 简介 面向对象是整个C#中最核心最有特色的一个模块了,它很好的诠释了程序与现实世界的联系. 面向对象的三大特征:继承.多 ...
- 第一章 Servlet
JavaEEe技术包括Servlet/Jsp.Java Message Service(JMS).Enterprise JavaBeans(EJB).JavaServer Faces(JSF),以及J ...
- springboot简单入门笔记
一.Spring Boot 入门 1.Spring Boot 简介 简化Spring应用开发的一个框架: 整个Spring技术栈的一个大整合: J2EE开发的一站式解决方案: 2.微服务 2014,m ...
- 常用的re模块的正则匹配的表达式
07.01自我总结 常用的re模块的正则匹配的表达式 一.校验数字的表达式 1.数字 ^[0-9]\*$ 2.n位的数字 ^\d{n}$ 3.至少n位的数字 ^\d{n,}$ 4.m-n位的数字 ^\ ...
- 为何出现了trx_mysql_thread_id为0 的事务是什么
今天巡检时突然发现有很多锁等待超时的情况,原以为是一个简单的小事,一查,结果令人深思. 1. 问题现象 发现日志中出现了大量的 ERROR 1205 (HY000): Lock wait timeo ...
- python基础知识练习题一
1.执行Python脚本的两种方式 1.在计算机终端(运行cmd),输入安装的Python路径,然后输入Python脚本的路径,回车. 2.直接运行python 2.简述位.字节的关系. 1字节 = ...
- 云计算:Linux运维核心管理命令详解
云计算:Linux运维核心管理命令详解 想做好运维工作,人先要学会勤快: 居安而思危,勤记而补拙,方可不断提高: 别人资料不论你用着再如何爽那也是别人的: 自己总结东西是你自身特有的一种思想与理念的展 ...
- sklearn学习 第一篇:knn分类
K临近分类是一种监督式的分类方法,首先根据已标记的数据对模型进行训练,然后根据模型对新的数据点进行预测,预测新数据点的标签(label),也就是该数据所属的分类. 一,kNN算法的逻辑 kNN算法的核 ...
- Linux 下实践 VxLAN
本文首发于我的公众号 Linux云计算网络(id: cloud_dev),专注于干货分享,号内有 10T 书籍和视频资源,后台回复 「1024」 即可领取,欢迎大家关注,二维码文末可以扫. 来源:ht ...
- python3 导入包总提示no moudle named xxx
一.python中的包有三种 1.python自带的包,如sys, os 2.python的第三方库,如 requests, selenium 3.自己写的.py文件 二.今天主要说下导入自己写的包 ...
