File Compression and archiving in Linux (archive of files in Linux)

Source: Internet
Author: User
Tags bz2 uncompress

1. Compressing Files at the Shell Prompt

Red Hat Enterprise Linux provides the bzip2, gzip, and zip tools for compression from a shell p Rompt. The bzip2 compression tool is recommended because it provides the most compression and are found on most unix-like Operating systems. The gzip compression tool can also is found on most unix-like operating systems. To transfer files between Linux and other operating system such as MS Windows, use zip because it's more Compati BLE 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 is given the extension . bz2, files compressed with gzip< /c2> is given the extension gz, and files compressed with Zip is given the extension . zip.

Files compressed with bzip2 is uncompressed with bunzip2, files compressed with gzip is uncom Pressed with gunzip, and files compressed with Zip is uncompressed with unzip.

1.1. Bzip2 and BUNZIP2

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 view content)

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 betwee N each one:

bzip2 filename.bz2 file1 file2 file3/usr/work/school (pro-Test bzip2 cannot compress folders)

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 mans pages for BZ IP2 and bunzip2.

1.2. Gzip and Gunzip

To use gzip -Compress a file, enter the following command at a shell prompt:

gzip filename (view content with Zcat only)

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:

(The previous three files cannot be placed in a folder, the-r parameter is the parameter that must be taken when the directory is archived, Gzip does not seem to have the ability to classify multiple documents into a file, GBIP2 does not, but zip is there)

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 seem s not work like that ' places them in a file named filename.gz', just needed while the directores included)

Tip

For more information, enter Mans 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 is creating and Filesdir represents the directory You want-put in the new zip file. The- r option specifies this want to include all files contained in the filesdir directory recur Sively.

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 mans 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 the 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 is creating and Directory/file represents the Dir Ectory and file you want-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 it 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 Extracti NG 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 is 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 Remo Ved 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 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 R Emoved 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 (archive of files in Linux)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.