tar命令详解

转载于:http://blog.csdn.net/zmhmwf/article/details/1906420  http://www.xxlinux.com/linux/article/accidence/install/20060610/1213.html

 

 

tar命令详解
格式: tar 选项 文件目录列表
功能: 对文件目录进行打包备份
选项:
-c 建立新的归档文件
-r 向归档文件末尾追加文件
-x 从归档文件中解出文件
-O 将文件解开到标准输出
-v 处理过程中输出相关信息
-f 对普通文件操作
-z 调用gzip来压缩归档文件,与-x联用时调用gzip完成解压缩
-Z 调用compress来压缩归档文件,与-x联用时调用compress完成解压缩
实例1: 目的:用tar打包一个目录下的文件
命令:#tar -cvf /mnt/lgx/a1.doc
结果:产生一个以.tar为扩展名的打包文件
实例2: 目的:用tar解开打包文件
命令:#tar -xvf /mnt/lgx/a1.doc.tar
附加说明:在通常情况下,tar打包与gzip(压缩)经常联合使用,效果更好。方法是:
首先用tar打包,如:#tar -cvf /mnt/lgx/a1.doc (产生a1.doc.tar文件)
然后用gzip压缩a1.doc.tar文件,如:#gzip /mnt/lgx/a1.doc.tar (产生a1.doc.tar.gz文件)
实例3: 目的:解压a1.doc.tar.gz文件
方法1:
#gzip -dc /mnt/lgx/a1.doc.tar.gz (产生a1.doc.tar文件)
#tar -xvf /mnt/lgx/a1.doc.tar (产生a1.doc文件)
这两次命令也可使用管道功能,把两个命令合二为一:
#gzip -dc /mnt/lgx/a1.doc.tar.gz | tar -xvf
方法2:使用tar提供的自动调用gzip解压缩功能
#tar -xzvf /mnt/lgx/a1.doc.tar.gz
经过tar打包后,也可用compress命令压缩(注:gzip比compress压缩更加有效),产生一个以.tar.Z的文件,在解包时,可先用“uncompress 文件名”格式解压,然后用“tar -xvf 文件名”解包。也可直接调用“tar -Zxvf 文件名”解包。


====================================================


tar命令详解(很好的中文说明)
怎样使用tar命令打包(备份)文件?
A:The syntax of tar is:
tar [options][files]
The options used most often with tar are as follows:
c Creates a new backup,overwriting any files already on the backup destination.
x Extracts files from backup media.
t Lists the contents of backup media.
v Displays the name of each file being processed.
f Creates backups on a specified device.
u Adds files to the backup if they are not already there,or if they have been modified since they were last written on the backup.
步骤:
1)估算打包文件的大小,确定使用档案媒体(archive media)的空间,用命令du -s
例如:
$ lf
a.out* fork.c mbox time.c
atoi.c int.c memcpy.c vi
$ du -s
296 .
$
该例显示的296代表the total number of 512-byte blocks,计算:512*296/1024=148kilobytes.
2)制作备份,用命令tar cvf /dev/fd0135ds18 .(将该目录下的所有内容备份到1.44英寸软盘中 where c causes a new backup to be created,v causes each file to be displayed as the backing up takes place,and f causes the subsequent argument--/dev/fd0135ds18 to be the destination of the backup.)
注:平时硬盘上的备份,我们一般用命令tar cvf filename.tar .
例如:
$ tar cvf wuyd.tar .
a ./.profile 2 tape blocks
a ./.lastlogin 0 tape blocks
a ./mbox 10 tape blocks
a ./fork.c 2 tape blocks
a ./memcpy.c 1 tape blocks
a ./.sh_history 3 tape blocks
a ./a.out 91 tape blocks
a ./time.c 1 tape blocks
a ./vi 169 tape blocks
a ./int.c 1 tape blocks
a ./atoi.c 1 tape blocks
tar: same as archive file: ./wuyd.tar
a ./.scoadmin.pref 1 tape blocks
$ lf
a.out* fork.c mbox time.c wuyd.tar
atoi.c int.c memcpy.c vi
我们还可以用命令tar cvf /dev/fd0135ds18 ./filename将该目录下的名为filename的文件备份到archive media中
怎样列出tar文档的内容?
A:tar tvf /dev/fd0135ds18
tar tvf filename.tar
例如:
$ tar tvf wuyd.tar
tar: blocksize = 20
rw-------203/50 896 Sep 17 15:07 2003 ./.profile
r--------203/21 0 Sep 24 19:49 2003 ./.lastlogin
rw-------203/50 4720 Sep 21 11:17 2003 ./mbox
rw-r--r--203/50 583 Sep 17 15:27 2003 ./fork.c
rw-r--r--203/50 218 Sep 17 20:22 2003 ./memcpy.c
rw-------203/50 1474 Sep 24 20:26 2003 ./.sh_history
rwxr-xr-x203/50 46224 Sep 22 21:00 2003 ./a.out
rw-r--r--203/50 341 Sep 19 09:02 2003 ./time.c
rw-r--r--203/50 86283 Sep 21 09:14 2003 ./vi
rw-r--r--203/50 93 Sep 21 10:53 2003 ./int.c
rw-r--r--203/50 144 Sep 22 21:00 2003 ./atoi.c
rw-r--r-- 0/3 41 Sep 23 20:34 2003 ./.scoadmin.pref
$
怎样从tar文档中导出文件?
A:tar xvf /dev/fd0135ds18
tar xvf filename.tar
例如:
$ mkdir uu;cp wuyd.tar ./uu
$ lf
a.out* fork.c mbox time.c vi
atoi.c int.c memcpy.c uu/ wuyd.tar
$ cd uu
$ tar xvf wuyd.tar
tar: blocksize = 20
x ./.profile, 896 bytes, 2 tape blocks
x ./.lastlogin, 0 bytes, 0 tape blocks
x ./mbox, 4720 bytes, 10 tape blocks
x ./fork.c, 583 bytes, 2 tape blocks
x ./memcpy.c, 218 bytes, 1 tape blocks
x ./.sh_history, 1474 bytes, 3 tape blocks
x ./a.out, 46224 bytes, 91 tape blocks
x ./time.c, 341 bytes, 1 tape blocks
x ./vi, 86283 bytes, 169 tape blocks
x ./int.c, 93 bytes, 1 tape blocks
x ./atoi.c, 144 bytes, 1 tape blocks
x ./.scoadmin.pref, 41 bytes, 1 tape blocks
$
我们可用命令tar xvf /dev/fd0135ds18 ./filename导出archive media中的单个文件
例如:
$ lf
wuyd.tar
$ tar xvf wuyd.tar ./time.c
tar: blocksize = 20
x ./time.c, 341 bytes, 1 tape blocks
$ lf
time.c wuyd.tar
$
怎样使用tar命令提供的简写形式?
A:
$ more /etc/default/tar
# @(#) def135.src 25.2 94/07/25
#
# Copyright © 1987-1994 The Santa Cruz Operation, Inc.
# All Rights Reserved.
# The information in this file is provided for the exclusive use of
# the licensees of The Santa Cruz Operation, Inc. Such users have the
# right to use, modify, and incorporate this code into other products
# for purposes authorized by the license agreement provided they include
# this notice and the associated copyright notice with any such product.
# The information in this file is provided "AS IS" without warranty.
#
# default/tar - archive devices: tar©
#
# device block size tape
#
archive0=/dev/rfd048ds9 18 360 n
archive1=/dev/rfd148ds9 18 360 n
archive2=/dev/rfd096ds15 10 1200 n
archive3=/dev/rfd196ds15 10 1200 n
# archive4=/dev/rfd096ds9 18 720 n
archive4=/dev/rfd0135ds9 18 720 n
archive5=/dev/rfd1135ds9 18 720 n
# archive5=/dev/rfd196ds9 18 720 n
archive6=/dev/rfd0135ds18 18 1440 n
archive7=/dev/rfd1135ds18 18 1440 n
archive8=/dev/rct0 20 0 y
archive9=/dev/rctmini 20 0 y
archive10=/dev/rdsk/fp03d 18 720 n
archive11=/dev/rdsk/fp03h 18 1440 n
archive12=/dev/rdsk/fp03v21 10 20330 n
#
# The default device in the absence of a numeric or "-f device" argument
archive=/dev/rfd0135ds18 18 1440 n
$
从上面可以看出4代表/dev/rfd0135ds9设备,而默认的设备是/dev/rfd0135ds18,所以 tar xvf /dev/rfd0135ds9 ./filename可以简写为tar xv4 ./filename,而 tar cvf /dev/fd0135ds18 .可以简写为tar cv 

 

==========================

 

 

 
描述
tar 程序用于储存或展开 tar 存档文件。存档文件可放在磁盘中 ,也可以存为普通文件。 tar是需要参数的,可选的参数是A、c、d、r、t、u、x,您在使用tar时必须首先为 tar 指定至少一个参数;然后,您必须指定要处理的文件或目录。如果指定一个目录则该目录下的所有子目录都将被加入存档。
应用举例:

1)展开 abc.tar.gz 使用命令: tar xvzf abc.tar.gz 展开 abc.tar 使用命令: tar xvf abc.tar

2)将当前目录下的 man 目录及其子目录存成存档 man.tar tar cf man.tar ./man
参数说明
运行tar时必须要有下列参数中的至少一个才可运行

-A, --catenate, --concatenate
将一存档与已有的存档合并
-c, --create
建立新的存档
-d, --diff, --compare
比较存档与当前文件的不同之处
--delete
从存档中删除
-r, --append
附加到存档结尾
-t, --list
列出存档中文件的目录
-u, --update
仅将较新的文件附加到存档中
-x, --extract, --get
从存档展开文件


其他参数

--atime-preserve
不改变转储文件的存取时间

-b, --block-size N
指定块大小为 Nx512 字节(缺省时 N=20)

-B, --read-full-blocks
读取时重组块(???!!!)

-C, --directory DIR

转到指定的目录

--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 程序的版本号

-w, --interactive, --confirmation
每个操作都要求确认

-W, --verify
写入存档后进行校验

--exclude FILE
不把指定文件包含在内

-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]
指定驱动器和密度[高中低]

--------------------------

打包: tar -cf soft.tar soft

解包: tar -xf soft.tar soft

压缩目录

打包压缩:tar czvf usr.tar.gz /home

解压缩:tar xzvf usr.tar.gz

压缩文件(对于目录失效)

压缩:zip good.zip good1 good2

解压:unzip good.zip

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值