File Compression and Archiving in linux (linux 中文件的归档)
Posted 何帅
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 中文件的归档)的主要内容,如果未能解决你的问题,请参考以下文章
cf1523C. Compression and Expansion
CF1523C. Compression and Expansion(堆栈模拟)
DeltixRound,Div. 1+2-C.Compression and Expansion-构造小模拟
论文记录-Deep Compression:Compressing DeepNeural Networks With Pruning, Trained Quantization And Huffman
论文记录-Deep Compression:Compressing DeepNeural Networks With Pruning, Trained Quantization And Huffman