Use of packaging commands in Linux

Source: Internet
Author: User
Tags unpack uncompress
Source: After some research and arrangement on the use of Unix systems (especially data management and backup, make full use of the commands tar, cpio, and compress of the Unix system to pack and compress them, so that they can act as compression software similar to DOS, and are also universal in Unix systems. Source: yarqutech

I have made full use of the tar, cpio, and compress commands of the Unix system in the Unix system (especially data management and backup) after some research and arrangement, it serves as a compression software similar to DOS, and is also universal in Unix systems.

In Unix systems, many files are first packaged into a file library through cpio or tar, and then compressed by compress. The following describes the methods and steps of using cpio and tar respectively.

I. cpio

1. Package files or directories.

Packaging can be performed in the following situations:

A) package subdirectories:

Find/usr/lib-print | cpio-o>/u0/temp1.cpio

Package the files and subdirectories in the/usr/lib directory into a file library/u0/temp1.cpio.

If you use the-o option to package all files and subdirectories in the entire directory, you must first use the find directory name-print to find the names of all files and subdirectories, the package is sent to cpio through the pipeline "|.

B) package without subdirectories:

Ls/usr/lib | cpio-o>/u0/temp1.cpio

Package files in the/usr/lib Directory (excluding files in subdirectories) into a file library/u0/temp1.cpio.

C) packaging specific files:

You can use the text search command grep in combination with the pipeline to exclude or select a specific type of files and send them to cpio for packaging. For example: ls/usr/lib/*. c | cpio-o>/u0/temp1.cpio

Or ls/usr/lib | grep '. c $' | cpio-o>/u0/temp1.cpio

All files ending with. c in the/usr/lib directory are found and packaged.

Another example is ls/usr/lib | grep abcd | cpio-o>/u0/temp1.cpio, which means to find the files with abcd characters in the file name in the/usr/lib directory and package them.

Ls/usr/lib | grep-v abcd | cpio-o>/u0/temp1.cpio, which indicates to find the files in the/usr/lib directory with no abcd characters in the file name and pack them. The-v option in the grep command is used to exclude the columns containing strings.

In this way, you can take full advantage of the powerful functions of Unix pipelines and some commands to package files.

2. compression:

After the file is packaged, you can use the compress command in Unix (under/usr/bin) to compress the package file. For common text files, the compression ratio is high, up to 81%.

Compress/u0/temp1.cpio compresses the file library/u0/temp1.cpio to/u0/temp1.cpio. Z (automatically add. Z and delete/u0/temp1.cpio ).

3. decompress:

Uncompress/u0/temp1.cpio. Z is automatically restored to/u0/temp1.cpio.

4. unpack and expand:

Unpack the package from the original directory structure to the current directory. If it is packaged in a relative path, when the package is unwrapped, it also stores the expanded file data in a relative path. if it is packaged in an absolute path, when the package is unwrapped, it also stores the expanded file data in an absolute path. Therefore, if it is a relative path, you should first enter the corresponding directory before expanding.

Cd/u1

Cpio-id </u0/tmp1.cpio decompress/u0/temp1.cpio to/u1 (Here we assume temp1.cpio is compressed in relative paths ).

If the u option is added, for example, cpio-iud </u0/temp1.cpio, the file will be overwritten if it exists.

Cpio-id </u0/temp1.cpio *. c. expand the *. c file, which is similar to the Pkunzip-d temp1.zip unpackage function in the Pkzip software in the DOS system.

5. display:

Cpio-it </u0/temp1.cpio [*. c] displays the file name in the file library, similar to the Pkunzip-vbnm temp1.zip function in the Pkzip software in the DOS system.

II. tar

1. Package files or directories:

Tar-cvf/u0/temp2.tar/usr/lib

Package the files and subdirectories in the/usr/lib directory into a file library/u0/temp2.tar.

Tar-cvf/u0/temp2.tar/usr/lib *. c *. f

Pack *. c *. f and other files (excluding subdirectories) in the/usr/lib directory.

Note: if the specified file is *. c *. f or *. *, the package does not contain subdirectories. If it is specified as. or *, it contains sub-directories.

2. compression:

Same as above: compress/u0/temp2.tar compressed to/u0/temp2.tar. Z

3. decompress:

Uncompress/u0/temp2.tar. Z is restored to/u0/temp2.tar.

4. unpack and expand:

Tar-xvf/u0/temp2.tar

If the package is packaged in a relative path, the expanded file data is stored in the relative path when the package is unwrapped. if the package is packaged in an absolute path, the expanded file data is stored in the absolute path when the package is unwrapped.

If you specify the expanded file name, pay attention to the directory path of the file.

5. display:

Tar-tvf/u0/temp2.tar display the file name in the file library. When specifying a file name, you must also note the file path.

These two commands have their own advantages and disadvantages.

1) tar is slower than cpio and cannot span two storage media sets. However, the file format is common in almost all Unix systems and is easy to use.

2) cpio enables you to select and exclude files during packaging by using the MPs queue function. It also supports multiple media copies and backs up special system files.

In addition, the compression command compress has a lower compression ratio than the Pkzip in DOS. After testing, 527 text files in a directory are 15.7 MB in total. compress is used to compress the files after Unix packaging, and the size is 2.1 MB. the same files are copied to the DOS system and compressed with Pkzip, the size is 1.4 MB.
Related Article

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.