Linux zip command __linux

Source: Internet
Author: User
Tags bz2 gz file
From:http://hi.baidu.com/jorling/blog/item/13db7bec1c38a1d32e2e21d3.html

File compression and archiving

Sometimes we need to store a set of files in a single file for backup or transfer to another directory or even another computer. Sometimes we also need to compress files into one file, so they use only a small amount of disk space and can be downloaded faster over the Internet.

Understanding the differences between archive files (archive file) and compressed files (compressed file) is important to users. An archive is a collection of files and directories that are stored in a file. The archive is not compressed-the disk space it uses is the sum of all the files and directories in it. A compressed file is also a collection of files and directories, and the collection is stored in a file, but it is stored so that it consumes less disk space than the sum of all the files and directories in it. If you don't have enough disk space on your computer, you can compress files that you don't use frequently, or that you no longer use but want to keep. You can even create an archive and then compress it to save disk space.

Notes

The archive file is not a compressed file, but a compressed file can be an archive file.

using the File Packager

Red Hat Linux includes a graphical compression tool, "File Packager." It can compress, decompress, and archive files and directories. The file Packager supports common UNIX and Linux file compression and archive formats, and it has a simple interface and rich documentation. It is also integrated into the desktop environment and the graphical file manager, making it easier to work with the archive.

To start the File Packager, click on the main Menu "=>" attachment "=>" File Packager. You can also start the File packager by typing File-roller from the shell prompt. Figure 14-1 shows the running File Packager.

Trick

If you are using a File manager (such as Nautilus), you can start the File packager by double-clicking the file you want to release or extract. The Browse window for the File packager appears with folders that show you the files you want to extract or release from the archive so you can extract or browse.

Figure 14-1. The running File Packager uses the file packager to decompress or release the archive

To cancel the archive or (and) Extract the file, click on the "Open" button on the toolbar. A File menu pops up, allowing you to select the archive you want to manipulate. For example, if you have a foo.tar.gz file in your home directory, highlight the file, and then click OK. The file appears as a folder in the main browsing window of the file packager, and you can double-click the folder icon to flip through the contents. The file packager retains all the original directory and subdirectory structures, which makes it easy for you to find a particular file in the archive. You can click on the "Extract" button, select the directory where you want to save the files that are released from the archive, and click the "OK" button to extract one of the files or the entire archive. Use the File Packager to create an archive

If you need to free up some hard disk space, or send multiple files or all the files in a directory to another user, the file packager allows you to create files and directories. To create a new archive, click the New button on the toolbar. A file browser pops up, allowing you to specify the archive name and compression technology. For example, you can select the tar.gz in gzip format from the Drop-down menu and type the name of the archive you want to create. By clicking OK, you can add files and directories to the new archive. To add a file to your new archive, click "Add", which will pop up a browse window ( figure 14-2), where you can find files or directories to put in the archive. When you are finished, click OK and close to close the archive.

Figure 14-2. Use the File Packager to create an archive

Trick

The tasks that the file packager can perform are more than just those mentioned here. Please read the Manual of the File Packager (click on "Help"=>"") for more information.

compressing files at the shell prompt

Compressed files use less disk space and are faster to download than uncompressed large files. The file compression tools you can use in Red Hat Linux are: gzip, bzip2, and zip.

It is recommended that you use the BZIP2 compression tool because it provides maximum compression and can be found on most UNIX-like operating systems. The GZIP compression tool can also be found on UNIX-like operating systems. If you need to transfer files between Linux and other operating systems such as MS windows, you should use zip because the command is most compatible with the compression tools on Windows.

Compression Tools file name extension Decompression Tools
Gzip . gz Gunzip
Bzip2 . bz2 Bunzip2
Zip . zip Unzip

Table 14-1. Compression tools

By conventions, files that are compressed with gzip extensions are. gz; files that are compressed with bzip2 are. bz2, and zip-compressed files with the extension. zip.

Files that are compressed with gzip can be decompressed using gunzip, bzip2 compressed files can be extracted using BUNZIP2, and zip-compressed files can be decompressed using unzip. bzip2 and Bunzip2

To compress the file using BZIP2, type the following command at the shell prompt:

bzip2 filename

The file is compressed and saved as a filename.bz2.

To extend a compressed file, type the following command:

BUNZIP2 filename.bz2

The filename.bz2 file is deleted, followed by filename.

You can use the BZIP2 command to work with multiple files and directories at the same time by listing them individually and spacing by space:

The above command compresses the contents of the File1, File2, File3, and/usr/work/school directories (assuming this directory exists) and puts them into the filename.bz2 file.

Trick

To get more information about the two commands, type man bzip2 and man bunzip2 at the shell prompt to read the instructions page for the bzip2 and BUNZIP2 commands.

gzip and Gunzip

To compress the file using gzip, type the following command at the shell prompt:

gzip filename

The file is compressed and saved as a filename.gz.

To extend a compressed file, type the following command:

Gunzip filename.gz

Filename.gz will be deleted, followed by filename.

You can use the gzip command to work with multiple files and directories at the same time by listing them individually and spacing by space:

The above command compresses the contents of the File1, File2, File3, and/usr/work/school directories (assuming this directory exists) and puts them into the filename.gz file.

Trick

For more information about the two commands, type man gzip and man gunzip to read the instructions page for the gzip and Gunzip commands at the shell prompt.

Zip and Unzip

To compress the file using Zip, type the following command at the shell prompt:

Zip-r Filename.zip Filesdir

In this example, Filename.zip represents the file you created, and Filesdir represents the directory where you want to place the new zip file. The-r option specifies that you want to include all files included in the Filesdir directory recursively (recursively).

To extract the contents of a zip file, type the following command:

Unzip Filename.zip

You can use the zip command to work with multiple files and directories at the same time by listing them individually and spacing by space:

The above command compresses the contents of the File1, File2, File3, and/usr/work/school directories (assuming this directory exists) and puts them into the Filename.zip file.

Trick

For more information about the two commands, type man zip and man unzip at the shell prompt to read the instructions page for the zip and unzip commands.

file files with Shell hints

The tar file is a collection of several files and/or directories in one file. This is the best path to create backup and archive.

The options used in tar are:

-c-Create a new archive.

-f-when used with the-C option, the created tar file uses the filename specified by this option, and the archive specified by this option when used with the-X option.

-t-displays a list of files included in the tar file.

-v-Displays the archive progress of the file.

-x-extracts files from the archive.

-z-uses gzip to compress the tar file.

-j-uses bzip2 to compress the tar file.

To create a tar file, type:

TAR-CVF Filename.tar Directory/file

In the above example, Filename.tar represents the file you created, Directory/file represents the files and directories you want to put in the archive.

You can use the tar command to work with multiple files and directories at the same time by listing them individually and spacing by space:

TAR-CVF Filename.tar/home/mine/work/home/mine/school

The above command puts all the files in the work and school subdirectories of the/home/mine directory into a new file called Filename.tar in the current directory.

To list the contents of the tar file, type:

TAR-TVF Filename.tar

To extract the contents of the tar file, type:

TAR-XVF Filename.tar

This command does not delete the tar file, but it copies the archived content to the current working directory and retains any directory structure used by the archive. For example, if the tar file contains a file called Bar.txt, and the file is contained in the foo/directory, extracting the archive will cause the foo/directory to be created in your current working directory that contains bar.txt files.

Keep in mind that tar defaults to not compress files. To create a file that uses tar and bzip to archive compression, use the-J option:

TAR-CJVF filename.tbz File

By conventions, the tar file with bzip2 compression has a. tbz extension. However, sometimes users use the tar.bz2 extension to archive their files.

The above command creates an archive file and then compresses it into a filename.tbz file. If you extract the Filename.tbz file using the BUNZIP2 command, the filename.tbz file is deleted, followed by the Filename.tar file.

You can also use a command to extend and deallocate bzip tar files:

TAR-XJVF FILENAME.TBZ

To create a file that is archived and compressed with tar and gzip, use the-Z option:

TAR-CZVF filename.tgz File

By conventions, the tar file that is compressed using gzip has a. tgz extension.

This command creates the archive file Filename.tar, and then compresses it into a filename.tgz file (the file Filename.tar is not retained). If you use the Gunzip command to extract the Filename.tgz file, the filename.tgz file is deleted and replaced with Filename.tar.

You can extend the gzip tar file with a single command:

TAR-XZVF filename.tgz

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.