Linux shell command usage and tar command for common use cases

  • 2020-09-28 09:18:57
  • OfStack

preface

The tar command is used to archive multiple files or directories into a single archive file, and the archive file can be further compressed using techniques such as gzip or bzip2.

The command format

[

tar [OPTION...] [FILE]...

]

Command function

Tar (Tape ARchive, short for tape archive, originally designed for packaged files to tape, now mostly use it to realize backup a partition or some important directory) is the most widely used in class Unix system commands, used to archive more files or directories into a single archive file, and archive file can be used further gzip or bzip2 technology such as compression, can retain the file permissions. In other words, the tar command can also be used for backups: first archive multiple files and directories into a single tar file or archive, and then release the files and directories from the tar file as needed.

Command options

选项 含义
-A或�catenate 新增文件到以存在的备份文件
-B 设置区块大小
-c或�create 建立新的备份文件
-C<目录> 这个选项用在解压缩,若要在特定目录解压缩,可以使用这个选项
-d 记录文件的差别
-x或�extract或�get 从备份文件中还原文件
-t或�list 列出备份文件的内容
-z或�gzip或�ungzip 通过gzip指令处理备份文件
-Z或�compress或�uncompress 通过compress指令处理备份文件
-f<备份文件>或�file=<备份文件> 指定备份文件
-v或�verbose 显示指令执行过程
-r 添加文件到已经压缩的文件
-u 添加改变了和现有的文件到已经存在的压缩文件
-j 支持bzip2解压文件
-v 显示操作过程
-l 文件系统边界设置
-k 保留原有文件不覆盖
-m 保留文件不被覆盖
-w 确认压缩文件的正确性
-p或�same-permissions 用原来的文件权限还原文件
-P或�absolute-names 文件名使用绝对名称,不移除文件名称前的“/”号
-N <日期格式>或�newer=<日期时间>只将较指定日期更新的文件保存到备份文件里
�exclude=<范本样式> 排除符合范本样式的文件

What is "file compression"?

As we know, the contents of a file in a computer system are information. Information is actually a sequence of bits (also known as bits) consisting of the values 0 and 1. The eight bits are organized into a group called bytes. Generally speaking, the 8 bits of a byte are not fully utilized. These unused bits take up most of the space of a file, and "file compression" is to make use of the complicated calculation method to free up the unused space so that the file takes up less space.

In simple terms, "compression" is the filling of space in a file that is not fully filled. A compressed file cannot be used directly by the operating system, so to "uncompress" a file is to "restore" it to what it was before it was compressed. The ratio of the amount of disk space taken up by the compressed file to the compressed file is called the compression ratio.
Common compression formats

Common compression formats in Linux are:

[

*.Z: compress program compressed file.
*.gz: gzip program compressed file.
*.bz2: bzip2 program compressed file.
*.tar: tar program package data, not compressed.
*.tar. gz - tgz: Data packaged by the tar program, compressed by gzip.
*.ES74en.bz2 (.es76EN2) : Data packaged by the tar program, compressed by bzip2.

]

In the above compression format, gzip and bzip2 are the two compression commands, which are part of the GNU plan. Previously, it was the compress command, but it is no longer popular. bzip2 has a better compression ratio than gzip, although bzip2 can usually only be compressed and decompressed against one file. If so, it would be tedious to compress the entire development environment directory.

Hence the tar command, tar is not a "compress command" but a "package command". In other words, tar can "package" many files into a single file, or even a directory. The tar command did not support compression at first, but later GNU planned to combine the whole tar with compression in order to provide users with more convenient and powerful compression and packaging functions.

Only packaged tar files are known as tarfile files, and compressed tar files are called tarball files.

The almighty tar command

The profile

tar can break multiple directories or files into one large file and supports gzip/bzip2

Archive: tar {-ES123en} [option...] -f destination source

Additional archive: tar {-r |-u} -f source [option...] destination

tar {-t |-x} -f source [option...] -C destination

The easiest way to use tar is simply to remember the following:

tar-jcv-f ES158en.tar.bz2 Compressed file or directory name View file: ES161en-ES162en-ES163en ES164en.tar.bz2 tar-jxv-f filename.tar. gz-C

Since tar is not a compression command, but a packing command, how do you pack and compress? Let's look at the common parameters of the 1 tar command:

Model parameters

-ES186en (wM 187EN) : Create a new archive. -r (wM: append) : Create a new archive file like -c1, but in append mode, you can only append to an uncompressed archive file, requiring the -ES191en parameter to be specified. -ES192en: View which files are contained in the contents of the archive file, and you can see the details, including the file name. -ES193en: Same as -r1, but only updated files are added to the archive. -ES195en: Unzip the archive. If there are multiple files with the same file name in one archive, each file is unzipped first, and the latest file overwrites the old one.

tar is divided into three modes: -ES198en, -ES199en and -u1. It is the archive/compression mode. In this mode, tar recursively traverses all directories and files under the specified directory and creates archive files. -ES202en is represented in the de-archive/decompression mode, and -ES203en is represented in the print list mode.

General parameters

-ES209en: Use bzip2 support for compression and decompression, preferably with file name *.tar.bz2. -ES213en: Use gzip support for compression and decompression, preferably with file name *.tar.gz. -ES217en: Displays the file name being processed during compression/decompression. -ES218en: The filename to be processed is followed by -ES219en. It is best to write -ES219en as a single argument. -ES220en: Specifies the directory to decompress. -ES221en: Keep the original information of the file, permissions, etc -ES222en: Leave the absolute path when unzipping. exclude=FILE: When packing compression, do not pack FILE.

Package and create the archive

Example: Package 1 directory.

Description: Package /home/test and create an archive named ES235en-18-06-02.tar and save it in the current directory.


# tar -cv -f command-18-06-02.tar /home/test
/home/test/.bash_logout
/home/test/.bashrc
/home/test/apache-tomcat-9.0.7.tar.gz
/home/test/.bash_profile
/home/test/nginx-1.10.1.tar.gz

The -ES240en (short for create) parameter, which represents the creation of a new archive file for the specified file or directory. Use -ES242en to specify the archive file to read or write, which can represent standard input or standard output, and -ES243en to write in conjunction with other parameters, making sure that the f parameter is followed by a file name. This is not recommended because the argument switching order is allowed, and writing -cfv will result in the compressed file name becoming v.

Detailed output is generated using the -v representation, which lists the names of files being read or written to the archive file in compressed or uncompressed mode.

Create the ES254en.gz archive

Example: Package and compress using gzip.

Description: Package all the files in /home/test/images and compress them with gzip to produce an archive named ES267en-18-06-02.tar.gz and place it in the current directory.


# tar -zcv -f MyImages-18-06-02.tar.gz /home/test/images
OR
# tar -zcv -f MyImages-18-06-02.tar.tgz /home/test/images
/home/test/images/alejandro-gonzalez-17189.jpg
/home/test/images/brooke-lark-275181.jpg
/home/test/images/brenda-godinez-228181.jpg
/home/test/images/artur-rutkowski-97622.jpg
/home/test/images/ben-white-138743.jpg

-ES273en means to use gzip support to compress or extract files. Note that gzip's compressed file format is best written as ES276en.gz. (Note: tar.gz and tgz mean the same thing)

Package compression excludes some files

Example: Package, compress and exclude some files.

home/test/images package all files except ES293en-lark-275181.ES295en and ES296en-white-138743.ES298en and compress gzip to generate an archive named MyImages-18-06-02.ES301en.gz and place it in the current directory.


# tar -czv -f MyImages-18-06-02.tar.gz --exclude=./brooke-lark-275181.jpg --exclude=./ben-white-138743.jpg /home/test/images
/home/test/images/alejandro-gonzalez-17189.jpg
/home/test/images/brenda-godinez-228181.jpg
/home/test/images/artur-rutkowski-97622.jpg

Unzip archive files (default)

Example: Unzip, default unzip.

Description: Unzip the archive named ES312en-18-06-02.tar to the current directory.


# tar -xvf MyImages-18-06-02.tar
home/test/images/alejandro-gonzalez-17189.jpg
home/test/images/brenda-godinez-228181.jpg
home/test/images/artur-rutkowski-97622.jpg

Where, the -ES317en parameter means to unzip 1 archive file. If there are two files with the same name in the archive file, each file will be unzipped and the latest will overwrite the old file. Note that the -ES318en parameter is not specified here, because tar, seeing that the -ES320en parameter is specified, knows that this is an unzip operation and automatically determines the compression type of the unzip packet.

Unzip the archive and specify the directory

Example: Unzip to a specified directory.

Description: Unzip the archive file named ES330en-18-06-02.tar.gz to a specified directory.


# tar -xv -f MyImages-18-06-02.tar -C /home/test/public_images
home/test/public_images/alejandro-gonzalez-17189.jpg
home/test/public_images/brenda-godinez-228181.jpg
home/test/public_images/artur-rutkowski-97622.jpg

View the zip file information

Example: View zip file information.

Description: Lists the file information in MyImages- 18-06-02.tar.bz2. The -ES345en parameter generates output similar to the ls(1) command.


# tar -tv -f MyImages-18-06-02.tar.gz
OR
# tar -tv -f MyImages-18-06-02.tar.bz2
-rw-r--r-- root/root 2176861 2018-06-02 21:26 home/test/images/alejandro-gonzalez-17189.jpg
-rw-r--r-- root/root 8452524 2018-06-02 21:26 home/test/images/brenda-godinez-228181.jpg
-rw-r--r-- root/root 1131986 2018-06-02 21:26 home/test/images/artur-rutkowski-97622.jpg

Unzip individual files

Example: Unzip a single file.

Description: extract home/test/.bashrc from the archive.


# tar -xv -f command-18-06-02.tar home/test/.bashrc
home/test/.bashrc

Unzip multiple specified files

Example: Unzip multiple specified files.

Description: Extract multiple files such as file1, file2 from the archive file, which can be separated by Spaces or in the form of wildcards.


# tar -zxv -f MyImages-18-06-02.tar.gz "file 1" "file 2"
OR
# tar -zxv -f MyImages-18-06-02.tar.gz --wildcards '*b*.jpg'
home/test/images/brooke-lark-275181.jpg
home/test/images/brenda-godinez-228181.jpg
home/test/images/ben-white-138743.jpg
home/test/images/aleks-dahlberg-274646.jpg

conclusion


Related articles: