tar命令可以用来进行打包和解打包,压缩和解压缩
基本语法:
打包和压缩的语法:
tar [选项] 源文件或目录
解打包和解压缩的语法:
tar [选项] 压缩包
选项说明:
打包与压缩的选项
1 -c:将多个文件或目录进行打包。
2 -v:显示打包文件的过程。
3 -f 文件名:指定打包的文件名。
4 -z:压缩和解压缩.tar.gz格式。
5 -j:压缩和解压缩.tar.bz2格式。
使用举例:
打包为tar格式的文件
[root@localhost home]# tar -cvf hello.tar hello hello-hard hello-soft
hello
hello-hard
hello-soft
[root@localhost home]# ls
hello hello-hard hello-soft hello.tar test test-soft
[root@localhost home]#
压缩为tar.gz格式的文件
[root@localhost home]# tar -zcvf test.tar.gz test test-soft
test/
test-soft
[root@localhost home]# ls
hello hello-hard hello-soft hello.tar test test-soft test.tar.gz
[root@localhost home]#
解打包和解压缩的选项
1 -x:对tar包做解打包操作。
2 -v:显示解打包的过程。
3 -f 文件名:指定要解压的文件名。
4 -z:压缩和解压缩.tar.gz格式。
5 -j:压缩和解压缩.tar.bz2格式。
6 -t:只查看包中有哪些文件或目录,不做解打包操作。
7 -C 目录名:指定解打包位置。
举例说明:
解打包tar格式的文件
[root@localhost home]# tar -xvf hello.tar
hello
hello-hard
hello-soft
[root@localhost home]# ls
hello hello-hard hello-soft hello.tar test.tar.gz
[root@localhost home]#
解压缩tar.gz格式的文件
[root@localhost home]# tar -zxvf test.tar.gz
test/
test-soft
[root@localhost home]# ls
hello hello-hard hello-soft hello.tar test test-soft test.tar.gz
[root@localhost home]#
查看tar格式的文件内容
[root@localhost home]# tar -tvf hello.tar
-rw-r--r-- root/root 10240 2019-07-11 01:28 hello
hrw-r--r-- root/root 0 2019-07-11 01:28 hello-hard 连接到 hello
lrwxrwxrwx root/root 0 2019-07-11 03:56 hello-soft -> hello
[root@localhost home]#
查看tar.gz格式的文件内容
[root@localhost home]# tar -ztvf test.tar.gz
drwxr-xr-x root/root 0 2019-07-11 01:14 test/
drwxr-xr-x root/root 0 2019-07-11 01:14 test-soft/
[root@localhost home]#