linux tar - 压缩解压

【基本介绍】
今天遇到要添加文件到tar文件里面,所以在这里介绍tar的一些用法。

【参数介绍】

GNU ‘tar’ saves many files together into a single tape or disk archive, and can restore individual files from the archive.
-c, --create
create a new archive
-r, --append
append files to the end of an archive

-t, --list
list the contents of an archive
-x, --extract, --get
extract files from an archive

-C, --directory=DIR
change to directory DIR

-f, --file=ARCHIVE
use archive file or device ARCHIVE

-j, --bzip2
filter the archive through bzip2
-p, --preserve-permissions
extract information about file permissions (default for superuser)

-v, --verbose
verbosely list files processed

-z, --gzip
filter the archive through gzip



【常用例子】
1. 创建归档文件
tar cvf archive_name.tar dirname/

c – create a new archive
v – verbosely list files which are processed.
f – following is the archive file name

2. 创建解压gzip归档文件
tar cvzf archive_name.tar dirname/
z – filter the archive through gzip
.tgz is same as .tar.gz

tar xvfz archive_name.tar.gz

3. 创建解压bzipped归档文件
tar cvfj archive_name.tar.bz2 dirname/
.tbz and .tb2 is same as .tar.bz2

tar xvfj archive_name.tar.bz2

4. 显示归档文件内容但是不解压
tar tvf archive_name.tar (归档文件)
tar tvfz archive_name.tar.gz (压缩gzip归档文件)
tar tvfj archive_name.tar.bz2 (压缩bzip归档文件)

5. 解压单一文件或者目录从tar tar.gz tar.bz2
tar xvf archive_file.tar /path/to/file
tar xvfz archive_file.tar.gz /path/to/file
tar xvfj archive_file.tar.bz2 /path/to/file

tar xvf archive_file.tar /path/to/dir/
tar xvfz archive_file.tar.gz /path/to/dir/
tar xvfj archive_file.tar.bz2 /path/to/dir/

6. 解压一组文件从tar tar.gz tar.bz2
tar xvf archive_file.tar --wildcards '*.pl'

7. 添加文件或者目录到已经有的归档文件中
tar rvf archive_name.tar newfile
tar rvf archive_name.tar newdir/
不能对已经压缩过的归档文件进行此操作,必须先解压然后再添加文件/目录然后再压缩
gunzip archive.tar.gz
tar rvf archive.tar newfile
gzip archive.tar


8. 压缩后删除源文件
tar zcvf archive.tar.gz newfile --remove-files

【参考引用】
[url]http://www.thegeekstuff.com/2010/04/unix-tar-command-examples/[/url]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值