tar command file packaging and compression

1. Concept

Packaging refers to packaging multiple files or directories into one file, and compression refers to compressing a large file into a small file through an algorithm. Since many compression programs in Linux can only perform one file, it is usually necessary to package all files into one file first, and then compress that packaged file.

2. Introduction to tar command

The tar command can be used to perform operations such as packaging and unpacking, compression and decompression, and the package produced by using the tar command is called a tar package. For example, you can use the tar command to package multiple files or directories into a tar package, and you can also unpack that tar package.

2.1 Packaging operation: tar <option> <source file or directory>

Command parameters:

  • -c pack multiple files or directories
  • -f <package name> Package to the specified file, -f should be followed by the complete file name (including extension)
  • -v print out the packaging process
  • -u Add new files in the tar package (files cannot be added directly to the compressed package)
2.1.1 Command example:

1. tar -cvf package.tar test1 test2
packs test1 and test2 into a package.tar file and displays the packaging process
2. tar -cf package.tar test1 Desktop/
packs the file test1 and the directory Desktop into a package.tar file
3. tar -uf package.tar test3
adds test3 to the package.tar file

2.2 Unpacking operation: tar <options> <compressed package>

  • -x unpack operation
  • -f <package name> Unpack to the specified file, f should be followed by the complete file name
  • -C<directory> specifies the directory as the unpacking location
  • -t view the files in the package
2.2.1 Command example:

1. tar -xf package.tar
unpacks the package.tar file to the current directory
2. tar -xf test.tar -C ...
unpacks test.tar to the upper level directory
3. tar -tf package.tar
to view the package Files in the .tar package

3. Simultaneously (de)package and (de)compress (most commonly used)

Compression under Linux needs to pack the file first and then compress it, while decompression is to decompress the compressed file into a package first, and then unpack the package. That is to say, (de)compression requires two steps, and the tar command can perform two steps at the same time through one line of commands. The two most common compression techniques under Linux are gzip and bzip2, so it is generally enough to know how to (de)compress these two compression formats.

Command format: tar <option> <compressed package> <source file or directory> . Just add z or j to the pack or unpack options. The z option uses the gzip compression program, and the j option uses the bzip2 compression program.

  • -z compress to ".tar.gz" format, or decompress the .gz compressed package
  • -j compress to ".tar.bz2" format, or decompress the .bz2 archive

Command example:
1. tar -czf test.tar.gz test1 test2
pack test1 and 2 into a tar package, and then use the gzip compression program to compress the tar package to generate a compressed package named test.tar.gz. It can also be directly said that test1 and 2 are packaged and compressed into a test.tar.gz file
2, tar -xzf test.tar.gz -C Desktop/
Use the gzip program to decompress the test.tar.gz file into a tar package, and then Unpack the tarball into the Desktop directory. It can also be directly said to decompress test.tar.gz to the Desktop directory
3. tar -jcvf test.tar.bz2 test1 test2
pack and compress test1 and 2 into a test.tar.bz2 file through the bzip2 compression program, and print out the process

4. Supplementary concepts:

1. The file name under Linux has no effect, but programmers usually indicate the type of file with an appropriate extension. For example, .sh means script or batch file; .tar, .tar.gz, .zip, etc. mean files compressed by different compression software;

2. Since the extension has no effect, why does the file after -f of the tar command need to add a file extension? Because Linux supports many compression commands, and the compression techniques used by different commands are not the same, it may not be possible to compress or decompress files with each other. Therefore, adding an extension when packaging a compressed file is to let us (programmers) know which compression command is used for the file, and can be decompressed against it. For example, decompressing a .tar.gz file requires the -z command (the gzip program), and a .tar.bz2 file requires the -j command (the bzip2 program).

Guess you like

Origin blog.csdn.net/weixin_42039228/article/details/131117674