Linux 打包工具tar

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 86 13:54 test_grep
    -rw-r--r-- 1 root    root     325 811 17:13 test_sed
    -rw-r--r-- 1 root    root     331 811 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 812 10:25 mytest.tar
    -rw-r--r-- 1 root    root      436 86 13:54 test_grep
    -rw-r--r-- 1 root    root      325 811 17:13 test_sed
    -rw-r--r-- 1 root    root      331 811 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 812 10:25 mytest.tar
    -rw-r--r-- 1 root    root      436 86 13:54 test_grep
    -rw-r--r-- 1 root    root      325 811 17:13 test_sed
    -rw-r--r-- 1 root    root      331 811 17:12 test_sed.bak
    drwxr-xr-x 2 root    root       59 812 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 86 13:54 test_grep
    -rw-r--r-- 1 root root 325 811 17:13 test_sed
    -rw-r--r-- 1 root root 331 811 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 812 10:25 mytest.tar
    drwxr-xr-x 2 root    root        6 812 10:37 tar_test
    -rw-r--r-- 1 root    root      436 86 13:54 test_grep
    -rw-r--r-- 1 root    root      325 811 17:13 test_sed
    -rw-r--r-- 1 root    root      331 811 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 812 10:25 mytest.tar
    -rw-r--r-- 1 root    root      603 812 10:38 mytest.tar.bz2
    drwxr-xr-x 2 root    root        6 812 10:37 tar_test
    -rw-r--r-- 1 root    root      436 86 13:54 test_grep
    -rw-r--r-- 1 root    root      325 811 17:13 test_sed
    -rw-r--r-- 1 root    root      331 811 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 812 10:25 mytest.tar
    -rw-r--r-- 1 root    root      603 812 10:38 mytest.tar.bz2
    drwxr-xr-x 2 root    root        6 812 10:37 tar_test
    -rw-r--r-- 1 root    root      436 86 13:54 test_grep
    -rw-r--r-- 1 root    root      325 811 17:13 test_sed
    -rw-r--r-- 1 root    root      331 811 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 812 10:25 mytest.tar
    -rw-r--r-- 1 root    root      603 812 10:38 mytest.tar.bz2
    -rw-r--r-- 1 root    root      516 812 10:39 mytest.tar.gz
    drwxr-xr-x 2 root    root        6 812 10:37 tar_test
    -rw-r--r-- 1 root    root      436 86 13:54 test_grep
    -rw-r--r-- 1 root    root      325 811 17:13 test_sed
    -rw-r--r-- 1 root    root      331 811 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 812 10:25 mytest.tar
    -rw-r--r-- 1 root    root      603 812 10:38 mytest.tar.bz2
    -rw-r--r-- 1 root    root      516 812 10:39 mytest.tar.gz
    drwxr-xr-x 2 root    root        6 812 10:37 tar_test
    -rw-r--r-- 1 root    root      436 86 13:54 test_grep
    -rw-r--r-- 1 root    root      325 811 17:13 test_sed
    -rw-r--r-- 1 root    root      331 811 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 812 10:25 mytest.tar
    -rw-r--r-- 1 root    root      603 812 10:38 mytest.tar.bz2
    -rw-r--r-- 1 root    root      516 812 10:39 mytest.tar.gz
    -rw-r--r-- 1 root    root      556 812 10:40 mytest.tar.xz
    drwxr-xr-x 2 root    root        6 812 10:37 tar_test
    -rw-r--r-- 1 root    root      436 86 13:54 test_grep
    -rw-r--r-- 1 root    root      325 811 17:13 test_sed
    -rw-r--r-- 1 root    root      331 811 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]# 
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值