15.归档和备份(快乐的Linux命令行)

 

1.本节学习的命令

gzip Compress or expand files
bzip2 A block sorting file compressor
tar Tape archiving utility
zip  Package and compress files
rsync Remote file and directory synchronization

 

2.知识点

知识点1:gzip选项详解。

选项 说明
-c 把输出写入到标准输出,并且保留原始文件。也有可能用--stdout 和--to-stdout 选项来指定。
-d 解压缩。就等同于gunzip
-f 强制压缩,即使原始文件的压缩文件已经存在了,也要执行。也可以用--force 选项来指定。
-h 显示用法信息。也可用--help 选项来指定。
-l 列出每个被压缩文件的压缩数据。也可用--list 选项。
-r 若命令的一个或多个参数是目录,则递归地压缩目录中的文件。也可用--recursive 选项来指定。
-t 测试压缩文件的完整性。也可用--test 选项来指定。就是看一下压缩文件是否完整。比如gzip -t foo.txt.gz 返回ok.
-v 显示压缩过程中的信息。也可用--verbose 选项来指定。
-number 设置压缩指数。范围1到9。数值1和9也可以各自用--fast (快)和--best(最大压缩,慢) 选项来表示。默认值是6。

知识点2:tar模式有哪些?和选项有什么区别?

c 为文件和/或目录列表创建归档文件。 就是把文件夹变成归档文件。
x 抽取归档文件。 就是把里面的内容给取出来。可能是文件夹
r 追加具体的路径到归档文件的末尾。
t 列出归档文件的内容。 就是把归档里面的内容全部列出来

模式和选项可以写在一起,而且不需要开头的短横线。但是一定要先是模式再是选项。

3.案例实操

[me@linuxbox ~]$ ls -l /etc > foo.txt
[me@linuxbox ~]$ ls -l foo.*
-rw-r--r-- 1 me     me 15738 2008-10-14 07:15 foo.txt
[me@linuxbox ~]$ gzip foo.txt
[me@linuxbox ~]$ ls -l foo.*
-rw-r--r-- 1 me     me 3230 2008-10-14 07:15 foo.txt.gz
[me@linuxbox ~]$ gunzip foo.txt.gz
[me@linuxbox ~]$ ls -l foo.*
-rw-r--r-- 1 me     me 15738 2008-10-14 07:15 foo.txt
注:gzip和gunzip原文件都会消失。后缀为.gz

[me@linuxbox ~]$ gzip foo.txt
[me@linuxbox ~]$ gzip -tv foo.txt.gz
foo.txt.gz: OK
[me@linuxbox ~]$ gzip -d foo.txt.gz
[me@linuxbox ~]$ gunzip foo.txt.gz

[me@linuxbox ~]$ ls -l /etc | gzip > foo.txt.gz
注:将内容压缩后,再放入foo.txt.gz的压缩包里。

[me@linuxbox ~]$ gunzip -c foo.txt.gz | less
[me@linuxbox ~]$ zcat foo.txt.gz | less
注:将foo.txt.gz的内容解压,由于有-c,所以保留原始foo.txt.gz文件。且将内容传给less查看。zcat等同
于gunzip -c

[me@linuxbox ~]$ ls -l /etc > foo.txt
[me@linuxbox ~]$ ls -l foo.txt
-rw-r--r-- 1 me     me      15738 2008-10-17 13:51 foo.txt
[me@linuxbox ~]$ bzip2 foo.txt
[me@linuxbox ~]$ ls -l foo.txt.bz2
-rw-r--r-- 1 me     me      2792 2008-10-17 13:51 foo.txt.bz2
[me@linuxbox ~]$ bunzip2 foo.txt.bz2
注:bzip用法等同于gzip,只是使用了不同的压缩算法。

[me@linuxbox ~]$ tar cf playground.tar playground
注:创建playground的tar包,模式是C 创建归档文件,选线是f

[me@linuxbox ~]$ tar tf playground.tar
me@linuxbox ~]$ tar tvf playground.tar
注:模式是t 列出归档文件的内容

[me@linuxbox ~]$ mkdir foo
[me@linuxbox ~]$ cd foo
[me@linuxbox ~]$ tar xf ../playground.tar
[me@linuxbox ~]$ ls
playground
注:模式是x,抽取归档文件。

[me@linuxbox foo]$ tar xf ../playground2.tar --wildcards 'home/me/playground/dir-\*/file-A'
注:这个命令将只会抽取匹配特定路径名的文件,路径名中包含了通配符 dir-*。

[me@linuxbox ~]$ find playground -name 'file-A' -exec tar rf playground.tar '{}' '+'
注:与tar的结合 追加模式(r) 这样每一个就在一个文件里面了。

[me@linuxbox ~]$ find playground -name 'file-A' | tar cf - --files-from=-
   | gzip > playground.tgz
注:指定了文件名“-”,则其被看作是标准输入或输出。这个 --file-from 选项(也可以用 -T 来指定)导致
tar 命令从一个文件而不是命令行来读入它的路径名列表。最后,这个由 tar 命令产生的归档 文件被管道到
gzip 命令中,然后创建了压缩归档文件 playground.tgz。

[me@linuxbox ~]$ find playground -name 'file-A' | tar czf playground.tgz -T -
注:tar - 进入playground.tgz
[me@linuxbox ~]$ find playground -name 'file-A' | tar cjf playground.tbz -T -
注:z为gzip j为bzip2

[me@linuxbox ~]$ zip -r playground.zip playground
[me@linuxbox foo]$ unzip ../playground.zip
注;注意一点,就是如果指定了一个已经存在的文件包,其被更新 而不是被替代。

[me@linuxbox ~]$ unzip -l playground.zip playground/dir-87/file-Z
Archive: ../playground.zip
    Length      Date    Time    Name

         0    10-05-08  09:25   playground/dir-87/file-Z

         0                      1 file
[me@linuxbox ~]$ cd foo
[me@linuxbox foo]$ unzip ./playground.zip playground/dir-87/file-Z
Archive: ../playground.zip
replace playground/dir-87/file-Z? [y]es, [n]o, [A]ll, [N]one,
[r]ename: y
extracting: playground/dir-87/file-Z

[me@linuxbox ~]$ ls -l /etc/ | zip ls-etc.zip -
adding: - (deflated 80%)
[me@linuxbox ~]$ unzip -p ls-etc.zip | less

[me@linuxbox ~]$ rsync -av playground foo
注:同步 playground 目录和它在 foo 目录中相对应的副本

[me@linuxbox ~]$ sudo rsync -av --delete /etc /home /usr/local /media/BigDisk/backup
注:把/etc,/home,和/usr/local 目录从我们的系统中复制到假想的存储设备中。 我们包含了–delete 这
个选项,来删除可能在备份设备中已经存在但却不再存在于源设备中的文件。

[me@linuxbox ~]$ sudo rsync -av --delete --rsh=ssh /etc /home /usr/local remote-sys:/backup
注:--rsh=ssh 选项,其指示 rsync 使用 ssh 程序作为它的远程 shell。其次,通过在目标路径名前加上
远端主机的名字(在这种情况下, 远端主机名为 remote-sys),来指定远端主机。即ssh remote-sys

[me@linuxbox ~]$ rsync -av -delete rsync://rsync.gtlib.gatech.edu/fedora-linux-
 core/development/i386/os fedora-devel
注:在这个例子里,我们使用了远端 rsync 服务器的 URI,其由协议(rsync://),远端主机名和软件仓库
的路径名组成。(rsync.gtlib.gatech.edu),
发布了59 篇原创文章 · 获赞 11 · 访问量 1万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章