15+ tar command usages with examples – Unix/Linux--reference
reference :http://crybit.com/tar-command-usages-with-examples/
The ‘tar’ saves many files together into a single tape or disk archive, and can restore individual files from the archive. It is very useful in such conditions like when we want to send a lot of files via email, transfer files from one machine to another etc. Here I am explaining some common and useful switches and it usages with examples.
Syntax:
# tar [options] file.tar file1 file2 .. .. ..
Where file.tar is the tar file and file1 and file2 .. .. are the files to make a tar.
I have created two files file1.txt and file2.txt for making examples.
[root@localhost TAR]# ll
total 8
-rw-r--r--. 1 root root 2770 Feb 7 22:37 file1.txt
-rw-r--r--. 1 root root 887 Feb 7 22:38 file2.txt
Common usages of tar command:
How to create a tar file ?
Syntax:
# tar -cf archive.tar files .. ..
Example:
[root@localhost TAR]# tar -cf file.tar file1.txt file2.txt
[root@localhost TAR]# ll file.tar
-rw-r--r--. 1 root root 10240 Feb 7 22:42 file.tar
How to list all files in an archive.tar ?
# tar -tf archive.tar
Example:
[root@localhost TAR]# tar -tf file.tar
file1.txt
file2.txt
How to extract all files from archive.tar ?
tar -xf archive.tar
Example:
[root@localhost TAR]# tar -xf file.tar
[root@localhost TAR]# ll
total 20
-rw-r--r--. 1 root root 2770 Feb 7 22:37 file1.txt
-rw-r--r--. 1 root root 887 Feb 7 22:38 file2.txt
-rw-r--r--. 1 root root 10240 Feb 7 22:42 file.tar
Switches with example:
1, -v, –verbose
verbosely list files processed:
Syntax:
List all files in an archive.tar verbosely:
tar -tvf archive.tar
Example:
[root@localhost TAR]# tar -tvf file.tar
-rw-r--r-- root/root 2770 2014-02-07 22:37 file1.txt
-rw-r--r-- root/root 887 2014-02-07 22:38 file2.txt
2, -c, –create
create a new archive.
3, -t, –list
list the contents of an archive.
4, -x, –extract, –get
extract files from an archive.
5, -d, –diff, –compare
find differences between archive and file system.
Example:
[root@localhost TAR]# tar -tf file.tar
file2.txt
file3.txt
file1.txt
[root@localhost TAR]# tar -df file.tar file1.txt file2.txt file4.txt
tar: file4.txt: Not found in archive
tar: Exiting with failure status due to previous errors
----Verbosely----
[root@localhost TAR]# tar -dvf file.tar file1.txt file2.txt
file2.txt
file1.txt
[root@localhost TAR]# tar -dvf file.tar file1.txt file2.txt file6.txt
file2.txt
file1.txt
tar: file6.txt: Not found in archive
tar: Exiting with failure status due to previous errors
6, –delete
delete from the archive (not on mag tapes!)
Example:
Delete file1.txt from the archive file.tar
[root@localhost TAR]# tar --delete -f file.tar file1.txt
[root@localhost TAR]# tar -tf file.tar
file2.txt
7, -r, –append
Append files to the end of an archive.
Example:
Append file3.txt to file.tar
[root@localhost TAR]# tar -rf file.tar file3.txt
[root@localhost TAR]# tar -tf file.tar
file1.txt
file2.txt
file3.txt
8, -A, –catenate, –concatenate
Append tar files to an archive.
Create another tar file
[root@localhost TAR]# tar -cf archive.tar file1.txt file3.txt
Append tar file to an archive.
[root@localhost TAR]# tar -Af file.tar archive.tar
[root@localhost TAR]# tar -tf file.tar
file2.txt
file3.txt
file1.txt
file1.txt
file3.txt
9, –test-label
test the archive volume label and exit.
10, -u, –update
Only append files newer than copy in archive.
Example:
[root@localhost TAR]# tar -tf file.tar
file1.txt
file2.txt
[root@localhost TAR]# tar -uf file.tar file1.txt file3.txt file2.txt
[root@localhost TAR]# tar -tf file.tar
file1.txt
file2.txt
file3.txt
11, -C, –directory=DIR
Change to directory DIR.
Example:
Extract files to another directory:
[root@localhost TAR]# tar -xvf file.tar -C /root/TAR2
file1.txt
file2.txt
[root@localhost TAR]# cd -
/root/TAR2
[root@localhost TAR2]# ll
total 28
-rw-r--r--. 1 root root 23250 Feb 7 23:11 file1.txt
-rw-r--r--. 1 root root 887 Feb 7 22:38 file2.txt
12, -p, –preserve-permissions
Extract information about file permissions (default for superuser)
Create archive with compression:
It is very helpful to make an archive of files which has comparatively large size. Commonly using compression methods are “BZIP” and “GZIP”.
Switches with examples, compression related.
13, -j, –bzip2
filter the archive through bzip2
Example:
[root@localhost TAR]# tar -jcf file.tar.bz file2.txt file1.txt
[root@localhost TAR]# ll
total 128
-rw-r--r--. 1 root root 23250 Feb 7 23:11 file1.txt
-rw-r--r--. 1 root root 887 Feb 7 22:38 file2.txt
-rw-r--r--. 1 root root 30720 Feb 7 23:30 file.tar
-rw-r--r--. 1 root root 1797 Feb 7 23:42 file.tar.bz
See, the tar file size is decreased to 1797 with BZIP
14, -z, –gzip
filter the archive through gzip
Example:
[root@localhost TAR]# tar -zcf file.tar.gz file2.txt file1.txt
[root@localhost TAR]# ll
total 132
-rw-r--r--. 1 root root 23250 Feb 7 23:11 file1.txt
-rw-r--r--. 1 root root 887 Feb 7 22:38 file2.txt
-rw-r--r--. 1 root root 30720 Feb 7 23:30 file.tar
-rw-r--r--. 1 root root 1797 Feb 7 23:42 file.tar.bz
-rw-r--r--. 1 root root 1673 Feb 7 23:45 file.tar.gz
That’s it!!
Other useful commands:
groupdel, groupmems, groupmod, useradd, usermod, chgrp, chown, ls, head, tail, top, ps, find,crontab, ftp commands
15+ tar command usages with examples – Unix/Linux--reference的更多相关文章
- 10+ commonly using find command switches with example Unix/Linux
http://crybit.com/find-command-usage-with-example-unixlinux/ find command is one of the best search ...
- 20+ Rsync command’s switches and common usages with examples – Unix/Linux--reference
reference:http://crybit.com/rsync-commands-switches/ The “rsync” is a powerful command under the Lin ...
- 15 Practical Grep Command Examples In Linux / UNIX
You should get a grip on the Linux grep command. This is part of the on-going 15 Examples series, wh ...
- 18 Tar Command Examples in Linux
FROM: http://www.tecmint.com/18-tar-command-examples-in-linux/ 18 Tar Command Examples in Linux By R ...
- 15 Basic ‘ls’ Command Examples in Linux
FROM: http://www.tecmint.com/15-basic-ls-command-examples-in-linux/ ls command is one of the most fr ...
- 13 Basic Cat Command Examples in Linux(转) Linux中cat命令的13中基本用法
Cat (串联) 命令是Linux/Unix开源系统中比较常用的一个命令.我们可以通过Cat命令创建一个或多个文件,查看文件内容,串联文件并将内容输出到终端设备或新的文件当中,这篇文章我们将会以实例的 ...
- 13 Basic Cat Command Examples in Linux
FROM: http://www.tecmint.com/13-basic-cat-command-examples-in-linux/ The cat (short for “concatenate ...
- 50个最常用的UNIX/Linux命令
转自http://get.jobdeer.com/493.get 1. tar command examples Create a new tar archive. $ tar cvf archive ...
- 如何使用Unix/Linux grep命令——磨刀不误砍柴工系列
http://man.linuxde.net/grep ---------------------------------------------------- 如何使用Unix/Linux gre ...
随机推荐
- Compiling Xen-4.4 From Source And Installing It On Ubuntu Server (Amd-64)
First of all, you should install a clean Ubuntu Server (Amd-64) on your server. (Version 14.04 is st ...
- Java基础 —— 概述
Java语言: JDK(Java Development Kit)开发工具包,提供Java的开发环境和运行环境 --> 适合于开发 JRE(Java Runtime Environment)Ja ...
- Java实现中文字符串的排序功能
package test; /** * * @Title 书的信息类 * @author LR * @version 1.0 * @since 2016-04-21 */ public class B ...
- php 扩展apc 参数优化
编辑php.ini 输入下面 [apc] apc.enabled=1apc.shm_segments = 1apc.shm_size = 1Gapc.ttl = 0apc.user_ttl = 0ap ...
- 麒麟OS剽窃
今年对于我们的IT行业来说可以算是耻辱的一年. 首先是“汉芯丑闻”,上海交大研制了一个所谓的国内第一个完全拥有自主知识产 权的DSP芯片(数字信号微处理器)——“汉芯”,研制人陈进教授以此领取政府一亿 ...
- 第三百四十二天 how can I 坚持
再问世间都去哪儿了,天气预报没搞完,计划没制定,又周三了. 今天回到家八点,吃完饭接近九点,和老妈开了会视频,这就九点半多了,发了呆洗了个碗就到这时候了,整天浑浑噩噩的,该如何是好. 又有点上火,舌头 ...
- Codeforces 467C. George and Job (dp)
题目链接:http://codeforces.com/contest/467/problem/C 求k个不重叠长m的连续子序列的最大和. dp[i][j]表示第i个数的位置个序列的最大和. 前缀和一下 ...
- How Tomcat Works(四)
Servlet容器有两个主要的模块,即连接器(connector)与容器(container),本文接下来创建一个连接器来增强前面文章中的应用程序的功能,以一种更优雅的方式来创建request对象和r ...
- 用html5的canvas生成图片并保存到本地
原文:http://www.2cto.com/kf/201209/156169.html 前端的代码: [javascript] function drawArrow(angle) { ...
- C:基本语句
基本语句知识 do{}while(); 与 while()do{}:for :while 语句的区别: while()do{}:先判断条件是否成立,条件满足则执行循环体 do{}while();是先执 ...