Linux tips: introduce the usage of tar command packaging, compression, encryption and decryption

Time:2021-10-12

In Linux, you can usetarThe command packs multiple files and directories into the specified archive file.

It should be noted that,tarBy default, the command only packs multiple files together and does not compress the files. Therefore, the file size after packaging will not become smaller. Due to the addition of some tar format file information, it may even become larger.

If you want totarTo perform compression or decompression in the command, you need to provide corresponding options and parameters. It will be explained later.

Tar command format

View man tar pairstarThe format of the command is described as follows:

tar [-] A –catenate –concatenate | c –create | d –diff –compare | –delete | r –append | t –list | –test-label | u –update | x –extract –get [options] [pathname …]

Tar stores and extracts files from a tape or disk archive.

A function letter need not be prefixed with “-”, and may be combined with other single-letter options. A long function name must be prefixed with –.

Some options take a parameter; with the single-letter form these must be given as separate arguments. With the long form, they may be given by appending =value to the option.

That is,tarThe command can create and extract archive files on tape, or disk.

provideAcdrtuxThese options can be preceded by no hyphen-

When other options are written together with these function options, they can also be omitted-Character. If other options are provided separately, you need to-start.

Common function options are described as follows:

  • -c, –create
    create a new archive.
    That is, specify to create a new archive instead of extracting files from the archive.
  • -r, –append
    append files to the end of an archive.
    That is, write a new file to the existing archive file.
  • -t, –list
    list the contents of an archive.
    That is, you can list the file information packed in the archived file without unpacking the archived file
  • -u, –update
    only append files newer than copy in archive
    That is, when the local file is new to the file in the archive file, pack the local file into the archive file.
    This option does not update compressed archive files.
  • -x, –extract, –get
    extract files from an archive.
    That is, extract the file from the archive

Among these function options, there are no options for compression and decompression.

Generally speaking, these function options will be used in combination with the following options:

  • -f, –file ARCHIVE
    use archive file or device ARCHIVE.
    That is,-fOption to provide a parameter that specifies the name of the archive file.
    If this option is not specified, the tar command usually writes the content to standard output.
  • -v, –verbose
    verbosely list files processed.
    That is, details of the print operation process. It is often used to view extracted file information.

Some examples are as follows:

  • Create Archive
$ tar c java/ hello.c -f archive.tar
$ ls
archive.tar    java    hello.c

staytar c java/ hello.c -f archive.tarIn the command,c java/ hello.cSpecify pairjava/cataloguehello.cPackage files.-f archive.tarSpecifies that the archive file name generated by packaging isarchive.tar

implementtarAfter the command, uselsThe command can see the generationarchive.tarFile.

  • List the file information in the archived file
$ tar tf archive.tar
java/
java/Service.java
hello.c

As you can see, the package generated just nowarchive.tarIt contains ajava/Directory, onehello.cFile, injava/There is one in the catalogService.javaFile.

  • Extract files from archive
$ tar xf archive.tar
$ tar xvf archive.tar
java/
java/Service.java
hello.c

As you can see,tar xvf archive.tarCommand fromarchive.tarExtract files from archive files locally and use-vOption lists the extracted file information.

andtar xf archive.tarThe command only extracts files locally and does not list the extracted file information.

Specify compression and decompression

becausetarBy default, the command only packs, not compresses, and the archive file size will not become smaller after packaging.

If you want to compress when packaging, you need to specify other options. If you want to decompress, you also need to specify the corresponding options.

The following options are used to specify what compression tool to use. The specified compression tool can be used for compression, or decompression.

  • -j, –bzip2
    usebzip2Command to compress or decompress
  • -z, –gzip, –gunzip –ungzip
    usegzipCommand to compress or decompress
  • -Z, –compress, –uncompress
    usecompressCommand to compress or decompress
  • -a, –auto-compress
    use archive suffix to determine the compression program.
    That is, the compression command is automatically selected according to the suffix of the archive file. For example,.gzSuffix usegzipCommand to compress.

be careful: these options are used to specify what compression tool to use for compression or decompression. These options only specify the compression tool. Whether to compress or decompress can be determined by-cOptions and-xOption.

When these options are used together with the option to package archive files, compression is performed.

When these options are used together with the option to extract archive files, decompression is performed.

The specified compression tool is a separate command. These commands need to be installed in the system before they can be used normally

Command to compress using tar

Here are some commands for compression using tar.

  • Compress using gzip
tar czf archive.tar.gz java/ hello.c

staytar czf archive.tar.gz java/ hello.cIn the command,-zOption specifies to use the gzip command.-cOption specifies to create an archive file in combination with-zUse is compression.-fOption specifies that the parameter followed by is the archive file name, that isarchive.tar.gz。 Given laterjava/ hello.cThese files will be compressed and packaged.

Generally speaking, for files compressed using gzip, the file name will be.gzending. staytarGzip is used for compression in the command, and the file name will be.tar.gzending. But this is not mandatory.

In Linux, the file name suffix does not determine the file format. It is written just for convenience.

  • Compress using bzip2
tar cjf archive.tar.bz2 java/ hello.c

In this command,-jOption specifies to use the bzip2 command.

Generally speaking, for files compressed with bzip2, the file name will be.bz2ending. staytarBzip2 is used for compression in the command, and the file name will be.tar.bz2ending.

  • Compress using compress
tar cZf archive.tar.Z java/ hello.c

In this command,-ZOption specifies to use the compress command.

If the current system is not installedcompressThe command will report an error.

Generally speaking, for files compressed with compress, the file name will be.Zending. staytarBzip2 is used for compression in the command, and the file name will be.tar.Zending.

  • Automatically use the corresponding command for compression according to the suffix of the archive file
$ tar caf archive.tar.gz java/ hello.c
$ file archive.tar.gz
archive.tar.gz: gzip compressed data, from Unix, last modified: Fri Dec  6 14:50:57 2019
$ tar caf archive.tar.bz2 java/ hello.c
$ file archive.tar.bz2
archive.tar.bz2: bzip2 compressed data, block size = 900k

staytar caf archive.tar.gz java/ hello.cIn the command,-aOption specifies that the corresponding command is automatically selected according to the archive file suffix. The suffix provided here is.gz, the gzip command is used.

After executing the command, usefile archive.tar.gzCommand to view the generated file format, which is indeed gzip compressed format.

Similarly, intar caf archive.tar.bz2 java/ hello.cCommand, by-aOptions and.bz2The suffix specifies that bzip2 is used for the operation.

Command to decompress using tar

staytarCommand is also used to decompress-z-f-ZOptions. Here are some commands for decompressing using tar.

  • Unzip using gzip
tar xzvf archive.tar.gz

staytar xzf archive.tar.gzIn the command,-zOption specifies to operate with gzip command,-xOption specifies that files are extracted from the archive, combined with-zUse is to decompress.-f archive.tar.gzSpecify pairarchive.tar.gzExtract the archive file and extract the file locally. The file must be a compressed file in gzip format to extract correctly.

Use here-vOption to print the extracted file. This option is not required. If this option is not added, the interface will not print when decompressing.

  • Unzip using bzip2
tar xjvf archive.tar.bz2

In this command,-jOption specifies to use the bzip2 command.

  • Decompress using compress
tar xZvf archive.tar.Z

In this command,-ZOption specifies to use the compress command.

be careful: for some archive compressed files that do not end with a contractual suffix, you can usefileCommand to confirm its format so that the corresponding options can be used for decompression.

Encryption and decryption combined with OpenSSL command

Encrypt archive files

in usetarWhen the command is packaged, it can be combined withopensslCommand to encrypt the generated archive file. The specific commands are as follows:

tar -czf - filename | openssl des3 -salt -k passwd | dd of=filename.des3

staytar -czf - filenameIn the command,-zcSpecifies that the given file is compressed using gzip. If you want to use other compression commands, you can use the corresponding options instead.

-f -Indicates that the created archive file is written to standard output,tarOrders can put-As the file name, and some special processing. It will be explained later. andfilenameIs the packed and compressed file name. Multiple file names or directory names can be provided.

This command writes the generated archive file to the standard output so that the contents of the archive file can be piped toopensslCommand processing.

See the online help link for GNU tarhttps://www.gnu.org/software/…, for use-The description as file name is as follows:

If you use ‘-‘ as an archive-name, tar reads the archive from standard input (when listing or extracting files), or writes it to standard output (when creating an archive).
If you use ‘-‘ as an archive-name when modifying an archive, tar reads the original archive from its standard input and writes the entire new archive to its standard output.

That is, when creating an archive, use-As the file name, the generated archive file will be written to the standard output, and the file will not be generated to the local file system.

When extracting archive files, use-As the file name, the archive file to be extracted is read from standard input.

openssl des3 -salt -k passwdThe command specifies that the DES3 algorithm is used for encryption,-k passwdSpecify encryption, which can be modifiedpasswdInto other passwords. If you need to use other algorithms for encryption, you can viewopensslHelp notes for.

If you do not want to enter the password in clear text on the terminal, you can not provide it-k passwdOption, you will be prompted to enter the password from the terminal and will not be echoed.

dd of=filename.des3The command specifies that the encrypted file name isfilename.des3, you can change it to another file name.

Decrypt archived files

After encrypting the archive file with the above command, you can decrypt it with the following command:

dd if=filename.des3 | openssl des3 -d -k passwd | tar zxf -

dd if=filename.des3 Command specifies readfilename.des3File content.

openssl des3 -d -k passwdThe command indicates that the DES3 algorithm is used for decryption.

The content after decryption is the content before decryptiontarThe contents of the archive file generated by the command will be written to the standard output and passed to the later through the pipelinetarStandard input for the command.

Can not provide-k passwdOption, you will be prompted to enter the password from the terminal during execution, and it will not be echoed.

tar zxf -The command means to read the contents of the archive file to be extracted from the standard input, and the extracted file will be written locally.