tar命令
1. 作用  tar命令是Unix/Linux系统中备份文件的可靠方法,几乎可以工作于任何环境中,它的使用权限是所有用户。
2. 格式  tar [主选项+辅选项] 文件或目录
3. 主要参数
使用该命令时,主选项是必须要有的,它告诉tar要做什么事情,辅选项是辅助使用的,可以选用。
3.1 主选项:

-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
3.2 辅助选项:
-b 该选项是为磁带机设定的,其后跟一数字,用来说明区块的大小,系统预设值为20(20×512 bytes)。
-f 使用档案文件或设备,这个选项通常是必选的。
-k 保存已经存在的文件。例如把某个文件还原,在还原的过程中遇到相同的文件,不会进行覆盖。

-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
-M 创建多卷的档案文件,以便在几个磁盘中存放。

-v 详细报告tar处理的文件信息。如无此选项,tar不报告文件信息。

[root@localhost test]# tar -cvf file.tar file
file
[root@localhost test]# ls
extra  file  file.tar
-w 每一步都要求确认。

-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
-J代表调用“xz”程序进行文件的压缩(但在RHEL5的1.53上面无法使用,在RHEL6的2.23上面可以使用)

-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
-A,--catenate,--concatenate

将一存档与已有的存档合并

[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
-d,--diff,--compare

比较存档与当前文件的不同之处

[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
--delete

从存档中删除


其他参数
--atime-preserve
不改变转储文件的存取时间
-b,--block-size N
指定块大小为 Nx512 字节(缺省时 N=20)
-B,--read-full-blocks
读取时重组块(???!!!)
-C,--directory DIR

转到指定的目录

[root@localhost test]# tar -xvf file.tar -C /tar/
file
[root@localhost test]# ls /tar
file
--checkpoint
读取存档时显示目录名
-f,--file [HOSTNAME:]F
指定存档或设备 (缺省为 /dev/rmt0)
--force-local
强制使用本地存档,即使存在克隆
-F,--info-script F --new-volume-script F
在每个磁盘结尾使用脚本 F (隐含 -M)
-G,--incremental
建立老 GNU 格式的备份
-g,--listed-incremental F
建立新 GNU 格式的备份
-h,--dereference
不转储动态链接,转储动态链接指向的文件。
-i,--ignore-zeros
忽略存档中的 0 字节块(通常意味着文件结束)
--ignore-failed-read
在不可读文件中作 0 标记后再退出???
-k,--keep-old-files
保存现有文件;从存档中展开时不进行覆盖
-K,--starting-file F
从存档文件 F 开始
-l,--one-file-system
在本地文件系统中创建存档
-L,--tape-length N
在写入 N*1024 个字节后暂停,等待更换磁盘
-m,--modification-time
当从一个档案中恢复文件时,不使用新的时间标签
-M,--multi-volume
建立多卷存档,以便在几个磁盘中存放
-N,--after-date DATE,--newer DATE
仅存储时间较新的文件
-o,--old-archive,--portability
以 V7 格式存档,不用 ANSI 格式
-O,--to-stdout
将文件展开到标准输出
-p,--same-permissions,--preserve-permissions
展开所有保护信息
-P,--absolute-paths
不要从文件名中去除 '/'
--preserve
like -p -s
与 -p -s 相似
-R,--record-number
显示信息时同时显示存档中的记录数
--remove-files
建立存档后删除源文件
-s,--same-order,--preserve-order
???
--same-owner
展开以后使所有文件属于同一所有者
-S,--sparse
高效处理
-T,--files-from F
从文件中得到要展开或要创建的文件名
--null
读取空结束的文件名,使 -C 失效
--totals
显示用 --create 参数写入的总字节数
-v,--verbose
详细显示处理的文件
-V,--label NAME
为存档指定卷标
--version

显示 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.
-w,--interactive,--confirmation
每个操作都要求确认
-W,--verify
写入存档后进行校验
--exclude FILE

不把指定文件包含在内

[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
-X,--exclude-from FILE
从指定文件中读入不想包含的文件的列表
-y,--bzip2,--bunzip2
用 bzip2 对存档压缩或解压
-Z,--compress,--uncompress
用 compress 对存档压缩或解压
-z,--gzip,--ungzip
用 gzip 对存档压缩或解压
--use-compress-program PROG
用 PROG 对存档压缩或解压 ( PROG 需能接受 -d 参数)
--block-compress
为便于磁盘存储,按块记录存档
-[0-7][lmh]
指定驱动器和密度[高中低]