tar命令使用简介

2025-11-01 20:34:51

1、创建

-f, --file=ARCHIVE     use archive file or device ARCHIVE :使用存档文件或设备档案(后面跟所要打包或解包的包名)

-v, --verbose      verbosely list files processed:详细显示

-c, --create      create a new archive :          建立新的归档文件

tar cvf applepen.tar apple pen

把apple pen这两个文件打包成一个文件applepen.tar,.tar是文件后缀。

记住-f后面先写打包的包名 applepen,后面是要打包的内容,apple和pen。

tar命令使用简介

2、压缩

-z, --gzip   ilter the archive through gzip:使用gzip压缩

可以调用gzip,bzip等压缩工具,对包进行压缩。gzip最常用,使用gzip即可,.gz为后缀。

tar命令使用简介

3、解包

-x, --extract, --get    extract files from an archive:解压(自动识别各种格式的压缩)

-C, --directory=DIR  change to directory DIR:加压到指定目录

tar xvf app/applepen.tar

-f后面跟所要加压的包即可

tar命令使用简介

4、查看

-t, --list     list the contents of an archive: 查看存档的内容列表

tar tvf applepen.tar

-t查看包里的内容,并不解压。

tar命令使用简介

5、追加

-r, --append      append files to the end of an archive:附加文件

tar rvf applepen.tar a1 a2

把a1、a2追加到已经打好的包里

tar命令使用简介

6、删除

--delete    delete from the archive (not on mag tapes!):删除包里的文件

tar --delete -vf applepen.tar a1 a2 a3

删除包里面的文件a1、a2、a3,没有的话会报错,注意名子要一样。

tar命令使用简介

7、注意

不要用绝对路径,使用相对路径;

tar: Removing leading `/' from member names  (移除绝对路径的/)

如果带/,解压后可能覆盖原有文件。

不要在当前目录打包,在上级目录打包

tar: ./aaa.tar.gz: file is the archive; not dumped

当前目录会报这个错

tar命令使用简介

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢