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:
groupdelgroupmemsgroupmoduseraddusermodchgrpchownlsheadtailtoppsfind,crontabftp commands

15+ tar command usages with examples – Unix/Linux--reference的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 13 Basic Cat Command Examples in Linux(转) Linux中cat命令的13中基本用法

    Cat (串联) 命令是Linux/Unix开源系统中比较常用的一个命令.我们可以通过Cat命令创建一个或多个文件,查看文件内容,串联文件并将内容输出到终端设备或新的文件当中,这篇文章我们将会以实例的 ...

  7. 13 Basic Cat Command Examples in Linux

    FROM: http://www.tecmint.com/13-basic-cat-command-examples-in-linux/ The cat (short for “concatenate ...

  8. 50个最常用的UNIX/Linux命令

    转自http://get.jobdeer.com/493.get 1. tar command examples Create a new tar archive. $ tar cvf archive ...

  9. 如何使用Unix/Linux grep命令——磨刀不误砍柴工系列

     http://man.linuxde.net/grep ---------------------------------------------------- 如何使用Unix/Linux gre ...

随机推荐

  1. 轻松学习Linux之认识Shell

            Shell是一个命令解释器提供了用户与内核进行交互操作的一种接口shell,编程对於系统管理员或是一般使用者都相当有用,除了自动化,还可写出一些有趣的小工具,Linux本身有各种版本一 ...

  2. c++builder XE8 线程 Thread

    thread Thread  c++builder XE8 / RAD 10 Settle delphi TThread.CreateAnonymousThread(MyMethod).Start; ...

  3. 支付结果回调v7核心,投保确认接口..

    <?xml version="1.0" encoding="GBK"?> <PACKET type="REQUEST"&g ...

  4. css3妙用 刷新图标

    从猫眼电影看到的一个图标 .geopos i:before { content: ""; width: .8em; height: .8em; border: .14em soli ...

  5. RGPJS 教程之八 创造场景

    开始画面 游戏画面 代码 <!DOCTYPE html> <html> <head> <script src="rpg-beta-2.js" ...

  6. How Tomcat Works(十四)补充

    在How Tomcat Works(十四)中,本人并没有对javax.servlet.Filter及javax.servlet.FilterChain做详细的描述,本文在这里做一下补充 FilterC ...

  7. MySQL重置密码(OSX)

    1.停止MySQL的服务 sudo /Library/StartupItems/MySQLCOM/MySQLCOM stop   2.cd /usr/local/mysql/bin ./mysqld_ ...

  8. 使用XML与远程服务器进行交互

    最近在做的一个项目其中的一部分是与远程服务器进行交互,确定身份验证的合法性,于是编写了SendRequest方法 此方法发送给远程服务器XML请求,服务器经过处理后,返回XML回应,由此方法接收到后进 ...

  9. Odoo的Domain (一)

    Odoo 的Domain:多个条件的列表. 条件:(字段名,操作符,值)三元式(列表或者元组) 字段名:当前模型的字段或者是通过点操作符访问的Many2one/Many2Many对象,当是Many2M ...

  10. MySQL事务处理和锁机制

    事务处理和并发性 1.1 基础知识和相关概念 1 )全部的表类型都可以使用锁,但是只有 InnoDB 和 BDB 才有内置的事务功能. 2 )使用 begin 开始事务,使用 commit 结束事务, ...