The tar
(tape archive) command bundles a bunch of files together and creates an archive (commonly called a tar file
or
tarball
) on a tape, disk drive, or floppy disk. The original files are not deleted after being copied to the tar file. Usage: To create an archive using tar
, use a command like this, which bundles all the files in the current directory that end with .doc
into the alldocs.tar
file:
tar c
vf alldocs.tar *.doc
Here's a second example, which creates a tar file named panda.tar
containing all the files from the panda
directory (and
any of its subdirectories): tar cvf panda.tar panda/
In these examples, the c
, v
, and f
flags mean create a new archive, be verbose (list files being archived), and write the archive to a
file. You can also create tar files on tape drives or floppy disks, like this: tar cvfM /dev/fd0 panda
Archive the files in the panda directory to floppy disk(s). The /dev/fd0
entry is Linux-ese for "floppy drive zero" (your A drive under DOS), and /dev/rmt0
means "removable media
tape zero," or your primary tape drive. The M
flag means use multiple floppy disks--when one disk is full, tar
prompts you to
insert another. To automatically compress the tar file as it is being created, add the z
flag, like this: tar cvz
f alldocs.tar.gz *.doc
In this example, I added the .gz
suffix to the archive file name, because the z
flag tells tar
to use the same compression as the
gzip
command. To list the contents of a tar file, use the t
(type) flag in a command, like this: tar t
vf alldocs.tar
List all files in alldocs.tar
.
To extract the contents of a tar file, use the x
(extract) flag in a command, like this: tar x
vf panda.tar
Extract files from panda.tar
.
This will copy all the files from the panda.tar
file
into the current directory. When a tar file is created, it can bundle
up all the
files in a directory, as well as any subdirectories and the files in
them. So when you're extracting a tar file, keep in mind that you
might end up with some new subdirectories in the current directory. We've used several different flags in the sample tar
commands so far. Here's a list of the most common flags: c
Create a new archive. gzip: 减少文件大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间。gzip 是在 Linux 系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用。
List: tar -tf <archive-filename>
Extract: tar -xf <archive-filename>
Create: tar -cf <archive-filename> [filenames...]
tar cvf /dev/rmt0 panda
Archive the files in the panda directory to the tape drive.
t
List the contents of an archive.
x
Extract the contents of an archive.
f
The archive file name is given on the command line (required whenever the tar output is going to a file)
M
The archive can span multiple floppies.
v
Print verbose output (list file names as they are processed).
u
Add files to the archive if they are newer than the copy in the tar file.
z
Compress or decompress files automatically.
语法:gzip [选项] 压缩(解压缩)的文件名
该命令的各选项含义如下:
-c 将输出写到标准输出上,并保留原有文件。
-d 将压缩文件解压。
-l 对每个压缩文件,显示下列字段:
压缩文件的大小;未压缩文件的大小;压缩比;未压缩文件的名字 -r 递归式地查找指定目录并压缩其中的所有文件或者是解压缩。 -t 测试,检查压缩文件是否完整。
-v 对每一个压缩和解压的文件,显示文件名和压缩比。
-num 用指定的数字 num 调整压缩的速度,
-1 或 --fast 表示最快压缩方法(低压缩比), -9 或--best表示最慢压缩方法(高压缩比)。系统缺省值为 6。
指令实例: gzip * % 把当前目录下的每个文件压缩成 .gz 文件。
gzip -dv * % 把当前目录下每个压缩的文件解压,并列出详细的信息。
gzip -l * % 详细显示例1中每个压缩的文件的信息,并不解压。
gzip usr.tar % 压缩 tar 备份文件 usr.tar,此时压缩文件的扩展名为.tar.gz。