首页 技术 正文
技术 2022年11月14日
0 收藏 360 点赞 2,541 浏览 4421 个字

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!! 15+ tar command usages with examples – Unix/Linux–reference

Other useful commands:
groupdelgroupmemsgroupmoduseraddusermodchgrpchownlsheadtailtoppsfind,crontabftp commands

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,023
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,360
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,774
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,852