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 ...
随机推荐
- idea,eclipse创建多模块项目
新建一个maven项目 iead,新建是不选择archetype,新建好之后,pom中的 <packaging>pom</packaging>节点是默认的,如果不是要改成这这样 ...
- Centos7安装Apache Http服务器无法访问如何解决
1. 安装Apache组件 [root@mycentos shell]# yum install httpd 2. 安装成功后,检测有无httpd进程 [root@mycentos shell]# p ...
- 读取Properties文件六种方法
1.使用java.util.Properties类的load()方法 示例: InputStream in = lnew BufferedInputStream(new FileInputStream ...
- highCharts图表应用-模拟心电图
通过前两章的学习,相信大家对highcharts已经有了初步的了解.这一章将通过一个例子来模拟Highcharts如何实现经常变化的数据显示. 比如说股票的涨停.实时篮球比分以及A选手和B选手的支持率 ...
- MFC多国语言——资源副本
此随笔主要参考了http://www.cnblogs.com/xianyunhe/archive/2011/09/02/2163842.html 为软件提供多国语言的支持的具体实现方法有很多,但基本原 ...
- Oracle 常用性能监控SQL语句
1. --查看表锁 SELECT * FROM SYS.V_$SQLAREA WHERE DISK_READS > 100; 2. --监控事例的等待 SELECT EVEN ...
- ipv6修改DNS服务-首选DNS服务器:240c::6666
下一代互联网国家工程中心推出的IPv6 DNS服务 首选DNS服务器:240c::6666 备用DNS服务器:240c::6644 来自下一代互联网国家工程中心官网消息显示,日前,下一代互联网国家 ...
- win10 更新导致 VisualSVN 报0x80041024错错误
主要是WMI丢失,重新注册下就好. 运行cmd.exe, 运行语句:mofcomp "%VISUALSVN_SERVER%WMI\VisualSVNServer.mof" 修复即可 ...
- IntelliJ IDEA2017 java连接mysql数据库并查询数据
最近自己开始重新学习java基础了,做java开发不可避免要处理数据库,由于好久不写java了,对idea也有点陌生了.所以这里写篇用jdbc来连接mysql的文章 至于mysql怎么装,请自行百度 ...
- linux用户与组管理命令的基本操作
用户账号管理命令 为了提高系统的利用率,避免因多个用户共用一个root账号而造成不必要要的系统安全隐患,通常需要为 新用户添加账户.在Linux系统中,添加用户只能由超级用户来完成,也就是说,只能由r ...