Commonly used linux compression and decompression files or folder commands tar and zip

Preface

At work, compressing and decompressing files or folders are relatively basic operations. You can use the tar or zip command to package a large number of files and directories into one file, which is very useful for backing up files or combining several files into one file for network transmission.

tar compressed file

tar [选项] 源文件或目录
[选项] 如下:
-c  将多个文件或目录进行打包。
-A  追加 tar 文件到归档文件。
-f 包名   指定包的文件名。包的扩展名是用来给管理员识别格式的,所以一定要正确指定扩展名。
-v  显示打包文件过程。

When using the tar command to specify options, you do not need to enter "-" in front of the options. For example, using the "cvf" option has the same effect as "-cvf".

tar -zcvf test.tar.gz ./test/ # 该命令表示压缩当前文件夹下的文件夹test,压缩后缀名为test.tar.gz
tar -cvf test.tar ./test/ # 如果不需要压缩成gz

The tar command can also package multiple files or directories, as long as they are separated by spaces. For example:

tar -cvf ana.tar anaconda-ks.cfg /tmp/ # 把anaconda-ks.cfg文件和/tmp目录打包成ana.tar文件包

tar decompression file

tar [选项] 压缩包
[选项] 如下:
-xtar 包做解打包操作。
-f  指定要解压的 tar 包的包名。
-t  只查看 tar 包中有哪些文件或目录,不对 tar 包做解打包操作。
-C 目录   指定解打包位置。
-v  显示解打包的具体过程。

In fact, compared with packaging, unpacking only requires replacing the packaging option "-cvf" with "-xvf".

tar -xzvf test.tar.gz # 该命令表示把后缀为.tar.gz的文件解压到当前文件夹下
tar -xvf test.tar # 如果压缩文件的后缀是.tar,没有gz,表示把后缀为.tar解压到当前文件夹下

If you use the "-xvf" option, the files in the package will be extracted to the current directory. If you want to specify the decompression location, you need to use the "-C (uppercase)" option. For example:

tar -xvf test.tar -C /tmp  # 把文件包test.tar解打包到/tmp/目录下

If you only want to see which files are in the package, you can replace the unpacking option "-x" with the testing option "-t". For example:

tar -tvf test.tar  # 会用长格式显示test.tar文件包中文件的详细信息,但不进行解压

Compressing files directly into ".tar.gz" and ".tar.bz2" formats is the most commonly used compression method in Linux

Compress and decompress ".tar.gz" format

tar -zcvf tmp.tar.gz /tmp/  # 把/temp/目录直接打包压缩为".tar.gz"格式,通过"-z"来识别格式,"-cvf"和打包选项一致

Decompression just adds a "-z" option in front of the unpacking option "-xvf". as follows:

tar -zxvf tmp.tar.gz  # 解压缩与解打包".tar.gz"格式

Compressing and decompressing “.tar.bz2” format

# 和".tar.gz"格式唯一的不同就是"-zcvf"选项换成了 "-jcvf",如下所示:
tar -jcvf tmp.tar.bz2 /tmp/  # 打包压缩为".tar.bz2"格式,注意压缩包文件名
tar -jxvf tmp.tar.bz2  # 解压缩与解打包".tar.bz2"格式

zip compressed file

zip [选项] 压缩包名 源文件或源目录列表
[选项] 如下:
-r  递归压缩目录,及将制定目录下的所有文件以及子目录全部压缩。
-m  将文件压缩之后,删除原始文件,相当于把文件移到压缩文件中。
-v  显示详细的压缩过程信息。
-q  在压缩的时候不显示命令的执行过程。
-压缩级别   压缩级别是从 1~9 的数字,-1 代表压缩速度更快,-9 代表压缩效果更好。
-u  更新压缩文件,即往压缩文件中添加新文件。

Compressed file

zip ana.zip anaconda-ks.cfg  # 把anaconda-ks.cfg压缩成 ana.zip
zip test.zip install.log install.log.syslog    # 同时压缩多个文件,把install.log install.log.syslog两个文件压缩

To compress the directory, you need to use the "-r" option

zip -r dir1.zip /tmp/  # 把tmp目录压缩成dir1.zip

unzip unzip zip file

unzip [选项] 压缩包名
[选项] 如下:
-d 目录名  将压缩文件解压到指定目录下。
-n  解压时并不覆盖已经存在的文件。
-o  解压时覆盖已经存在的文件,并且无需用户确认。
-v  查看压缩文件的详细信息,包括压缩文件中包含的文件大小、文件名以及压缩比等,但并不做解压操作。
-t  测试压缩文件有无损坏,但并不解压。
-x 文件列表 解压文件,但不包含文件列表中指定的文件。

Whether it is a file compressed package or a directory compressed package, it can be decompressed directly.

unzip dir1.zip  # 把dir1.zip解压在当前目录

Use the -d option to manually specify the decompression location

unzip -d /tmp/ ana.zip  # 把ana.zip解压在/tmp/目录

Guess you like

Origin blog.csdn.net/qq_34125713/article/details/128691745