Common Linux commands (2): tar command (compressed files/decompressed files)

1. Introduction to commands

tar is used to create or restore Linux files and directories as archives. In addition, tar can also change files in the archive or add new files to the archive. This program was originally designed to back up files to tape (Tape ARchive), hence the name tar.

The working process of tar is mainly divided into two steps, the forward direction is packaging and compression, and the reverse direction is decompression and restoration. Packaging refers to turning a large number of files or directories into a total file, while compression refers to turning a large file into a small file through some compression algorithms. Reverse decompression and restoration are exactly the opposite processes. The compression and decompression of tar mainly uses the gzip and bzip2 commands.

So why are compressed packages like .zip and .rar rarely seen in Linux?
Zip and rar cannot retain file attributes when compressing files, so there is compression using the tar command, that is, archiving first and then compressing.
In Linux systems, it is usually preferable to use the .tar compressed file format, because the .tar file is not compressed, but just packs the files together, which is more suitable for archiving and file packaging.

In Linux, common compressed package formats are: .tar, .tar.xz, .tar.gz(or .tgz), .tar.bz2. Note here: .tarthe file is a file format that is only packaged and not compressed.
And .tar.xz, .tar.gz(or .tgz), .tar.bz2file is the compression of .tar file.
So the .tar file itself is not a compressed file, but an archive file format.

2. Installation and uninstallation

1. Check if there is tar command on linux

yum list installed | grep tar

Insert image description here
2. Install tar command

yum -y install tar

3. Extended commands to install and uninstall updates

命令1:yum -y install [关键词]
命令2:yum -y remove [关键词]
命令3:yum -y update [关键词]

3. Command format

tar [OPTIONS] [FILE]...

Option description

-A, --catenate, --concatenate
	新增文件到已存在的存档
-c,--create
	建立新的存档
-C, --directory=DIR
	将 tar 的工作目录从当前目录改为指定目录。该选项对顺序敏感,即影响其后的所有选项
-d, --diff, --compare
	对比档案与文件系统的差异
--delete
	从档案中删除制定的文件
-f, --file=ARCHIVE
	指定存档文件
-j, --bzip2
	通过 bzip2 命令压缩或解压缩档案
-k, --keep-old-files
	还原档案时,保留当前目录下的原有文件不被覆盖 
-l, --check-links
	如果不是所有链接都被转储,则打印一条消息
-m, --touch
	还原文件时,不变更文件的更改时间
-N, --newer, --after-date=DATE
	只将较指定日期更新的文件保存到档案中
-O, --to-stdout
	将提取的文件名和目录名打印到标准输出
-p, --preserve-permissions, --same-permissions
	提取文件时保留文件原来的权限
-P, --absolute-names
	创建存档时不移除文件名称前的 / 号
-r, --append
	追加文件到档案的末尾
-t, --list
	列出档案的内容
-u, --update
	添加比档案中文件更新的文件到档案中
-v, --verbose
	显示指令执行过程
-W, --verify
	向档案写入文件后尝试验证
-x, --extract, --get
	从档案提取文件  
-z, --gzip, --gunzip, --ungzip
	通过 gzip 命令压缩或解压档案
-Z, --compress, --uncompress
	通过 compress 指令处理备份文件
--exclude=PATTERN
	排除符合指定模式的文件
-?, --help
	显示短选项的概要信息并退出
--usage
	显示可用选项列表并退出
--version
	显示版本和版权信息并退出

4. Commonly used examples

The file directory structure on Linux is as follows. Combine the following files to explain the tar example.

/home/test
├── a.txt
├── b.log
├── test01
    └── c.txt

4.1. Packaging

(1) Pack the directories or files in the specified directory.

tar -czvf test.tar.gz  /home/test

由于指定了绝对路径/home/test, 所以压缩包内将目录也保存下来了
Insert image description here
Insert image description here
(2) Package the directory /home/test into test.tgz, and use gzip for compression.

tar -czvf test.tgz /home/test

(3) Package the directory /home/test into test.tbz2, and use bzip2 for compression.

tar –cjvf test.tgz2 /home/test

(4) Append files to the existing tar file

tar -rvf test.tar newfile1 newfile2

4.2. Unpacking

(1) Restore the compressed package test.tar.gz to the original directory

tar -xzvf test.tar.gz

注意:执行该命令会把文件解压缩到当前目录,若tar.gz压缩包内包含了绝对路径,则会在当前目录下创建该绝对路径。例如:在当前目录下创建/home/test。 若不想再次创建该路径,可以在home的上一级目录下执行tar解压命令

(2) Restore the compressed package test.tgz to the original directory and use gzip to decompress it.

tar -xzvf test.tgz

(3) Unpack test.tgz to the specified directory and use gzip to decompress it.

tar -xzvf test.tgz -C DIR

(4) Restore the compressed package test.tbz2 to the original directory and use bzip2 to decompress it.

tar -xjvf test.tbz2

4.3. View

(1) Only view the file list of the archive and do not unpack it.

tar -tzvf test.tar.gz

Insert image description here

5. Summary

The .tar file is a file format that is only packaged and not compressed.
But sometimes in order to reduce the file size, .tar files are combined with compression algorithms, such as the frequently used gzip and bzip2 compression algorithms. Such files are usually called tar archives or .tar.gz, .tar.bz2, etc. These tarballs are compressed versions of .tar files in order to reduce the file size. Decompressing these compressed packages will restore them to the original .tar files.

Guess you like

Origin blog.csdn.net/weixin_49114503/article/details/132982384