Basic Linux Tutorial: Package and compress files

Source: Internet
Author: User

Basic Linux Tutorial: Package and compress files

I. Notes

  • The number of files is too large. Is it difficult to copy the files? What should I do? Pack!
  • If the file is too large, will it take a long time to download and transmit it over the network? What should I do? Compression!

In Linux, the extension of the compressed files to be packaged is mostly :【*. tar, * .tar.gz ,*. gz ,*. bz2 ,*. tar. xz] these files are collectively referred to as "tar" packages. Why is there such an extension? Didn't the Linux Extension be useful? [Different suffixes indicate that the compression algorithms used are different. The sort of the compression ratio from low to high is: gz <bz2 <xz; the compression speed is the opposite: gz> bz2> xz]

Yes. The file extension is not available for Linux. However, Linux supports many compression commands, and different commands use different compression technologies. Of course, they may not be interconnected (compressed or decompressed ). Therefore, when we download a compressed file, we naturally need to know which compression command is used to decompress the file.

That is to say, although the Linux File Extension has no absolute relationship with its attributes, the appropriate extension is necessary to facilitate identification.

Packaging: in essence, many files and directories are packaged into one file.
Compression: convert a large file or directory into a small file by using the compression algorithm.

Most commands can only compress a single file. gzip and bzip2 can also compress the directory, but the result is: compress all files in the directory separately and separately.

I. File packaging and Compression
Tar: Pack and compress a command. The tar command of a slightly higher version is optional.
It is very easy to extract the "tar" package, just like this:

12 $ tar xf filename
$ Tar xf filename-C/tmp # decompress it to the/tmp directory

Note: filename is the file name of the "tar" package to be decompressed. In the latter form, option f must be placed at the end.
Create a tar package
1 $ tar-cvf filename.tar FILES

Specify the compression algorithm: z indicates gz, j indicates bz2, and J indicates xz.
123 $ tar-zcvf filename.tar.gz FILES
$ Tar-jcvf filename.tar.bz2 FILES
$ Tar-Jcvf filename.tar. xz FILES

Note: FILES indicates the name of the file or directory compressed by the packing box.

How to unlock a single file only
1234 ## first-view the file name to be decompressed in jtvf
Tar-jtvf filename.tar.bz2 # query the file name to be decompressed. Assume It is etc/abc.
# Decompress the package and specify the etc/abc file name
Tar-jxvf filename.tar.bz2 etc/abc

Remember: Remember tar xvf and replace the x position with c, and t.

  • Compression (option: c): name of the file or directory to be compressed by tar-jcvf filename,tar.bz2 (multiple files can be compressed)
  • Query (option: t): tar-jtvf filename,tar.bz2
  • Decompress (option: x): tar-jxvf filename.tar.bz2-C directory to be decompressed

Because the-f parameter must be followed by the "name of the file to be created ".
By default, tar removes the/before the absolute path. In this way, if you do not use-C/to specify the root directory as its relative directory during restoration, tar will put the directory structure after unpacking in the current directory. If you need to decompress Or Compress with an absolute path, use the-P option.

Gzip & bzip2 command
Common gzip options:
-C: Output compressed data to stdout, which can be processed through redirection.

-D: Extract

-V: Details

-#: Compression ratio: 1-9

Bzip is used in the same way as gzip. -K option to retain the original file
Summary:

  • Gzip and bzip2 do not retain the original file, but generate a new compressed file.
  • Only one file can be compressed

Zip & unzip command:
# Zip all.zip *. jpg. The command is to compress all .jpg files into a zip package.
# Unzip all.zip # decompress

This article permanently updates the link address:

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.