Linux 压缩归档

压缩归档

  1. 掌握归档的定义:归档(archiving)就是将许多文件(或目录)打包成一个文件。
  2. 了解归档的目的:归档的目的就是方便备份、还原及文件的传输操作。
  3.  掌握tar命令的功能:将多个文件(也可能包括目录,因为目录本身也是文件)放在一起存放到一个磁带或磁盘归档文件中。并且将来可以根据需要只还原归档文件中的某些指定的文件。
  4. 掌握tar命令的常用选项:
  • c:创建一个新的tar文件。
  • t:列出tar文件中目录的内容。
  • x:从tar文件中抽取文件。
  • f:指定归档文件或磁带(也可能是软盘)设备(一般都要选)。
  • v:显示所打包的文件的详细信息,v是verbose的第1个字母。
  • z:使用gzip压缩算法来压缩打包后的文件。
  • j:使用bzip2压缩算法来压缩打包后的文件(文件压缩的更小)

常用选项组合:

压缩归档:czvf

解压:xzvf

压缩归档:

[root@localhost ~]# mkdir text
[root@localhost ~]# cd text
[root@localhost text]# touch {1..10}.txt
[root@localhost text]# ll
总用量 0
-rw-r--r--. 1 root root 0 5月  13 16:18 10.txt
-rw-r--r--. 1 root root 0 5月  13 16:18 1.txt
-rw-r--r--. 1 root root 0 5月  13 16:18 2.txt
-rw-r--r--. 1 root root 0 5月  13 16:18 3.txt
-rw-r--r--. 1 root root 0 5月  13 16:18 4.txt
-rw-r--r--. 1 root root 0 5月  13 16:18 5.txt
-rw-r--r--. 1 root root 0 5月  13 16:18 6.txt
-rw-r--r--. 1 root root 0 5月  13 16:18 7.txt
-rw-r--r--. 1 root root 0 5月  13 16:18 8.txt
-rw-r--r--. 1 root root 0 5月  13 16:18 9.txt
[root@localhost text]# tar czvf text.tar.gz ./*
./10.txt
./1.txt
./2.txt
./3.txt
./4.txt
./5.txt
./6.txt
./7.txt
./8.txt
./9.txt
[root@localhost text]# ll
总用量 4
-rw-r--r--. 1 root root   0 5月  13 16:18 10.txt
-rw-r--r--. 1 root root   0 5月  13 16:18 1.txt
-rw-r--r--. 1 root root   0 5月  13 16:18 2.txt
-rw-r--r--. 1 root root   0 5月  13 16:18 3.txt
-rw-r--r--. 1 root root   0 5月  13 16:18 4.txt
-rw-r--r--. 1 root root   0 5月  13 16:18 5.txt
-rw-r--r--. 1 root root   0 5月  13 16:18 6.txt
-rw-r--r--. 1 root root   0 5月  13 16:18 7.txt
-rw-r--r--. 1 root root   0 5月  13 16:18 8.txt
-rw-r--r--. 1 root root   0 5月  13 16:18 9.txt
-rw-r--r--. 1 root root 181 5月  13 16:20 text.tar.gz

压缩到哪个文件夹里就进入到哪个文件夹里去压缩。

将text中的文件压缩到text2中去:

[root@localhost /]# cd /root/text2
[root@localhost text2]# tar czvf text2.tar.gz /root/text/*
tar: 从成员名中删除开头的“/”
/root/text/10.txt
/root/text/1.txt
/root/text/2.txt
/root/text/3.txt
/root/text/4.txt
/root/text/5.txt
/root/text/6.txt
/root/text/7.txt
/root/text/8.txt
/root/text/9.txt
/root/text/text.tar.gz
[root@localhost text2]# ll
总用量 4
-rw-r--r--. 1 root root 450 5月  13 16:27 text2.tar.gz

或者:

指定创建tar的位置

[root@ken test]# tar czf /kenken1/test1.tar.gz ./*
[root@ken test]# ls /kenken1

解压到指定文件夹:

[root@localhost text]# tar -xf text.tar.gz -C /root/text2
[root@localhost text]# ll /root/text2
总用量 4
-rw-r--r--. 1 root root   0 5月  13 16:18 10.txt
-rw-r--r--. 1 root root   0 5月  13 16:18 1.txt
-rw-r--r--. 1 root root   0 5月  13 16:18 2.txt
-rw-r--r--. 1 root root   0 5月  13 16:18 3.txt
-rw-r--r--. 1 root root   0 5月  13 16:18 4.txt
-rw-r--r--. 1 root root   0 5月  13 16:18 5.txt
-rw-r--r--. 1 root root   0 5月  13 16:18 6.txt
-rw-r--r--. 1 root root   0 5月  13 16:18 7.txt
-rw-r--r--. 1 root root   0 5月  13 16:18 8.txt
-rw-r--r--. 1 root root   0 5月  13 16:18 9.txt
-rw-r--r--. 1 root root 450 5月  13 16:27 text2.tar.gz

 

posted @ 2019-05-13 16:44  假如菜鸟不是鸟  阅读(731)  评论(0编辑  收藏  举报