今天遇到要添加文件到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
【参考引用】
http://www.thegeekstuff.com/2010/04/unix-tar-command-examples/