Linux学习笔记—文件与文件系统的压缩与打包(转载)
压缩文件的用途与技术
例如,计算机都是以byte单位来计量的,1byte占8bit。如果存储数字1,那么1byte就会空出7bit。采用一定的计算方式,压缩这些空间可以大大降低文件存储。
Linux系统常见的压缩命令
Linux下压缩文件的扩展名有*.tar, *.tar.gz, *.tgz, *.gz, *.Z, *.bz2
扩展名用来说明压缩所采用的指令类型
- *.Z compress程序压缩的文件
- *.gz gzip程序压缩的文件
- *.bz2 bzip2程序压缩的文件
- *.tar tar打包的数据,未压缩
- *.tar.gz tar打包的数据,经gzip压缩
- *.tar.bz2 tar打包的数据,经bzip2压缩
Compress
非常旧的指令,可以忽视,gzip可以解压其压缩的*.Z文件
gzip,zcat
gzip可以解压compress,zip,gzip压缩的文件
参数:
-c:将压缩数据输出到屏幕
-d:解压缩的参数
-t:检验压缩文件的一致性
-v:显示压缩比
-#:压缩等级,-1最快但压缩比最低,-9最慢但压缩比最好,默认-6

[root@localhost tmp]# ls -l ./auto.smb
-rwxr-xr-x. 1 root root 687 6月 29 21:37 ./auto.smb
//压缩
[root@localhost tmp]# gzip -v ./auto.smb
./auto.smb: 37.1% -- replaced with ./auto.smb.gz
[root@localhost tmp]# ls -l ./auto.smb.gz
-rwxr-xr-x. 1 root root 459 6月 29 21:37 ./auto.smb.gz
//解压缩
[root@localhost tmp]# gzip -d ./auto.smb.g

zcat指令可以读取纯文本被压缩后的压缩文件

[root@localhost tmp]# zcat ./auto.smb.gz
#!/bin/bash # This file must be executable to work! chmod 755! key="$1"
opts="-fstype=cifs" for P in /bin /sbin /usr/bin /usr/sbin
do
if [ -x $P/smbclient ]
then
SMBCLIENT=$P/smbclient
break
fi
done [ -x $SMBCLIENT ] || exit 1 $SMBCLIENT -gNL $key 2>/dev/null| awk -v key="$key" -v opts="$opts" -F'|' -- '
BEGIN { ORS=""; first=1 }
/Disk/ {
if (first)
print opts; first=0
dir = $2
loc = $2
# Enclose mount dir and location in quotes
# Double quote "$" in location as it is special
gsub(/\$$/, "\\$", loc);
gsub(/\&/,"\\\\&",loc)
print " \\\n\t \"/" dir "\"", "\"://" key "/" loc "\""
}
END { if (!first) print "\n"; else exit 1 }
' [root@localhost tmp]#

bzip2,bzcat
参数:
-c:将压缩过程的数据显示到屏幕
-d:解压缩的参数
-k:保留源文件
-z:压缩的参数
-v:显示压缩比信息
-#:压缩比,同gzip

[root@localhost tmp]# ls -l ./auto.smb
-rwxr-xr-x. 1 root root 687 6月 29 21:37 ./auto.smb
[root@localhost tmp]# bzip2 -kzv ./auto.smb
./auto.smb: 1.314:1, 6.090 bits/byte, 23.87% saved, 687 in, 523 out.
[root@localhost tmp]# ls -l ./auto.smb.bz2
-rwxr-xr-x. 1 root root 523 6月 29 21:37 ./auto.smb.bz2

使用bzcat可以查看压缩后的文件内容
打包命令:tar
将多个文件打包成一个文件,便于多个文件的压缩
tar
tar [-j|-z] [cv] [-f 新建的文件名] filename . .. //打包与压缩
tar [-j|-z] [tv] [-f 新建的文件名] //查看文件名
tar [-j|-z] [xv] [-f 新建的文件名]
常用的指令方式如下:
压缩:tar -jcv -f filename.tar.bz2
查询:tar -jtv -f filename.tar.bz2
解压:tar -jxv -f filename.tar.bz2 -C 欲解压的目录

[root@localhost hsperfdata_wuchao]# cd /tmp
[root@localhost tmp]# mkdir ./tartest
[root@localhost tmp]# cd ./tartest
[root@localhost tartest]# cp /var/spool/mail/root . [root@localhost tartest]# ls -l root
-rw-------. 1 root root 571540 6月 30 10:40 root
[root@localhost tartest]# cp /var/spool/mail/wuchao . [root@localhost tartest]# ls -l ./wuchao
-rw-r-----. 1 root root 0 6月 30 10:41 ./wuchao //压缩
[root@localhost tartest]# tar -jcv -f tartest.tar.bz2 ./root ./wuchao
./root
./wuchao //查看压缩文件
[root@localhost tartest]# tar -jtv -f tartest.tar.bz2
-rw------- root/root 571540 2016-06-30 10:40 ./root
-rw-r----- root/root 0 2016-06-30 10:41 ./wuchao //解压
[root@localhost tartest]# tar -jxv -f tartest.tar.bz2 .
./root
./wuchao //解压到特定目录
[root@localhost tartest]# mkdir ./t
[root@localhost tartest]# tar -jxv -f ./tartest.tar.bz2 -C ./t

完整备份工具:dump
dump
用于对文件系统的备份,对某个文件系统进行dump备份后,第二次dump时可以指定备份等级,等级0表示完整备份,等级大于0表示新备份的数据只会记录与上一次较低等级相比新增或更改的文件。
使用限制:
- 当备份的数据为单一文件系统:可以使用完整的dump功能,同时备份时可以使用挂载点或设备文件名来进行备份。
- 当备份的数据只是目录,并非单一的文件系统:比如要备份/home/someone,但该目录并非单一的文件系统,此时的备份数据都要在该目录下,且只能使用level0,不支持-u参数,即无法创建/etc/dumpdates这个level备份的时间记录文件。
dump [-Suv] [-level] [-f 备份文件] 待备份数据
dump -W
参数:
-S:列出后面待备份数据需要的磁盘空间
-u:将本次dump的时间记录到/etc/dumpdates文件中
-v:将dump的文件过程显示出来
-j:加入bzip2的支持,将数据进行压缩
-level:dump等级
-f:备份后的文件
-W:列出/etc/fstab里具有dump设置的分区是否备份过
用dump备份完整的文件系统

//查看文件系统
[root@localhost tartest]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 46G 5.8G 40G 13% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 656K 1.9G 1% /dev/shm
tmpfs 1.9G 17M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-home 23G 92M 23G 1% /home
/dev/sda5 497M 213M 284M 43% /boot
tmpfs 373M 28K 373M 1% /run/user/0
/dev/sda3 976M 2.6M 907M 1% /run/media/root/MyFile //查看备份/dev/sda3需要的空间
[root@localhost tartest]# dump -S /dev/sda3
53248 //进行备份,备份到/root/boot.dump文件,/run/media/root/MyFile也可以写成/dev/sda3
[root@localhost tartest]# dump -0u -f /root/boot.dump /run/media/root/MyFile
DUMP: Date of this level 0 dump: Thu Jun 30 15:42:53 2016
DUMP: Dumping /dev/sda3 (/run/media/root/MyFile) to /root/boot.dump
DUMP: Label: MyFile
DUMP: Writing 10 Kilobyte records
DUMP: mapping (Pass I) [regular files]
DUMP: mapping (Pass II) [directories]
DUMP: estimated 52 blocks.
DUMP: Volume 1 started with block 1 at: Thu Jun 30 15:42:53 2016
DUMP: dumping (Pass III) [directories]
DUMP: dumping (Pass IV) [regular files]
DUMP: Closing /root/boot.dump
DUMP: Volume 1 completed at: Thu Jun 30 15:42:53 2016
DUMP: Volume 1 40 blocks (0.04MB)
DUMP: 40 blocks (0.04MB) on 1 volume(s)
DUMP: finished in less than a second
DUMP: Date of this level 0 dump: Thu Jun 30 15:42:53 2016
DUMP: Date this dump completed: Thu Jun 30 15:42:53 2016
DUMP: Average transfer rate: 0 kB/s
DUMP: DUMP IS DONE
//查看备份文件
[root@localhost tartest]# ls -dl /run/media/root/MyFile /root/boot.dump
-rw-r--r--. 1 root root 40960 6月 30 15:42 /root/boot.dump
drwx------. 5 root root 4096 6月 30 15:38 /run/media/root/MyFile //查看更新记录
[root@localhost tartest]# cat /etc/dumpdates
/dev/sda3 0 Thu Jun 30 15:42:53 2016 +0800 //查看有没有任何文件系统被备份过
Last dump(s) done (Dump '>' file systems):
/dev/sda3 (/run/media/root/MyFile) Last dump: Level 0, Date Thu Jun 30 15:42:53 2016

备份非文件系统,即单一目录的方法:

//将目录/run/media/root/MyFile/folder1备份
[root@localhost tartest]# dump -0j -f /root/fw.dump /run/media/root/MyFile/folder1
DUMP: Date of this level 0 dump: Thu Jun 30 15:59:17 2016
DUMP: Dumping /dev/sda3 (/run/media/root/MyFile (dir /folder1)) to /root/fw.dump
DUMP: Label: MyFile
DUMP: Writing 10 Kilobyte records
DUMP: Compressing output at compression level 2 (bzlib)
DUMP: mapping (Pass I) [regular files]
DUMP: mapping (Pass II) [directories]
DUMP: estimated 46 blocks.
DUMP: Volume 1 started with block 1 at: Thu Jun 30 15:59:17 2016
DUMP: dumping (Pass III) [directories]
DUMP: dumping (Pass IV) [regular files]
DUMP: Closing /root/fw.dump
DUMP: Volume 1 completed at: Thu Jun 30 15:59:17 2016
DUMP: 40 blocks (0.04MB) on 1 volume(s)
DUMP: finished in less than a second
DUMP: Date of this level 0 dump: Thu Jun 30 15:59:17 2016
DUMP: Date this dump completed: Thu Jun 30 15:59:17 2016
DUMP: Average transfer rate: 0 kB/s
DUMP: Wrote 40kB uncompressed, 11kB compressed, 3.637:1
DUMP: DUMP IS DONE
[root@localhost tartest]#

restore
备份后的恢复
restore -t [-f dumpfile] [-h] 用来查看dump文件
restore -C [-f dumpfile] [-D 挂载点] 用来比较dump文件与实际文件
restore -i [-f dumpfile] 进入互动模式
restore -r [-f dumpfile] 还原整个文件系统
-h:查看完整备份数据的inode和文件系统的label信息
用restore查看dump后的备份数据内容

[root@localhost tartest]# restore -t -f /root/boot.dump
Dump date: Thu Jun 30 15:42:53 2016
Dumped from: the epoch
Level 0 dump of /run/media/root/MyFile on localhost.localdomain:/dev/sda3
Label: MyFile
2 .
11 ./lost+found
12 ./Untitled Folder
8193 ./Untitled Folder 2
13 ./Untitled Folder 2/Untitled Folder
[root@localhost tartest]#

比较差异并还原整个系统

//移除原文件系统的一个文件夹
[root@localhost /]# rm -rf /run/media/root/MyFile/folder1 //查看文件系统和备份文件之间的差异
[root@localhost /]# restore -C -f /root/boot.dump
Dump date: Thu Jun 30 15:42:53 2016
Dumped from: the epoch
Level 0 dump of /run/media/root/MyFile on localhost.localdomain:/dev/sda3
Label: MyFile
filesys = /run/media/root/MyFile
restore: unable to stat ./Untitled Folder: No such file or directory
Some files were modified! 1 compare errors
[root@localhost /]#

还原文件系统:首先创建新的文件系统

[root@localhost /]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them.
Be careful before using the write command. Command (m for help): p .
.
.
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 206847 102400 7 HPFS/NTFS/exFAT
/dev/sda2 206848 403138559 201465856 7 HPFS/NTFS/exFAT
/dev/sda3 403138560 405235711 1048576 83 Linux
/dev/sda4 473042944 625141759 76049408 5 Extended
/dev/sda5 473044992 474068991 512000 83 Linux
/dev/sda6 474071040 625141759 75535360 8e Linux LVM Command (m for help): d
Partition number (1-6, default 6): 2
Partition 2 is deleted Command (m for help): p
.
.
.
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 206847 102400 7 HPFS/NTFS/exFAT
/dev/sda3 403138560 405235711 1048576 83 Linux
/dev/sda4 473042944 625141759 76049408 5 Extended
/dev/sda5 473044992 474068991 512000 83 Linux
/dev/sda6 474071040 625141759 75535360 8e Linux LVM
Command (m for help): n
Partition type:
p primary (2 primary, 1 extended, 1 free)
l logical (numbered from 5)
Select (default p): p
Selected partition 2
First sector (206848-625142447, default 206848):
Using default value 206848
Last sector, +sectors or +size{K,M,G} (206848-403138559, default 403138559): +2014M
Partition 2 of type Linux and of size 2 GiB is set .
.
. Device Boot Start End Blocks Id System
/dev/sda1 * 2048 206847 102400 7 HPFS/NTFS/exFAT
/dev/sda2 206848 4331519 2062336 83 Linux
/dev/sda3 403138560 405235711 1048576 83 Linux
/dev/sda4 473042944 625141759 76049408 5 Extended
/dev/sda5 473044992 474068991 512000 83 Linux
/dev/sda6 474071040 625141759 75535360 8e Linux LVM Command (m for help): w
The partition table has been altered!
[root@localhost /]# partprobe
[root@localhost /]# mkfs -t ext3 /dev/sda2
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
129024 inodes, 515584 blocks
25779 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=528482304
16 block groups
32768 blocks per group, 32768 fragments per group
8064 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912 Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done [root@localhost /]# mount /dev/sda2 /mnt

cd到新建的文件系统的挂载点

//cd到新建的文件系统的挂载点(重要)
[root@localhost /]# cd /mnt
//将备份文件还原到当前文件系统
[root@localhost mnt]# restore -r -f /root/fw.dump
Dump tape is compressed.
./lost+found: (inode 11) not found on tape
./Untitled Folder 2: (inode 8193) not found on tape
[root@localhost mnt]#

仅还原部分文件的互动模式

//查看备份文件
[root@localhost mnt]# restore -t -f /root/boot.dump
Dump date: Thu Jun 30 15:42:53 2016
Dumped from: the epoch
Level 0 dump of /run/media/root/MyFile on localhost.localdomain:/dev/sda3
Label: MyFile
2 .
11 ./lost+found
12 ./Untitled Folder
8193 ./Untitled Folder 2
13 ./Untitled Folder 2/Untitled Folder //进入restore互动模式,此时的所有操作都在备份文件内部操作
[root@localhost mnt]# restore -i -f /root/boot.dump
//输入help 查看帮助
restore > help
Available commands are:
ls [arg] - list directory
cd arg - change directory
pwd - print current directory
add [arg] - add `arg' to list of files to be extracted
delete [arg] - delete `arg' from list of files to be extracted
extract - extract requested files
setmodes - set modes of requested directories
quit - immediately exit program
what - list dump header information
verbose - toggle verbose flag (useful with ``ls'')
prompt - toggle the prompt display
help or `?' - print this list
If no `arg' is supplied, the current directory is used
//查看备份文件列表
restore > ls
.:
Untitled Folder/ Untitled Folder 2/ lost+found/
//进入到文件夹Untitled Folder 2
restore > cd 'Untitled Folder 2'
//查看但前目录名
restore > pwd
/Untitled Folder 2 //查看当前目录下的文件列表
restore > ls 'Untitled Folder'
./Untitled Folder 2/Untitled Folder:
//将Untitled Folder目录添加到解压缩列表
restore > add 'Untitled Folder' //查看当前目录,要被解压的文件前面会有一个*号
restore > ls
./Untitled Folder 2:
*Untitled Folder/
//解压
restore > extract
You have not read any volumes yet.
Unless you know which volume your file(s) are on you should start
with the last volume and work towards the first.
Specify next volume # (none if no more volumes): 1
set owner/mode for '.'? [yn] y
restore > q
//查看解压后的文件
[root@localhost mnt]# ls 'Untitled Folder 2'
Untitled Folder
[root@localhost mnt]#

转载自:
Linux学习笔记—文件与文件系统的压缩与打包(转载)的更多相关文章
- Linux学习笔记之文件与文件系统的压缩与打包
四.文件与文件系统的压缩与打包 用途: 传输时减少带宽 Linux系统常见的压缩命令 Compass(旧版压缩,并且在centOS中默认为不安装) gzip, zcat bzip2, bzcat zi ...
- Linux学习笔记之 Btrfs文件系统简介及使用
Btrfs 也有一个重要的缺点,当 BTree 中某个节点出现错误时,文件系统将失去该节点之下的所有的文件信息.而 ext2/3 却避免了这种被称为”错误扩散”的问题. Btrfs相关介绍: Btrf ...
- 关于文件与文件系统的压缩与打包命令-Linux(笔记)
1.gzip : 压缩命令 gzip [-cdtv#] 文件名称 (后缀为.gz) -c :将压缩的数据输出到屏幕上,可通过数据流重定向处理 -d : 解压缩的參数 -v : 能够显示源文件/压缩文件 ...
- Linux 学习笔记 2:文件系统
1.文件系统层次结构 系统目录内容: /: 根目录(之后的/都是目录分隔符) /home:用户目录 /bin: Unix常用命令,如bash, date, cat, tar等 /sbin: 管理员命令 ...
- 第九章、文件与文件系统的压缩与打包 Linux 系统常见的压缩命令
Linux 系统常见的压缩命令: 在Linux中,压缩文件的扩展名大多是:『*.tar, *.tar.gz, *.tgz, *.gz, *.Z, *.bz2』 Linux 支持的压缩命令非常多,且不同 ...
- linux——(5)文件与文件系统的压缩与打包
概念一:常见的压缩文件拓展名 .z compress程序压缩的文件. .gz gzip程序压缩的文件. .bz2 bzip2程序压缩的文件. .tar tar程序打包的数据,并没有压缩过. .tar. ...
- Linux第八章:文件,文件系统的压缩,打包备份
压缩:gzip -v 文件名 1:压缩后成 文件名.gz 的压缩文件,原文件消失 2:压缩的文件可以直接使用zcat 文件名.gz 读取里面的内容 解压缩: gunzip 文件名.gz 替 ...
- Linux系统(一)文件系统、压缩、打包操作总结
序言 当前的形势,.Net已经开源,.Net Core 正在跨平台,可见微软巨人在努力,在改变,在进步,在走向春天.从前被微软供作上帝的.Net从业者,如果不打开心扉面向开源,改变自己,那么很可能在不 ...
- 文件与文件系统的压缩与打包 tar gzip bzip2
1:linux下常见的压缩文件后缀: .gz .zip .bz2 打包后的: .tar.gz .tar.zip .tar.bz2 2:gzip: 压缩:gzip file 解压:gunzip file ...
随机推荐
- codeForces 574b Bear and Three Musketeers
一种巧妙到暴力方式,这题到抽象化:在一个无向图中,找一个度数和最小到三阶到圈. 首先对边进行枚举,这样确定了两个顶点,然后在对点进行枚举,找到一个可以构成三元圈到点,则计算他们到度数和. 最后保存最小 ...
- 名词解释 ssl、tls、key、crt、cer、x509
1. SSL:安全套接字层 Secure Socket Layer的缩写 2. TLS:传输层安全协议 Transport Layer Security的缩写 ,SSL的继任者 3. KEY:通常指 ...
- 目录视图摘要视图订阅 基于Extjs开发不允许为空的文本框提示及相应的验证错误提示(转)
原文地址:http://blog.csdn.net/kunoy/article/details/8007585 本文主要解决问题: 1.区分哪些文本框不允许为空,很多网站都采用在文本框后加*号,ext ...
- [转]Tomcat和Apache的区别和联系
Apache,指的是Apache软件基金会下的一个项目——Apache HTTP Server Project:Nginx同样也是一款开源的HTTP服务器软件(当然它也可以作为邮件代理服务器.通用的T ...
- R语言中将hello打印10次的两种方法
我们有两种方法来做这件事情: 1.for结构 for循环重复的执行一个语句,直到某个变量的值不再包含在序列seq中为止. 语法: for (var in seq) statement 例如: > ...
- java程序调用kettle
(1).将相应的kettle的jar包导入的java项目,主要的jar包有一下几个. (2).java程序. package cn.com.taiji.oosweb.test.web; import ...
- 下列哪一个接口定义了用于查找、创建和删除EJB实例
下列哪一个接口定义了用于查找.创建和删除EJB实例 A.Home B.Remote C.Local D.Message 解答:A remote接口定义了业务方法,用于EJB客户端调用业务方法. hom ...
- Disillusioning #1 水题+原题赛(被虐瞎)
https://vijos.org/tests/542c04dc17f3ca2064fe7718 好一场 水题 比赛啊 t1直接上暴力费用流10分QAQ,虽然一开始我觉得可以不用的,直接dfs可以得出 ...
- php form 图片上传至服务器上
本文章也是写给自己看的,因为写的很简洁,连判断都没有,只是直接实现了能上传的功能. 前台: <form action="upload.php" method="PO ...
- servlet各版本区别以及dynamic web module 版本之间的区别
java的web系统有多种类型,比如静态的和动态的,然后动态的java web project要设置dynamic web module,也就是动态网页模型,他必须要喝对应的服务器搭配好了才能跑,今天 ...