0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

Linux中的18个tar命令示例

dyquk4xk2p3d 来源:良许Linux 2023-05-04 14:59 次阅读

1. 在 Linux 中创建 tar 存档文件

下面的示例命令将创建一个tar存档文件rumenz-14-09-12.tar对于目录/home/rumenz在当前工作目录中。请参阅示例命令的操作。

#tar-cvfrumenz-14-09-12.tar/home/rumenz/

/home/rumenz/
/home/rumenz/cleanfiles.sh
/home/rumenz/openvpn-2.1.4.tar.gz
/home/rumenz/rumenz-14-09-12.tar
/home/rumenz/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm
/home/rumenz/rpmforge-release-0.5.2-2.el5.rf.i386.rpm

创建 tar 存档文件的选项。

c– 创建一个新的 .tar 存档文件。

v– 详细显示 .tar 文件进度。

f– 存档文件的文件名类型。

2. 在 Linux 中创建 tar.gz 存档文件

创建一个压缩gzip我们使用该选项作为存档文件z.例如,下面的命令将创建一个压缩MyImages-14-09-12.tar.gz目录的文件/home/MyImages.(Note: tar.gz and tgz both are similar)。

#tarcvzfMyImages-14-09-12.tar.gz/home/MyImages
OR
#tarcvzfMyImages-14-09-12.tgz/home/MyImages

/home/MyImages/
/home/MyImages/Sara-Khan-and-model-Priyanka-Shah.jpg
/home/MyImages/RobertKristenviolent101201.jpg
/home/MyImages/Justintimerlake101125.jpg
/home/MyImages/Mileyphoto101203.jpg
/home/MyImages/JenniferRobert101130.jpg
/home/MyImages/katrinabarbiedoll231110.jpg
/home/MyImages/the-japanese-wife-press-conference.jpg
/home/MyImages/ReesewitherspoonCIA101202.jpg
/home/MyImages/yanaguptabaresf231110.jpg

3. 在 Linux 中创建 tar.bz2 存档文件

bz2功能压缩并创建一个小于gzip.这bz2压缩比压缩和解压缩文件需要更多的时间gzip

要创建压缩的tar文件,我们使用选项j.以下示例命令将创建一个Phpfiles-org.tar.bz2一个目录的文件/home/php

#tarcvfjPhpfiles-org.tar.bz2/home/php
OR
#tarcvfjPhpfiles-org.tar.tbz/home/php
OR
#tarcvfjPhpfiles-org.tar.tb2/home/php

/home/php/
/home/php/iframe_ew.php
/home/php/videos_all.php
/home/php/rss.php
/home/php/index.php
/home/php/vendor.php
/home/php/video_title.php
/home/php/report.php
/home/php/object.html
/home/php/video.php

4. 在 Linux 中解压 tar 存档文件

要解压缩或提取 tar 文件,只需使用选项发出以下命令x(extract)。例如,下面的命令将解压文件public_html-14-09-12.tar在当前工作目录中。

如果要在不同的目录中解压,请使用选项作为-C(specified directory)。

##UntarfilesinCurrentDirectory##
#tar-xvfpublic_html-14-09-12.tar

##UntarfilesinspecifiedDirectory##
#tar-xvfpublic_html-14-09-12.tar-C/home/public_html/videos/

/home/public_html/videos/
/home/public_html/videos/views.php
/home/public_html/videos/index.php
/home/public_html/videos/logout.php
/home/public_html/videos/all_categories.php
/home/public_html/videos/feeds.xml

5. 在 Linux 中解压 tar.gz 存档文件

解压tar.gz存档文件,只需运行以下命令。如果我们想在不同的目录中解压,只需使用选项-C和目录路径,如上例所示。

#tar-xvfthumbnails-14-09-12.tar.gz

/home/public_html/videos/thumbnails/
/home/public_html/videos/thumbnails/katdeepika231110.jpg
/home/public_html/videos/thumbnails/katrinabarbiedoll231110.jpg
/home/public_html/videos/thumbnails/onceuponatime101125.jpg
/home/public_html/videos/thumbnails/playbutton.png
/home/public_html/videos/thumbnails/ReesewitherspoonCIA101202.jpg
/home/public_html/videos/thumbnails/snagItNarration.jpg
/home/public_html/videos/thumbnails/Minissha-Lamba.jpg
/home/public_html/videos/thumbnails/Lindsaydance101201.jpg
/home/public_html/videos/thumbnails/Mileyphoto101203.jpg

6. 在 Linux 中解压 tar.bz2 存档文件

解压缩高度压缩的tar.bz2文件,只需使用以下命令。下面的示例命令将解压所有.flv存档文件中的文件。

#tar-xvfvideos-14-09-12.tar.bz2

/home/public_html/videos/flv/katrinabarbiedoll231110.flv
/home/public_html/videos/flv/BrookmuellerCIA101125.flv
/home/public_html/videos/flv/dollybackinbb4101125.flv
/home/public_html/videos/flv/JenniferRobert101130.flv
/home/public_html/videos/flv/JustinAwardmovie101125.flv
/home/public_html/videos/flv/Lakme-Fashion-Week.flv
/home/public_html/videos/flv/Mileyphoto101203.flv
/home/public_html/videos/flv/Minissha-Lamba.flv

7. 列出 Linux 中 tar 存档文件的内容

要列出tar存档文件的内容,只需运行以下带有选项的命令t(list content)。下面的命令将列出内容uploadprogress.tar文件。

#tar-tvfuploadprogress.tar

-rw-r--r--chregu/staff22762011-08-151810package2.xml
-rw-r--r--chregu/staff78772011-08-151810uploadprogress/examples/index.php
-rw-r--r--chregu/staff16852011-08-151810uploadprogress/examples/server.php
-rw-r--r--chregu/staff16972011-08-151810uploadprogress/examples/info.php
-rw-r--r--chregu/staff3672011-08-151810uploadprogress/config.m4
-rw-r--r--chregu/staff3032011-08-151810uploadprogress/config.w32
-rw-r--r--chregu/staff35632011-08-151810uploadprogress/php_uploadprogress.h
-rw-r--r--chregu/staff154332011-08-151810uploadprogress/uploadprogress.c
-rw-r--r--chregu/staff14332011-08-151810package.xml

8. 在 Linux 中列出内容 tar.gz 存档文件

使用以下命令列出内容tar.gz文件。

#tar-tvfstaging.rumenz.com.tar.gz

-rw-r--r--root/root02012-08-300457staging.rumenz.com-access_log
-rw-r--r--root/root5872012-08-291812staging.rumenz.com-access_log.1
-rw-r--r--root/root1562012-01-210756staging.rumenz.com-access_log.2
-rw-r--r--root/root1562011-12-211156staging.rumenz.com-access_log.3
-rw-r--r--root/root1562011-11-201724staging.rumenz.com-access_log.4
-rw-r--r--root/root02012-08-300457staging.rumenz.com-error_log
-rw-r--r--root/root39812012-08-291812staging.rumenz.com-error_log.1
-rw-r--r--root/root2112012-01-210756staging.rumenz.com-error_log.2
-rw-r--r--root/root2112011-12-211156staging.rumenz.com-error_log.3
-rw-r--r--root/root2112011-11-201724staging.rumenz.com-error_log.4

9. Linux 中列出内容 tar.bz2 存档文件

列出内容tar.bz2文件,发出以下命令。

#tar-tvfPhpfiles-org.tar.bz2

drwxr-xr-xroot/root02012-09-150308/home/php/
-rw-r--r--root/root17512012-09-150308/home/php/iframe_ew.php
-rw-r--r--root/root112202012-09-150308/home/php/videos_all.php
-rw-r--r--root/root21522012-09-150308/home/php/rss.php
-rw-r--r--root/root30212012-09-150308/home/php/index.php
-rw-r--r--root/root25542012-09-150308/home/php/vendor.php
-rw-r--r--root/root4062012-09-150308/home/php/video_title.php
-rw-r--r--root/root41162012-09-150308/home/php/report.php
-rw-r--r--root/root12732012-09-150308/home/php/object.html

10. 在 Linux 中从 tar 文件解压单个文件

提取名为的单个文件cleanfiles.sh从cleanfiles.sh.tar使用以下命令。

#tar-xvfcleanfiles.sh.tarcleanfiles.sh
OR
#tar--extract--file=cleanfiles.sh.tarcleanfiles.sh

cleanfiles.sh

11. 在 Linux 中从 tar.gz 文件解压单个文件

提取单个文件rumenzbackup.xml来自rumenzbackup.tar.gz归档文件,使用命令如下。

#tar-zxvfrumenzbackup.tar.gzrumenzbackup.xml
OR
#tar--extract--file=rumenzbackup.tar.gzrumenzbackup.xml

rumenzbackup.xml

12.Linux 下 tar.bz2 文件解压单个文件

提取名为的单个文件index.php从文件Phpfiles-org.tar.bz2使用以下选项。

#tar-jxvfPhpfiles-org.tar.bz2home/php/index.php
OR
#tar--extract--file=Phpfiles-org.tar.bz2/home/php/index.php

/home/php/index.php

13. 从 tar、tar.gz 和 tar.bz2 文件中解压多个文件

从文件夹中提取或解压多个文件tar,tar.gz,和tar.bz2存档文件。例如,下面的命令将提取file 1file 2来自存档文件。

#tar-xvfrumenz-14-09-12.tar"file1""file2"

#tar-zxvfMyImages-14-09-12.tar.gz"file1""file2"

#tar-jxvfPhpfiles-org.tar.bz2"file1""file2"

14. 在 Linux 中使用通配符提取文件组

为了提取一组文件,我们使用基于通配符的提取。例如,提取一组模式以.php从一个tar, tar.gz, and tar.bz2存档文件。

#tar-xvfPhpfiles-org.tar--wildcards'*.php'

#tar-zxvfPhpfiles-org.tar.gz--wildcards'*.php'

#tar-jxvfPhpfiles-org.tar.bz2--wildcards'*.php'

/home/php/iframe_ew.php
/home/php/videos_all.php
/home/php/rss.php
/home/php/index.php
/home/php/vendor.php
/home/php/video_title.php
/home/php/report.php
/home/php/video.php

15. 在 Linux 中将文件或目录添加到 tar 归档文件

要将文件或目录添加到现有的 tar 存档文件,我们使用选项r(append)。比如我们添加文件xyz.txt和目录php对现有的rumenz-14-09-12.tar存档文件。

#tar-rvfrumenz-14-09-12.tarxyz.txt

#tar-rvfrumenz-14-09-12.tarphp

drwxr-xr-xroot/root02012-09-150221home/rumenz/
-rw-r--r--root/root157406152012-09-150242home/rumenz/cleanfiles.sh
-rw-r--r--root/root8637262012-09-150241home/rumenz/openvpn-2.1.4.tar.gz
-rw-r--r--root/root210636802012-09-150221home/rumenz/rumenz-14-09-12.tar
-rw-r--r--root/root44376002012-09-150241home/rumenz/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm
-rw-r--r--root/root126802012-09-150241home/rumenz/rpmforge-release-0.5.2-2.el5.rf.i386.rpm
-rw-r--r--root/root02012-08-181904xyz.txt
drwxr-xr-xroot/root02012-09-150308php/
-rw-r--r--root/root17512012-09-150308php/iframe_ew.php
-rw-r--r--root/root112202012-09-150308php/videos_all.php
-rw-r--r--root/root21522012-09-150308php/rss.php
-rw-r--r--root/root30212012-09-150308php/index.php
-rw-r--r--root/root25542012-09-150308php/vendor.php
-rw-r--r--root/root4062012-09-150308php/video_title.php

16. 将文件或目录添加到 tar.gz 和 tar.bz2 文件

tar 命令没有将文件或目录添加到现有压缩文件的选项tar.gz和tar.bz2存档文件。如果我们尝试会得到以下错误。

#tar-rvfMyImages-14-09-12.tar.gzxyz.txt

#tar-rvfPhpfiles-org.tar.bz2xyz.txt

tar:Thisdoesnotlooklikeatararchive
tar:Skippingtonextheader
xyz.txt
tar:Errorexitdelayedfrompreviouserrors

17. 如何验证 tar、tar.gz 和 tar.bz2 存档文件

要验证任何 tar 或压缩存档文件,我们使用该选项W(verify)。为此,只需使用以下命令示例。(Note:您不能对压缩的 (*.tar.gz, *.tar.bz2) 存档文件)。

#tartvfWrumenz-14-09-12.tar

tar:Thisdoesnotlooklikeatararchive
tar:Skippingtonextheader
tar:Archivecontainsobsolescentbase-64headers
tar:VERIFYFAILURE:30740invalidheadersdetected
Verify-rw-r--r--root/root8637262012-09-150241/home/rumenz/openvpn-2.1.4.tar.gz
Verify-rw-r--r--root/root210636802012-09-150221/home/rumenz/rumenz-14-09-12.tar
tar:/home/rumenz/rumenz-14-09-12.tar:Warning:Cannotstat:Nosuchfileordirectory
Verify-rw-r--r--root/root44376002012-09-150241home/rumenz/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm
tar:/home/rumenz/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm:Warning:Cannotstat:Nosuchfileordirectory
Verify-rw-r--r--root/root126802012-09-150241home/rumenz/rpmforge-release-0.5.2-2.el5.rf.i386.rpm
tar:/home/rumenz/rpmforge-release-0.5.2-2.el5.rf.i386.rpm:Warning:Cannotstat:Nosuchfileordirectory
Verify-rw-r--r--root/root02012-08-181904xyz.txt
Verifydrwxr-xr-xroot/root02012-09-150308php/

18. 检查 tar、tar.gz 和 tar.bz2 存档文件的大小

tar,tar.gz,和tar.bz2存档文件,使用以下命令。例如,下面的命令将以千字节(KB)。

#tar-czf-rumenz-14-09-12.tar|wc-c
12820480

#tar-czf-MyImages-14-09-12.tar.gz|wc-c
112640

#tar-czf-Phpfiles-org.tar.bz2|wc-c
20480

审核编辑:汤梓红

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • Linux
    +关注

    关注

    87

    文章

    10991

    浏览量

    206735
  • 压缩
    +关注

    关注

    2

    文章

    99

    浏览量

    19259
  • 文件
    +关注

    关注

    1

    文章

    540

    浏览量

    24402
  • 命令
    +关注

    关注

    5

    文章

    638

    浏览量

    21849

原文标题:Linux中的18个tar命令示例

文章出处:【微信号:良许Linux,微信公众号:良许Linux】欢迎添加关注!文章转载请注明出处。

收藏 人收藏

    评论

    相关推荐

    Linux教程:Linux Tee命令真实示例详细指南

    使用 Linux 终端时,您可能需要执行命令将一些数据写入文件并进一步处理其输出。命令是一个可以帮助您实现这一目标的强大实用程序tee。 这篇文章将为您提供有关tee命令的详细指南,并
    发表于 10-31 10:02 442次阅读

    linux中的压缩命令

    linux中主要有.zip,.gz,.bz2及.tar.gz和.tar.bz2等压缩格式。
    发表于 01-04 09:48 530次阅读

    Linux tcpdump命令示例 如何在Linux中安装tcpdump

    在这里,我们将展示如何安装 tcpdump,然后讨论并介绍一些有用的命令以及它们的实际示例
    发表于 08-06 11:11 2388次阅读
    <b class='flag-5'>Linux</b> tcpdump<b class='flag-5'>命令</b><b class='flag-5'>示例</b> 如何在<b class='flag-5'>Linux</b>中安装tcpdump

    必看!树莓派常用的Linux命令

    命令是Debian linux系统用的,功能很强例如:sudo apt-get update //升级列表的软件包sudo apt-get upgrade//升级所有已安装的软件sudo
    发表于 09-15 14:22

    ubuntutar命令解压出错??

    ubuntutar命令解压出错?这是什么情况怎么都不能解压呢??
    发表于 09-23 14:19

    Linux进阶命令详解

    。可能有不对的地方,欢迎指正。第001节_ find查找文本我们在Windows搜索文件,一般查找文件需要传入两条件:1)在那些目录查找;2)查找的内容;在Linux
    发表于 03-16 09:45

    面试linux常见的20命令

    查看文件尾10行tail -n 10 example.txt18.查看日志文件tail -f exmaple.log//这个命令会自动显示新增内容,屏幕只显示10行内容的(可设置)。19.启动Vi编辑器
    发表于 07-04 07:54

    20常用Linux命令

    20常用Linux命令 今天总结几个非常常用的Linux命令,其中有几个在面试很可能问相关
    发表于 05-11 15:46

    虚拟机:Linux查看tar包文件内容的方法

    虚拟机:Linux查看tar包文件内容的方法
    的头像 发表于 06-22 16:49 6258次阅读

    如何写Linux下的tar打包软件?

    相信你对 linux 的 .tar.gz 有点熟悉,这就是先 tar 打包(.tar 后缀),再对此 tar 文件用 gzip 压缩(.
    的头像 发表于 03-10 10:44 1269次阅读

    Linux tar命令高级用法——备份数据

    Linux上有功能强大的tar命令tar最初是为了制作磁带备份(tape archive)而设计的,它的作用是把文件和目录备份到磁带中,然后从磁带中提取或恢复文件。现在我们可以
    发表于 11-21 09:21 797次阅读

    如何在Linux使用tar命令创建/提取tar文件

    `tar`命令允许您创建,提取,解压缩tar存档文件。它支持各种压缩程序,例如`gzip`,`bzip2`,`lzip`,`lzma`,`lzop`,`xz`和`compress`压缩程序。
    的头像 发表于 12-05 16:37 3482次阅读

    如何在Linux上使用粘贴命令

    中粘贴命令的各种选项和使用场景,并通过示例和输出演示如何使用它。 一、什么是粘贴命令? Paste命令是Unix和Linux操作系统上的一个
    的头像 发表于 02-08 16:58 1449次阅读

    分享18个实用Linux运维命令及知识

    查找当前目录下所有以.tar结尾的文件然后移动到指定目录。
    的头像 发表于 04-03 14:06 296次阅读

    一款运行于windows上的linux命令神器-Cmder

    Cmder是一款Windows的命令行工具,用起来和Linux命令一样顺畅。可以使用大量的Linux 命令,比如 grep, curl,v
    的头像 发表于 05-05 10:30 1601次阅读
    一款运行于windows上的<b class='flag-5'>linux</b><b class='flag-5'>命令</b>神器-Cmder