Linux 打包工具tar@TOC
描述
tar 主要用于将多个文件或目录进行打包成一个文件进行存储备份;
tar 还可以调用其他压缩工具在打包的同时对包进行压缩处理;
使用方式
-
命令语法
tar [OPTION...] [FILE]...
-
常用选项
选项 描述 -c/–create 创建打包文件 -t/–list 列出打包文件中的内容列表 -x/–get 从打包文件中提取文件 -C DIR/–directory=DIR 切换工作目录,用于打包特定目录中的内容或解包到特定目录 -v/–verbose 显示详细操作过程 -f <files>/–file=<files> 指定需打包的文件,只能为最后一个选项 -j/–bzip2 通过bzip2的压缩方式进行压缩或解压,文件名*.tar.bz2 -z/–gzip 通过gzip的压缩方式进行压缩或解压,文件名*.tar.gz -J/–xz 通过xz的压缩方式进行压缩或解压,文件名*.tar.xz -
示例
- 文件打包、查包与解包
[root@centos-36_2 tmp]# ll | grep -E '.*test' -rw-r--r-- 1 root root 436 8月 6 13:54 test_grep -rw-r--r-- 1 root root 325 8月 11 17:13 test_sed -rw-r--r-- 1 root root 331 8月 11 17:12 test_sed.bak [root@centos-36_2 tmp]# [root@centos-36_2 tmp]# tar -cvf mytest.tar ./test_* # 打包当前目录下已test_开头的文件 ./test_grep ./test_sed ./test_sed.bak [root@centos-36_2 tmp]# ll | grep -E '.*test' -rw-r--r-- 1 root root 10240 8月 12 10:25 mytest.tar -rw-r--r-- 1 root root 436 8月 6 13:54 test_grep -rw-r--r-- 1 root root 325 8月 11 17:13 test_sed -rw-r--r-- 1 root root 331 8月 11 17:12 test_sed.bak [root@centos-36_2 tmp]# [root@centos-36_2 tmp]# tar -tvf mytest.tar # 列出mytest.tar包中的信息列表 -rw-r--r-- root/root 436 2021-08-06 13:54 ./test_grep -rw-r--r-- root/root 325 2021-08-11 17:13 ./test_sed -rw-r--r-- root/root 331 2021-08-11 17:12 ./test_sed.bak [root@centos-36_2 tmp]# [root@centos-36_2 tmp]# mkdir testtar [root@centos-36_2 tmp]# ll | grep -E '.*test' -rw-r--r-- 1 root root 10240 8月 12 10:25 mytest.tar -rw-r--r-- 1 root root 436 8月 6 13:54 test_grep -rw-r--r-- 1 root root 325 8月 11 17:13 test_sed -rw-r--r-- 1 root root 331 8月 11 17:12 test_sed.bak drwxr-xr-x 2 root root 59 8月 12 10:28 testtar [root@centos-36_2 tmp]# [root@centos-36_2 tmp]# tar -xvf mytest.tar -C ./testtar/ # 将mytest.tar解包到testtar目录 ./test_grep ./test_sed ./test_sed.bak [root@centos-36_2 tmp]# ll ./testtar/ 总用量 12 -rw-r--r-- 1 root root 436 8月 6 13:54 test_grep -rw-r--r-- 1 root root 325 8月 11 17:13 test_sed -rw-r--r-- 1 root root 331 8月 11 17:12 test_sed.bak [root@centos-36_2 tmp]#
- 文件打包同时以bz2格式压缩(调用命令bzip2)
[root@centos-36_2 tmp]# ll | grep -E '.*test' -rw-r--r-- 1 root root 10240 8月 12 10:25 mytest.tar drwxr-xr-x 2 root root 6 8月 12 10:37 tar_test -rw-r--r-- 1 root root 436 8月 6 13:54 test_grep -rw-r--r-- 1 root root 325 8月 11 17:13 test_sed -rw-r--r-- 1 root root 331 8月 11 17:12 test_sed.bak [root@centos-36_2 tmp]# [root@centos-36_2 tmp]# tar -cjvf mytest.tar.bz2 ./test_* ./test_grep ./test_sed ./test_sed.bak [root@centos-36_2 tmp]# ll | grep -E '.*test' -rw-r--r-- 1 root root 10240 8月 12 10:25 mytest.tar -rw-r--r-- 1 root root 603 8月 12 10:38 mytest.tar.bz2 drwxr-xr-x 2 root root 6 8月 12 10:37 tar_test -rw-r--r-- 1 root root 436 8月 6 13:54 test_grep -rw-r--r-- 1 root root 325 8月 11 17:13 test_sed -rw-r--r-- 1 root root 331 8月 11 17:12 test_sed.bak [root@centos-36_2 tmp]#
- 文件打包同时以gz格式压缩(调用命令gzip)
[root@centos-36_2 tmp]# ll | grep -E '.*test' -rw-r--r-- 1 root root 10240 8月 12 10:25 mytest.tar -rw-r--r-- 1 root root 603 8月 12 10:38 mytest.tar.bz2 drwxr-xr-x 2 root root 6 8月 12 10:37 tar_test -rw-r--r-- 1 root root 436 8月 6 13:54 test_grep -rw-r--r-- 1 root root 325 8月 11 17:13 test_sed -rw-r--r-- 1 root root 331 8月 11 17:12 test_sed.bak [root@centos-36_2 tmp]# [root@centos-36_2 tmp]# tar -czvf mytest.tar.gz ./test_* ./test_grep ./test_sed ./test_sed.bak [root@centos-36_2 tmp]# ll | grep -E '.*test' -rw-r--r-- 1 root root 10240 8月 12 10:25 mytest.tar -rw-r--r-- 1 root root 603 8月 12 10:38 mytest.tar.bz2 -rw-r--r-- 1 root root 516 8月 12 10:39 mytest.tar.gz drwxr-xr-x 2 root root 6 8月 12 10:37 tar_test -rw-r--r-- 1 root root 436 8月 6 13:54 test_grep -rw-r--r-- 1 root root 325 8月 11 17:13 test_sed -rw-r--r-- 1 root root 331 8月 11 17:12 test_sed.bak [root@centos-36_2 tmp]# [root@centos-36_2 tmp]# tar -tvf mytest.tar.bz2 -rw-r--r-- root/root 436 2021-08-06 13:54 ./test_grep -rw-r--r-- root/root 325 2021-08-11 17:13 ./test_sed -rw-r--r-- root/root 331 2021-08-11 17:12 ./test_sed.bak [root@centos-36_2 tmp]#
- 文件打包同时以xz格式压缩(调用命令xz)
[root@centos-36_2 tmp]# ll | grep -E '.*test' -rw-r--r-- 1 root root 10240 8月 12 10:25 mytest.tar -rw-r--r-- 1 root root 603 8月 12 10:38 mytest.tar.bz2 -rw-r--r-- 1 root root 516 8月 12 10:39 mytest.tar.gz drwxr-xr-x 2 root root 6 8月 12 10:37 tar_test -rw-r--r-- 1 root root 436 8月 6 13:54 test_grep -rw-r--r-- 1 root root 325 8月 11 17:13 test_sed -rw-r--r-- 1 root root 331 8月 11 17:12 test_sed.bak [root@centos-36_2 tmp]# [root@centos-36_2 tmp]# tar -cJvf mytest.tar.xz ./test_* ./test_grep ./test_sed ./test_sed.bak [root@centos-36_2 tmp]# ll | grep -E '.*test' -rw-r--r-- 1 root root 10240 8月 12 10:25 mytest.tar -rw-r--r-- 1 root root 603 8月 12 10:38 mytest.tar.bz2 -rw-r--r-- 1 root root 516 8月 12 10:39 mytest.tar.gz -rw-r--r-- 1 root root 556 8月 12 10:40 mytest.tar.xz drwxr-xr-x 2 root root 6 8月 12 10:37 tar_test -rw-r--r-- 1 root root 436 8月 6 13:54 test_grep -rw-r--r-- 1 root root 325 8月 11 17:13 test_sed -rw-r--r-- 1 root root 331 8月 11 17:12 test_sed.bak [root@centos-36_2 tmp]#
- 对不同格式压缩进行解包(解包时不必加解压格式,直接使用-xvf即可,内部调用bunzip2/gunzip/unxz进行解压)
[root@centos-36_2 tmp]# tree ./tar_test/ ./tar_test/ ├── bz ├── gz └── xz 3 directories, 0 files [root@centos-36_2 tmp]# tar -xvf mytest.tar.bz2 -C ./tar_test/bz/ ./test_grep ./test_sed ./test_sed.bak [root@centos-36_2 tmp]# tar -xvf mytest.tar.gz -C ./tar_test/gz/ ./test_grep ./test_sed ./test_sed.bak [root@centos-36_2 tmp]# tar -xvf mytest.tar.xz -C ./tar_test/xz/ ./test_grep ./test_sed ./test_sed.bak [root@centos-36_2 tmp]# tree ./tar_test/ ./tar_test/ ├── bz │ ├── test_grep │ ├── test_sed │ └── test_sed.bak ├── gz │ ├── test_grep │ ├── test_sed │ └── test_sed.bak └── xz ├── test_grep ├── test_sed └── test_sed.bak 3 directories, 9 files [root@centos-36_2 tmp]#