文件的压缩和打包 1.常用压缩指令: • *.bz2 #bzip2 程序压缩文件 • *.gz #gzip 程序压缩文件 • *.tar #tar 程序打包文件但并未压缩 • *.tar.gz #tar 程序打包文件且经过 gzip 压缩 ·compress [root@linux ~]# compress [-dcr] 文件或目录 -d :解压缩 -r :递归压缩,即目录下的文件全部压缩 -c :将压缩产生的信息输出到 standard output [root@linux tmp]# compress man.config [root@linux tmp]# compress -d man.config.Z [root@linux tmp]# compress -c man.config > man.config.back.Z ·gzip, zcat [root@linux ~]# gzip [-cdt-] file #压缩指定文件 [root@linux ~]# zcat file.gz #查看压缩文件内容 -c :将压缩数据输出到指定设备,不改变原文件,用重导向功能 -d :解压缩 -r :递归压缩 -t :检验压缩文件一致性 -- :压缩等级,-1 最快,但是压缩比最低、-9 最慢,但是压缩比最高!默认是-6 [root@linux ~]# cd /tmp [root@linux tmp]# cp /etc/man.config . [root@linux tmp]# gzip man.config [root@linux tmp]# zcat man.config.gz [root@linux tmp]# gzip -d man.config.gz [root@linux tmp]# gzip -9 -c man.config > man.config.gz ·bzip2, bzcat [root@linux ~]# bzip2 [-cdz-] file [root@linux ~]# bzcat file.bz2 -c :将压缩数据输出到指定设备,不改变原文件,用重导向功能 -d :解压缩 -z :强制压缩 -- :压缩等级,同gzip [root@linux tmp]# bzip2 -z man.config [root@linux tmp]# bzcat man.config.bz2 [root@linux tmp]# bzip2 -d man.config.bz2 [root@linux tmp]# bzip2 -9 -c man.config > man.config.bz2 ·tar [root@linux ~]# tar [-cxtzjvfpPN] 文件或目录 -c :建立压缩文件 -x :解开压缩文件 -t :查看 tarfile 文件 -z :是否使用 gzip 压缩 -j :是否使用 bzip2 压缩 -v :显示命令执行过程 -f :指定压缩文件名,在 f 后要立即接文件名 -p :保留原文件属性、权限 -P :使用绝对路径压缩 -N :只压缩比指定日期新的文件 --exclude FILE:在压缩时,排除这些文件 [root@linux ~]# tar -cvf /tmp/etc.tar /etc #打包但不压缩/etc目录下的所有文件,指定打包文件名为etc.tar并放在/tmp目录下 [root@linux ~]# tar -zcvf /tmp/etc.tar.gz /etc #打包并用gzip压缩 [root@linux ~]# tar -jcvf /tmp/etc.tar.bz2 /etc #打包并用bzip2压缩 [root@linux ~]# tar -ztvf /tmp/etc.tar.gz #查看打包文件,如果是gzip压缩的必须加-z选项 [root@linux ~]# cd /usr/local/src [root@linux src]# tar -zxvf /tmp/etc.tar.gz #将 /tmp/etc.tar.gz 解压到 /usr/local/src 目录下 [root@linux ~]# cd /tmp [root@linux tmp]# tar -zxvf /tmp/etc.tar.gz etc/passwd #在 /tmp 目录下,只将 /tmp/etc.tar.gz 文件内的 etc/passwd 解压缩,没有 "/" [root@linux ~]# tar -zcvpf /tmp/etc.tar.gz /etc #压缩/etc目录下的所有文件并保留原始属性 [root@linux ~]# tar -N '2005/06/01' -zcvf home.tar.gz /home #只压缩比2005/06/01日期新的文件 [root@linux ~]# tar --exclude /home/dmtsai -zcvf myfile.tar.gz /home/* /etc #压缩 /home, /etc ,但排除 /home/dmtsai [root@linux ~]# cd /tmp [root@linux tmp]# tar -cvf - /etc | tar -xvf - #将 /etc/ 压缩后直接解压到 /tmp 目录下,不产生中间文件。输出文件变成 - 而输入文件 #也变成 - 。其实就是复制的过程。 2.绝对路径和权限的问题 请特别注意tar参数中的 -P,-p ,tar将 /etc 目录的 / 删除了,因为在 tar 里面的文件如果具有『绝对路径』,那么当解压这个文件时将一定会解压到该绝对路径下,即/etc,而不是相对路径。如果不删除,万一别的系统上也有 /etc 目录,那么就会被覆盖。因此,在默认情况下,如果是以绝对路径建立压缩文件,那么 tar 将自动删除 / 。如果需要以绝对路径来建立压缩文件,就加入 -P 参数。解压时也要加上-P 参数,就会以绝对路径解压。 -p 就是权限,使用参数 -p 后,被压缩的文件将保留原有权限。 3.tarfile 与 tarball tarball :通过 tar 打包并压缩的文件 tarfile :通过 tar 打包但不压缩的文件 tar 可用于备份到储存设备上,假如磁带机代号为 /dev/st0,备份/home目录下的数据: [root@linux ~]# tar /dev/st0 /home Linux 系统中,gzip 已经被整合到 tar 。但是 Sun 或者其它较旧的 Unix 版本中, 当中的 tar 并没有整合 gzip ,如果需要解压缩,就需要: [root@linux ~]# gzip -d testing.tar.gz [root@linux ~]# tar -xvf testing.tar 在没有加入特殊参数的情况下 bzip2, gzip,compress 会把原始文件替代,但是 tar 会保留原始文件。 4.dd [root@linux ~]#dd if="input_file" of="outptu_file" bs="block_size" count="number" if :输入文件或设备 of :输出文件或设备 bs :block大小,默认是 512 bytes count:bs的数量 [root@linux ~]# dd if=/etc/passwd of=/tmp/passwd.back 3+1 records in 3+1 records out 3+1 表示 3 个完整的512 bytes,以及未满 512 bytes 的另一个 block [root@linux ~]# dd if=/dev/hda of=/tmp/mbr.back bs=512 count=1 #备份 /dev/hda 的 MBR [root@linux ~]# dd if=/dev/hda1 of=/some/path/filename #备份 /dev/hda1 整个分区 5.cpio [root@linux ~]# cpio -covB > [file|device] #备份 [root@linux ~]# cpio -icduv < [file|device] #还原 -o :将数据复制输出到文件或设备上,建立备份 -i :将数据从文件或设备复制到系统中,还原备份 -t :列出备份文件的内容列表 -c :使用旧的ASCII备份格式 -v :显示命令详细执行过程 -B :修改默认的 Blocks 增加到 5120 bytes ,默认是 512 bytes,加快存储速度 -d :自动建立目录 -u :自动将较新的文件覆盖较旧的文件 [root@linux ~]# find / -print | cpio -covB > /dev/st0 #将系统上所有的数据全部写入磁带机 [root@linux ~]# cpio -icdvt < /dev/st0 [root@linux ~]# cpio -icdvt < /dev/st0 > /tmp/content #查看磁带机上的备份文件 [root@linux ~]# cpio -icduv < /dev/st0 #还原磁带机上的备份文件 [root@linux ~]# find /etc -type f | cpio -o > /root/etc.cpio #将/etc目录下的文件备份到/root/etc.cpio [root@linux ~]# cpio -i < /root/etc.cpio #还原备份 由于 cpio 无法直接读取文件,需要『每一个文件或目录的路径连同文件名一起』才可以被记录下来。因此,cpio 经常与 find 指令搭配使用。