摘要:本文主要学习了Linux的打包命令和压缩命令。
tar命令
tar命令可以用来进行打包和解打包,压缩和解压缩。
基本语法
打包和压缩的语法:
1 tar [选项] 源文件或目录
解打包和解压缩的语法:
1 tar [选项] 压缩包
选项说明
打包和压缩的选项:
1 -c:将多个文件或目录进行打包。
2 -v:显示打包文件的过程。
3 -f 文件名:指定打包的文件名。
4 -z:压缩和解压缩.tar.gz格式。
5 -j:压缩和解压缩.tar.bz2格式。
解打包和解压缩的选项:
1 -x:对tar包做解打包操作。
2 -v:显示解打包的过程。
3 -f 文件名:指定要解压的文件名。
4 -z:压缩和解压缩.tar.gz格式。
5 -j:压缩和解压缩.tar.bz2格式。
6 -t:只查看包中有哪些文件或目录,不做解打包操作。
7 -C 目录名:指定解打包位置。
使用举例
打包为 tar 格式的文件:
1 [root@localhost home]# tar -cvf hello.tar hello hello-hard hello-soft
2 hello
3 hello-hard
4 hello-soft
5 [root@localhost home]# ls
6 hello hello-hard hello-soft hello.tar test test-soft
7 [root@localhost home]#
压缩为 tar.gz 格式的文件:
1 [root@localhost home]# tar -zcvf test.tar.gz test test-soft
2 test/
3 test-soft
4 [root@localhost home]# ls
5 hello hello-hard hello-soft hello.tar test test-soft test.tar.gz
6 [root@localhost home]#
解打包 tar 格式的文件:
1 [root@localhost home]# tar -xvf hello.tar
2 hello
3 hello-hard
4 hello-soft
5 [root@localhost home]# ls
6 hello hello-hard hello-soft hello.tar test.tar.gz
7 [root@localhost home]#
解压缩 tar.gz 格式的文件:
1 [root@localhost home]# tar -zxvf test.tar.gz
2 test/
3 test-soft
4 [root@localhost home]# ls
5 hello hello-hard hello-soft hello.tar test test-soft test.tar.gz
6 [root@localhost home]#
查看 tar 格式文件的内容:
1 [root@localhost home]# tar -tvf hello.tar
2 -rw-r--r-- root/root 10240 2019-07-11 01:28 hello
3 hrw-r--r-- root/root 0 2019-07-11 01:28 hello-hard 连接到 hello
4 lrwxrwxrwx root/root 0 2019-07-11 03:56 hello-soft -> hello
5 [root@localhost home]#
查看 tar.gz 格式文件的内容:
1 [root@localhost home]# tar -ztvf test.tar.gz
2 drwxr-xr-x root/root 0 2019-07-11 01:14 test/
3 drwxr-xr-x root/root 0 2019-07-11 01:14 test-soft/
4 [root@localhost home]#
zip命令
zip命令类似于Windows系统中的winzip压缩程序。
基本语法
1 zip [选项] 压缩包名 源文件或源目录列表
选项说明
1 -r:递归压缩目录,及将指定目录下的所有文件以及子目录全部压缩。
2 -m:将文件压缩之后,删除原始文件,相当于把文件移到压缩文件中。
3 -v:显示详细的压缩过程信息。
4 -q:在压缩的时候不显示命令的执行过程。
5 -压缩级别:压缩级别是从1~9的数字,-1代表压缩速度更快,-9代表压缩效果更好。
6 -u:更新压缩文件,即往压缩文件中添加新文件。
使用举例
1 [root@localhost home]# ls
2 hello hello-hard hello-soft test test-soft
3 [root@localhost home]# zip hello.zip hello hello-hard
4 adding: hello (deflated 99%)
5 adding: hello-hard (deflated 99%)
6 [root@localhost home]# ls
7 hello hello-hard hello-soft hello.zip test test-soft
8 [root@localhost home]# zip test.zip test test-soft
9 adding: test/ (stored 0%)
10 adding: test-soft/ (stored 0%)
11 [root@localhost home]# ls
12 hello hello-hard hello-soft hello.zip test test-soft test.zip
13 [root@localhost home]#
unzip命令
unzip命令可以查看和解压缩zip文件。
基本语法
1 unzip [选项] 压缩包名
选项说明
1 -d 目录名:将压缩文件解压到指定目录下。
2 -n:解压时并不覆盖已经存在的文件。
3 -o:解压时覆盖已经存在的文件,并且无需用户确认。
4 -v:查看压缩文件的详细信息,包括压缩文件中包含的文件大小、文件名以及压缩比等,但并不做解压操作。
5 -t:测试压缩文件有无损坏,但并不解压。
6 -x 文件列表:解压文件,但不包含文件列表中指定的文件。
使用举例
1 [root@localhost home]# ls
2 hello.zip test.zip
3 [root@localhost home]# unzip -v hello.zip
4 Archive: hello.zip
5 Length Method Size Cmpr Date Time CRC-32 Name
6 -------- ------ ------- ---- ---------- ----- -------- ----
7 10240 Defl:N 99 99% 07-11-2019 01:28 dda39bf9 hello
8 10240 Defl:N 99 99% 07-11-2019 01:28 dda39bf9 hello-hard
9 -------- ------- --- -------
10 20480 198 99% 2 files
11 [root@localhost home]# unzip hello.zip
12 Archive: hello.zip
13 inflating: hello
14 inflating: hello-hard
15 [root@localhost home]# ls
16 hello hello-hard hello.zip test.zip
17 [root@localhost home]# unzip -d zip test.zip
18 Archive: test.zip
19 creating: zip/test/
20 creating: zip/test-soft/
21 [root@localhost home]# ls
22 hello hello-hard hello.zip test.zip zip
23 [root@localhost home]# ls zip
24 test test-soft
25 [root@localhost home]#
以上就是良许教程网为各位朋友分享的Linux相关知识。