Linux 基础命令知识3

提取快照和备份数据都是日常工作,这节了解一下Shell基本的归档命令知识

tar归档

tar用于将多个文件和文件夹保存为单个文件,同时保留所有的文件属性,如所有者、权限等。

 
 
  1. #tar 归档语法 tar -cf output.tar [SOURCES]
  2. tar -cf output.tar file1 file2 file
  3. # 命令参数-c 代表 “create file” -f 表示“指定文件名(specify filename)” -f后面必须紧跟文件名
tar向归档文件中添加文件
 
 
  1. #-r append file to tar file
  2. tar -rvf output.tar appent.file
  3. #-v verbosly list files processed 打印处理的文件名称
  4. #用下面的方法显示出归档文件的内容 -t list the contents of archive
  5. tar -tf output.tar
  6. hello.gz
  7. shell_curl.sh
  8. tar -tvvf test.tar
  9. -rwxr-xr-x root/root 181 2013-09-03 18:16 hello.gz
  10. -rwxr-xr-x root/root 71 2013-09-04 18:14 shell_curl.sh
tar从归档文件中提取文件或文件夹

选项 -x 表示提取extratale file from the archive 
选项 -C 表示指定提取的目标地址文件 change the directory DIR

 
 
  1. #当使用 -x时-f指定stdin用于提取内容 -c时-f表示指定stdout作为归档文件
  2. tar -xvf test.tar test
  3. tar -cf - file1 file2 | tar -xvf - -C ~/directory
tar拼接两个归档文件
 
 
  1. #使用-A选项 append tar file to archive
  2. tar -Avf test.tar test2.tar
  3. tar -tf test.tar
  4. hello.gz
  5. shell_curl.sh
  6. shell_for.sh
tar通过检查时间戳来更新归档的内容
 
 
  1. #-u only update file newer to the archive
  2. tar -uvvf archive.tar filea
从归档文件中删除文件
 
 
  1. tar -f archive.tar --delete file1 file2
  2. tar --delte --file archive.tar file1 file2
压缩归档文件

tar 命名只能用来对文件进行归档,它并不具备压缩功能,所以多数用户在使用归档文件时都会对文件采用某种形式的压缩,归档文件通常被压缩为一下格式之一:.tar.gz ,.tar.bz2, .tar.lzma,.tar.lzo

 
 
  1. #可以通过 -z 来指定使用gzip格式 -j指定bunzip格式
  2. #--exculude 排除所有的.txt文件
  3. tar -czvvf arch.tar.gz * --exculude "*.txt"
  4. tar -czvvf test.tar.gz shell_*
  5. -rwxrwxrwx root/root 114 2013-09-26 11:05 shell_26.sh
  6. -rwxr-xr-x root/root 147 2013-09-05 17:29 shell_case.sh
  7. -rwxr-xr-x you/you 71 2013-09-04 18:14 shell_curl.sh
  8. -rwxr-xr-x root/root 199 2013-09-05 12:22 shell_findMaxFile.sh
  9. -rwxr-xr-x root/root 344 2013-09-04 17:40 shell_for.sh
  10. -rwxr-xr-x root/root 362 2013-09-05 17:23 shell_getInet.sh

gunzip 或 gzip压缩

gunzip 和 zip 压缩文件
 
 
  1. gzip filename
  2. ls
  3. filename.gz
  4. #gzip会删除原文件生成一个压缩文件filename.gz
  5. #gzip查看压缩文件的属性信息
  6. gzip -l hello.gz
  7. compressed uncompressed ratio uncompressed_name
  8. 181 211 25.6% hello
  9. #从stdin 中读取并写入到stdout中:
  10. cat file | gzip -c > file.gz
压缩归档文件

方法1

 
 
  1. #-z 选项指定使用gzip格式进行压缩
  2. tar -czvvf archive.tar.gz [files]
  3. #-a 指定从文件扩展名自动判断压缩格式
  4. tar -cavvf archive.tar.[files]
  5. #ps tar 命令解压-x选项用于解压,tar -zxvf archive.tar.gz 或者 tar -xavf archive.tar.gz

方法2

 
 
  1. #创建一个归档文件
  2. tar -cvvf archive.tar [files]
  3. #压缩归档文件
  4. gzip archive.tar
  5. #PS: gzip -9 test.img 指定压缩率 1最下 9最大 压缩率越大,压缩速度越慢
解压缩
 
 
  1. gunzip filename.gz
  2. #zcat 无需解压文件,直接读取内容到stdout
  3. zcat fileame.gz
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值