-c 创建新的档案文件。如果用户想备份一个目录或是一些文件,就要选择这个选项。
[root@localhost test]# ls file [root@localhost test]# tar -cf file.tar file [root@localhost test]# ls file file.tar
-r 把要存档的文件追加到档案文件的末尾。例如用户已经做好备份文件,又发现还有一个目录或是一些文件忘记备份了,这时可以使用该选项,将忘记的目录或文件追加到备份文件中。
[root@localhost test]# ll total 56 -rw-r--r--. 1 root root 22898 Apr 10 22:34 file -rw-r--r--. 1 root root 30720 Apr 10 22:34 file.tar [root@localhost test]# cp file extra [root@localhost test]# tar -rf file.tar extra [root@localhost test]# ll total 100 -rw-r--r--. 1 root root 22898 Apr 10 22:36 extra -rw-r--r--. 1 root root 22898 Apr 10 22:34 file -rw-r--r--. 1 root root 51200 Apr 10 22:36 file.tar
-t 列出档案文件的内容,查看已经备份了哪些文件。
[root@localhost test]# tar -tvf file.tar -rw-r--r-- root/root 22898 2014-04-10 22:34 file -rw-r--r-- root/root 22898 2014-04-10 22:36 extra
-u 更新文件。就是说,用新增的文件取代原备份文件,如果在备份文件中找不到要更新的文件,则把它追加到备份文件的最后。
[root@localhost test]# cat extra >> file [root@localhost test]# ll total 124 -rw-r--r--. 1 root root 22898 Apr 10 22:36 extra -rw-r--r--. 1 root root 45796 Apr 10 22:39 file -rw-r--r--. 1 root root 51200 Apr 10 22:36 file.tar [root@localhost test]# tar -uf file.tar file [root@localhost test]# ll total 172 -rw-r--r--. 1 root root 22898 Apr 10 22:36 extra -rw-r--r--. 1 root root 45796 Apr 10 22:39 file -rw-r--r--. 1 root root 102400 Apr 10 22:40 file.tar
-x 从档案文件中释放文件。
[root@localhost test]# mv file file.bak [root@localhost test]# tar -xf file.tar [root@localhost test]# ls extra file file.bak file.tar
-m 在还原文件时,把所有文件的修改时间保持不变。
[root@localhost test]# stat file File: `file‘ Size: 45796 Blocks: 96 IO Block: 4096 regular file Device: fd00h/64768d Inode: 10474 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2014-04-10 22:45:54.435896574 +0800 Modify: 2014-04-10 22:45:54.439896465 +0800 Change: 2014-04-10 22:45:54.439896465 +0800 [root@localhost test]# tar cvf file2.tar file file [root@localhost test]# stat file File: `file‘ Size: 45796 Blocks: 96 IO Block: 4096 regular file Device: fd00h/64768d Inode: 10474 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2014-04-10 22:46:46.706898125 +0800 Modify: 2014-04-10 22:45:54.439896465 +0800 Change: 2014-04-10 22:45:54.439896465 +0800 [root@localhost test]# tar -xvf file2.tar file [root@localhost test]# stat file File: `file‘ Size: 45796 Blocks: 96 IO Block: 4096 regular file Device: fd00h/64768d Inode: 10633 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2014-04-10 22:47:45.674545593 +0800 Modify: 2014-04-10 22:45:54.000000000 +0800 Change: 2014-04-10 22:47:45.680896167 +0800
-v 详细报告tar处理的文件信息。如无此选项,tar不报告文件信息。
[root@localhost test]# tar -cvf file.tar file file [root@localhost test]# ls extra file file.tar
-j 代表使用‘bzip2’程序进行文件的压缩
[root@localhost test]# ls extra file [root@localhost test]# tar -cvjf file.tar.bz2 file file [root@localhost test]# ls extra file file.tar.bz2 [root@localhost test]# mv file file.bak [root@localhost test]# tar -xvjf file.tar.bz2 file [root@localhost test]# ls extra file file.bak file.tar.bz2
-z 用gzip来压缩/解压缩文件,加上该选项后可以将档案文件进行压缩,但还原时也一定要使用该选项进行解压缩。
[root@localhost test]# ls extra file [root@localhost test]# tar -zcvf file.tar.gz file file [root@localhost test]# ls extra file file.tar.gz [root@localhost test]# mv file file.bak [root@localhost test]# tar -zxvf file.tar.gz file [root@localhost test]# ls extra file file.bak file.tar.gz
将一存档与已有的存档合并
[root@localhost test]# ls extra file [root@localhost test]# tar -cvf file.tar file file [root@localhost test]# tar -cvf extra.tar extra extra [root@localhost test]# ll total 156 -rw-r--r--. 1 root root 22898 Apr 10 22:36 extra -rw-r--r--. 1 root root 30720 Apr 10 22:56 extra.tar -rw-r--r--. 1 root root 45796 Apr 10 22:45 file -rw-r--r--. 1 root root 51200 Apr 10 22:56 file.tar [root@localhost test]# tar -Af file.tar extra.tar [root@localhost test]# ll total 184 -rw-r--r--. 1 root root 22898 Apr 10 22:36 extra -rw-r--r--. 1 root root 30720 Apr 10 22:56 extra.tar -rw-r--r--. 1 root root 45796 Apr 10 22:45 file -rw-r--r--. 1 root root 81920 Apr 10 22:56 file.tar
比较存档与当前文件的不同之处
[root@localhost test]# tar -dvf file file.tar tar: This does not look like a tar archive tar: Skipping to next header tar: file.tar: Not found in archive tar: Exiting with failure status due to previous errors
从存档中删除
转到指定的目录
[root@localhost test]# tar -xvf file.tar -C /tar/ file [root@localhost test]# ls /tar file
显示 tar 程序的版本号
[root@localhost test]# tar --version tar (GNU tar) 1.23 Copyright (C) 2010 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by John Gilmore and Jay Fenlason.
不把指定文件包含在内
[root@localhost test]# tar --exclude file -cvf file3.tar tar tar/ tar/extra [root@localhost test]# tar -cvf file1.tar tar tar/ tar/file tar/extra [root@localhost test]# tar -tvf file1.tar drwxr-xr-x root/root 0 2014-04-10 23:06 tar/ -rw-r--r-- root/root 45796 2014-04-10 23:06 tar/file -rw-r--r-- root/root 22898 2014-04-10 23:06 tar/extra [root@localhost test]# tar -tvf file3.tar drwxr-xr-x root/root 0 2014-04-10 23:06 tar/ -rw-r--r-- root/root 22898 2014-04-10 23:06 tar/extra
本文出自 “linux成长之路” 博客,请务必保留此出处http://linuxlovers.blog.51cto.com/6787002/1393694
原文:http://linuxlovers.blog.51cto.com/6787002/1393694