压缩和解压缩文件tar, tar.gz and tar.bz2
1, 对于tar.gz
压缩:tar -zcvf archive-name.tar.gz directory-name
解压:tar -zxvf prog-1-jan-2005.tar.gz -C /tmp
http://www.tecmint.com/18-tar-command-examples-in-linux/
The Linux “tar” stands for tape archive, which is used by large number of Linux/Unix system administrators to deal with tape drives backup. The tar command used to rip a collection of files and directories into highly compressed archive file commonly called tarball or tar, gzip andbzip in Linux. The tar is most widely used command to create compressed archive files and that can be moved easily from one disk to anther disk or machine to machine.
In this article we will going to review and discuss various tar command examples including how to create archive files using (tar, tar.gz and tar.bz2) compression, how to extract archive file, extract a single file, view content of file, verify a file, add files or directories to archive file, estimate the size of tar archive file, etc.
The main purpose of this guide is to provide various tar command examples that might be helpful you to understand and become expert in tar archive manipulation.
1. Create tar Archive File
The below example command will create a tar archive file tecmint-14-09-12.tar for a directory /home/tecmint in current working directory. See the example command in action.
# tar -cvf tecmint-14-09-12.tar /home/tecmint/ /home/tecmint/
/home/tecmint/cleanfiles.sh
/home/tecmint/openvpn-2.1.4.tar.gz
/home/tecmint/tecmint-14-09-12.tar
/home/tecmint/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm
/home/tecmint/rpmforge-release-0.5.2-2.el5.rf.i386.rpm
Let’s discuss the each option we have used in the above command for creating tar archive file.
- c – Creates a new .tar archive file.
- v – Verbosely show the .tar file progress.
- f – File name type of the archive file.
2. Create tar.gz Archive File
To create a compressed gzip archive file we use the option as z. For example the below command will create a compressed MyImages-14-09-12.tar.gz file for the directory/home/MyImages. (Note : tar.gz and tgz both are similar).
# tar cvzf MyImages-14-09-12.tar.gz /home/MyImages
OR
# tar cvzf MyImages-14-09-12.tgz /home/MyImages /home/MyImages/
/home/MyImages/Sara-Khan-and-model-Priyanka-Shah.jpg
/home/MyImages/RobertKristenviolent101201.jpg
/home/MyImages/Justintimerlakenaked101125.jpg
/home/MyImages/Mileynudephoto101203.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. Create tar.bz2 Archive File
The bz2 feature compress and create archive file less than the size of the gzip. The bz2compression takes more time to compress and decompress files as compared to gzip which takes less time. To create highly compressed tar file we use option as j. The following example of command will create a Phpfiles-org.tar.bz2 file for a directory /home/php. (Note: tar.bz2 and tbz is similar as tb2).
# tar cvfj Phpfiles-org.tar.bz2 /home/php
OR
# tar cvfj Phpfiles-org.tar.tbz /home/php
OR
# tar cvfj Phpfiles-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. Untar tar Archive File
To untar or extract a tar file, just issue following command using option x (extract). For example the below command will untar the file public_html-14-09-12.tar in present working directory. If you want untar in a different directory then use option as -C (specified directory).
## Untar files in Current Directory ##
# tar -xvf public_html-14-09-12.tar ## Untar files in specified Directory ##
# tar -xvf public_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. Uncompress tar.gz Archive File
To Uncompress tar.gz archive file, just run following command. If would like to untar in different directory just use option -C and the path of the directory, like we shown in the above example.
# tar -xvf thumbnails-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/Mileynudephoto101203.jpg
6. Uncompress tar.bz2 Archive File
To Uncompress highly compressed tar.bz2 file, just use the following command. The below example command will untar all the .flv files from the archive file.
# tar -xvf videos-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/Mileynudephoto101203.flv
/home/public_html/videos/flv/Minissha-Lamba.flv
7. List Content of tar Archive File
To list the content of tar archive file, just run the following command with option t (list content). The below command will list the content of uploadprogress.tar file.
# tar -tvf uploadprogress.tar -rw-r--r-- chregu/staff 2276 2011-08-15 18:51:10 package2.xml
-rw-r--r-- chregu/staff 7877 2011-08-15 18:51:10 uploadprogress/examples/index.php
-rw-r--r-- chregu/staff 1685 2011-08-15 18:51:10 uploadprogress/examples/server.php
-rw-r--r-- chregu/staff 1697 2011-08-15 18:51:10 uploadprogress/examples/info.php
-rw-r--r-- chregu/staff 367 2011-08-15 18:51:10 uploadprogress/config.m4
-rw-r--r-- chregu/staff 303 2011-08-15 18:51:10 uploadprogress/config.w32
-rw-r--r-- chregu/staff 3563 2011-08-15 18:51:10 uploadprogress/php_uploadprogress.h
-rw-r--r-- chregu/staff 15433 2011-08-15 18:51:10 uploadprogress/uploadprogress.c
-rw-r--r-- chregu/staff 1433 2011-08-15 18:51:10 package.xml
8. List Content tar.gz Archive File
Use the following command to list the content of tar.gz file.
# tar -tvf staging.tecmint.com.tar.gz -rw-r--r-- root/root 0 2012-08-30 04:03:57 staging.tecmint.com-access_log
-rw-r--r-- root/root 587 2012-08-29 18:35:12 staging.tecmint.com-access_log.1
-rw-r--r-- root/root 156 2012-01-21 07:17:56 staging.tecmint.com-access_log.2
-rw-r--r-- root/root 156 2011-12-21 11:30:56 staging.tecmint.com-access_log.3
-rw-r--r-- root/root 156 2011-11-20 17:28:24 staging.tecmint.com-access_log.4
-rw-r--r-- root/root 0 2012-08-30 04:03:57 staging.tecmint.com-error_log
-rw-r--r-- root/root 3981 2012-08-29 18:35:12 staging.tecmint.com-error_log.1
-rw-r--r-- root/root 211 2012-01-21 07:17:56 staging.tecmint.com-error_log.2
-rw-r--r-- root/root 211 2011-12-21 11:30:56 staging.tecmint.com-error_log.3
-rw-r--r-- root/root 211 2011-11-20 17:28:24 staging.tecmint.com-error_log.4
9. List Content tar.bz2 Archive File
To list the content of tar.bz2 file, issue the following command.
# tar -tvf Phpfiles-org.tar.bz2 drwxr-xr-x root/root 0 2012-09-15 03:06:08 /home/php/
-rw-r--r-- root/root 1751 2012-09-15 03:06:08 /home/php/iframe_ew.php
-rw-r--r-- root/root 11220 2012-09-15 03:06:08 /home/php/videos_all.php
-rw-r--r-- root/root 2152 2012-09-15 03:06:08 /home/php/rss.php
-rw-r--r-- root/root 3021 2012-09-15 03:06:08 /home/php/index.php
-rw-r--r-- root/root 2554 2012-09-15 03:06:08 /home/php/vendor.php
-rw-r--r-- root/root 406 2012-09-15 03:06:08 /home/php/video_title.php
-rw-r--r-- root/root 4116 2012-09-15 03:06:08 /home/php/report.php
-rw-r--r-- root/root 1273 2012-09-15 03:06:08 /home/php/object.html
10. Untar Single file from tar File
To extract a single file called cleanfiles.sh from cleanfiles.sh.tar use the following command.
# tar -xvf cleanfiles.sh.tar cleanfiles.sh
OR
# tar --extract --file=cleanfiles.sh.tar cleanfiles.sh cleanfiles.sh
11. Untar Single file from tar.gz File
To extract a single file tecmintbackup.xml from tecmintbackup.tar.gz archive file, use the command as follows.
# tar -zxvf tecmintbackup.tar.gz tecmintbackup.xml
OR
# tar --extract --file=tecmintbackup.tar.gz tecmintbackup.xml tecmintbackup.xml
12. Untar Single file from tar.bz2 File
To extract a single file called index.php from the file Phpfiles-org.tar.bz2 use the following option.
# tar -jxvf Phpfiles-org.tar.bz2 home/php/index.php
OR
# tar --extract --file=Phpfiles-org.tar.bz2 /home/php/index.php /home/php/index.php
13. Untar Multiple files from tar, tar.gz and tar.bz2 File
To extract or untar multiple files from the tar, tar.gz and tar.bz2 archive file. For example the below command will extract “file 1” “file 2” from the archive files.
# tar -xvf tecmint-14-09-12.tar "file 1" "file 2" # tar -zxvf MyImages-14-09-12.tar.gz "file 1" "file 2" # tar -jxvf Phpfiles-org.tar.bz2 "file 1" "file 2"
14. Extract Group of Files using Wildcard
To extract a group of files we use wildcard based extracting. For example, to extract a group of all files whose pattern begins with .php from a tar, tar.gz and tar.bz2 archive file.
# tar -xvf Phpfiles-org.tar --wildcards '*.php' # tar -zxvf Phpfiles-org.tar.gz --wildcards '*.php' # tar -jxvf Phpfiles-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. Add Files or Directories to tar Archive File
To add files or directories to existing tar archived file we use the option r (append). For example we add file xyz.txt and directory php to existing tecmint-14-09-12.tar archive file.
# tar -rvf tecmint-14-09-12.tar xyz.txt # tar -rvf tecmint-14-09-12.tar php drwxr-xr-x root/root 0 2012-09-15 02:24:21 home/tecmint/
-rw-r--r-- root/root 15740615 2012-09-15 02:23:42 home/tecmint/cleanfiles.sh
-rw-r--r-- root/root 863726 2012-09-15 02:23:41 home/tecmint/openvpn-2.1.4.tar.gz
-rw-r--r-- root/root 21063680 2012-09-15 02:24:21 home/tecmint/tecmint-14-09-12.tar
-rw-r--r-- root/root 4437600 2012-09-15 02:23:41 home/tecmint/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm
-rw-r--r-- root/root 12680 2012-09-15 02:23:41 home/tecmint/rpmforge-release-0.5.2-2.el5.rf.i386.rpm
-rw-r--r-- root/root 0 2012-08-18 19:11:04 xyz.txt
drwxr-xr-x root/root 0 2012-09-15 03:06:08 php/
-rw-r--r-- root/root 1751 2012-09-15 03:06:08 php/iframe_ew.php
-rw-r--r-- root/root 11220 2012-09-15 03:06:08 php/videos_all.php
-rw-r--r-- root/root 2152 2012-09-15 03:06:08 php/rss.php
-rw-r--r-- root/root 3021 2012-09-15 03:06:08 php/index.php
-rw-r--r-- root/root 2554 2012-09-15 03:06:08 php/vendor.php
-rw-r--r-- root/root 406 2012-09-15 03:06:08 php/video_title.php
16. Add Files or Directories to tar.gz and tar.bz2 files
The tar command don’t have a option to add files or directories to a existing compressed tar.gzand tar.bz2 archive file. If we do try will get tbe following error.
# tar -rvf MyImages-14-09-12.tar.gz xyz.txt # tar -rvf Phpfiles-org.tar.bz2 xyz.txt tar: This does not look like a tar archive
tar: Skipping to next header
xyz.txt
tar: Error exit delayed from previous errors
17. How To Verify tar, tar.gz and tar.bz2 Archive File
To verfify any tar or compressed archived file we use option as W (verify). To do, just use the following examples of command. (Note : You cannot do verification on a compressed ( *.tar.gz, *.tar.bz2 ) archive file).
# tar tvfW tecmint-14-09-12.tar tar: This does not look like a tar archive
tar: Skipping to next header
tar: Archive contains obsolescent base-64 headers
tar: VERIFY FAILURE: 30740 invalid headers detected
Verify -rw-r--r-- root/root 863726 2012-09-15 02:23:41 /home/tecmint/openvpn-2.1.4.tar.gz
Verify -rw-r--r-- root/root 21063680 2012-09-15 02:24:21 /home/tecmint/tecmint-14-09-12.tar
tar: /home/tecmint/tecmint-14-09-12.tar: Warning: Cannot stat: No such file or directory
Verify -rw-r--r-- root/root 4437600 2012-09-15 02:23:41 home/tecmint/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm
tar: /home/tecmint/phpmyadmin-2.11.11.3-1.el5.rf.noarch.rpm: Warning: Cannot stat: No such file or directory
Verify -rw-r--r-- root/root 12680 2012-09-15 02:23:41 home/tecmint/rpmforge-release-0.5.2-2.el5.rf.i386.rpm
tar: /home/tecmint/rpmforge-release-0.5.2-2.el5.rf.i386.rpm: Warning: Cannot stat: No such file or directory
Verify -rw-r--r-- root/root 0 2012-08-18 19:11:04 xyz.txt
Verify drwxr-xr-x root/root 0 2012-09-15 03:06:08 php/
18. Check the Size of the tar, tar.gz and tar.bz2 Archive File
To check the size of any tar, tar.gz and tar.bz2 archive file, use the following command. For example the below command will display the size of archvie file in Kilobytes (KB).
# tar -czf - tecmint-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
Tar Usage and Options
- c – create a archive file.
- x – extract a archive file.
- v – show the progress of archive file.
- f – filename of archive file.
- t – viewing content of archive file.
- j – filter archive through bzip2.
- z – filter archive through gzip.
- r – append or update files or directories to existing archive file.
- W – Verify a archive file.
- wildcards – Specify patters in unix tar command.
That’s it for now, hope the above tar command examples are enough for you to learn and for more information please use man tar command. If we’ve missed any example please do share with us via comment box and please don’t forget to share this article with your friends. This is the best way to say thanks…..
压缩和解压缩文件tar, tar.gz and tar.bz2的更多相关文章
- Linux常用命令学习3---(文件的压缩和解压缩命令zip unzip tar、关机和重启命令shutdown reboot……)
1.压缩和解压缩命令 常用压缩格式:.zip..gz..bz2..tar.gz..tar.bz2..rar .zip格式压缩和解压缩命令 zip 压缩文件名 源文件:压缩文件 ...
- 【转载】linux 压缩和解压缩命令gz、tar、zip、bz2
linux系统下压缩解压缩很让人头大,每次都要查命令.转载下方便以后查阅.原文信息如下: 作者:capecape 来源:CSDN 原文:https://blog.csdn.net/capecape/a ...
- gz、tar、zip、bz2压缩和解压缩命令
gzip 压缩后的格式为:*.gz 这种压缩方式不能保存原文件:且不能压缩目录 命令举例:#压缩[root@localhost tmp]# gzip buodo[root@localhost tmp] ...
- tar 压缩和解压缩使用笔记
tar 压缩和解压缩使用笔记 1 文件 1.1 打包 1.1 压缩 $ tar czf myfile.txt.tar.gz ./myfile.txt 1.2 解压缩 解压缩到目录: $ mkdir o ...
- Ubuntu终端文件的压缩和解压缩命令
在Ubntu的终端中压缩和解压缩是每天几乎要用到的命令,由于linux中各种压缩文件类型较多,所以需要记住几个主要的压缩和解压缩命令: 文件类型 执行动作 命令 .tar 解包 tar xvf Fil ...
- Linux文件压缩和解压缩命令
Linux文件压缩和解压缩命令: tar 命令(打包并压缩的话,原文件也会默认存在) -c 建立打包档案 -x 解包 -t 查看包里的类容 -r 向包里追加文件 -v 显示打包过程 -f 文件 比如: ...
- C#压缩和解压文件
这里用两种方法实现C#压缩和解压文件 1.使用System.IO.Compression名称空间下的相关类(需引用 System.IO.Compression.FileSystem和System.IO ...
- 使用commons-compress操作zip文件(压缩和解压缩)
http://www.cnblogs.com/luxh/archive/2012/06/28/2568758.html Apache Commons Compress是一个压缩.解压缩文件的类库. 可 ...
- [Java 基础] 使用java.util.zip包压缩和解压缩文件
reference : http://www.open-open.com/lib/view/open1381641653833.html Java API中的import java.util.zip ...
- 重新想象 Windows 8 Store Apps (70) - 其它: 文件压缩和解压缩, 与 Windows 商店相关的操作, app 与 web, 几个 Core 的应用, 页面的生命周期和程序的生命周期
[源码下载] 重新想象 Windows 8 Store Apps (70) - 其它: 文件压缩和解压缩, 与 Windows 商店相关的操作, app 与 web, 几个 Core 的应用, 页面的 ...
随机推荐
- nginx下目录浏览及其验证功能、版本隐藏等配置记录
工作中常常有写不能有网页下载东西的需求,在Apache下搭建完成后直接导入文件即可达到下载/显示文件的效果;而Nginx的目录列表功能默认是关闭的,如果需要打开Nginx的目录列表功能,需要手动配置, ...
- HDU 3537 Daizhenyang's Coin
链接 [http://acm.hdu.edu.cn/showproblem.php?pid=3537] 题意 题意:已知一排硬币中有n个硬币正面朝上,输入正面朝上的硬币的位置ai.两人轮流操作, 每次 ...
- 《Linux内核分析》实践4
<Linux内核分析> 实践四--ELF文件格式分析 20135211李行之 一.概述 1.ELF全称Executable and Linkable Format,可执行连接格式,ELF格 ...
- 分布式版本控制系统Git的安装与使用 第二次作业
(本次作业要求来自:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2103) 一.安装Git bash软件和安装notepad++ 二 ...
- Daemon Thread [shirocache] (Running)
shiro cache的原理 https://cwiki.apache.org/confluence/display/SHIRO/Caching shirocache这个线程相关的内容
- TCP 三次握手理解和过程
1:TCP为什么要三次握手,不是两次四次? 2:TCP协议三次握手过程分析
- 【转帖】互联网加密及OpenSSL介绍和简单使用
转帖:https://mritd.me/2016/07/02/%E4%BA%92%E8%81%94%E7%BD%91%E5%8A%A0%E5%AF%86%E5%8F%8AOpenSSL%E4%BB%8 ...
- leetcode:Reverse Integer 及Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- 14.5 富文本编辑【JavaScript高级程序设计第三版】
富文本编辑,又称为WYSIWYG(What You See Is What You Get,所见即所得).在网页中编辑富文本内容,是人们对Web 应用程序最大的期待之一.虽然也没有规范,但在IE 最早 ...
- Hadoop源码分析之FileSystem抽象文件系统
Hadopo提供了一个抽象的文件系统模型FileSystem,HDFS是其中的一个实现. FileSystem是Hadoop中所有文件系统的抽象父类,它定义了文件系统所具有的基本特征和基本操作. Fi ...