The tar command is a powerful tool for SSH access to servers that require compression, decompression, packaging, and unpacking

The tar command creates files for Linux files and directories. With tar, you can create an archive (a backup file) for a particular file, change files in the archive, or add new files to the archive. Tar was originally used to create files on tape, but now users can create files on any device. With tar, you can pack a whole bunch of files and directories into a single file, which is useful for backing up files or combining several files into one file for easy transmission over the network.

Pack and compress

  • Packaging means turning a bunch of files or directories into a single file;
  • Compression is a large file through some compression algorithms into a small file.

Many Linux compressors can only compress a single file, so when you want to compress a large number of files, you have to pack them into a single file (tar) and then compress them with the compressors (gzip bzip2).

The most common package for Linux is tar. The packages produced by tar are often called tar packages, and the commands in a tar package are usually finished with.tar. Once the tar is generated, you can use other programs to compress it.

The command format

> tar [required parameters] [select parameters] file

Command function

Used to compress and extract files. Tar itself has no compression capability. It is achieved by using the compression function.

The command parameter

Necessary parameters

  • -a or –catenate: Add file to existing backup file;
  • -b: Set block size;
  • -c or –create: create a new backup file;
  • -c < directory > : This option is used for decompression. To decompress from a specific directory, use this option.
  • -D: Record file differences;
  • -x or –extract or –get: Restore files from backup files;
  • -t or –list: lists the contents of the backup files;
  • -z or –gzip or –ungzip: Processes backup files with the gzip directive;
  • -z or –compress or –uncompress: Backup files are processed with the compress instruction;
  • -f< backup file > or –file=< backup file > : Specifies the backup file;
  • -v or –verbose: Shows the execution of the command;
  • -r: Add file to already compressed file;
  • -u: Add changed and existing files to existing compressed files;
  • -j: Support bzip2 to decompress files;
  • -v: Display the operation process;
  • -l: file system boundary setting;
  • -k: Keep the original file without overwriting;
  • -m: Keep the file unoverwritten;
  • -W: Confirm the correctness of the compressed file;
  • -p or –same-permissions: Restore a file with its original file permissions;
  • -p or — Absolute-names: Use absolute names for file names and do not remove the “/” sign before the file name;
  • -n < date format > or –newer=< date time > : Save only files newer than the specified date to the backup files;
  • –exclude=< template style > : exclude template style compliant files.

To choose parameters

  • -b Sets the number of blocks
  • -c Switch to the specified directory
  • -f Specifies compressed files
  • –help displays help information
  • –version displays version information

Pack a file without compressing it

> tar -cvf rumenz.tar rumenz.txt

Pack and compress a file (using gzip compression)

> tar -zcvf rumenz.tar.gz rumenz.txt

Pack and compress a file (using bzip2 compression)

> tar -jcvf rumenz.tar.bz2 rumenz.txt

The filename after option f is self-made, and we traditionally use.tar for identification. If the z option is added,.tar.gz or.tgz represent the gzip compressed tar package. If you add the j option, use.tar.bz2 as the tar package name.

Lists the contents of the packaged zip file

> tar -ztvf rumenz.tar.gz

Because we use
gzipCompression of the
rumenz.tar.gz, so look it up
rumenz.tar.gzThe files in the package must be added
zThat’s the choice

tar.gzunzip

> tar -xzvf rumenz.tar.gz

tar.gzUnzip to the/TMP directory

> tar -xzvf rumenz.tar.gz -C /tmp

Just unziprumenz.tar.gzThe inside of the1.txt.2.txt

> tar-zcvf rumenz.tar.gz *.txt // Check the file inside rumenz.tar.gz (without compression) > tar-ztvf rumenz.tar.gz 1.txt 2.txt 3.txt 4.txt > tar -zxvf rumenz.tar.gz 1.txt 2.txt 1.txt 2.txt > ls 1.txt 2.txt rumenz.tar.gz

Compress the file and preserve its permissions

> tar -zcpvf rumenz.tar.gz 1.txt 2.txt 3.txt 4.txt

The -p property is important, especially if you want to keep the original file’s properties.

Compress a file (or directory) without packaging

> tar --exclude rumenz/doc rumenz.tar.gz rumenz/*

in/etc, than2021/01/01New files are only backed up

> tar -N "2021/01/01" -zcvf etc.tar.gz /etc

Bzip2 Compression, View, Unzip

  • Compression: tar-jcvf rumenz.tar.bz2 rumenz
  • Query: tar-jtvf rumenz.tar.bz2
  • Tar-jxvf rumenz.tar.bz2-c test

The original link: https://rumenz.com/rumenbiji/… WeChat official account: entry station