1.文件归档,就是把多个文件变成一个归档文件
tar c 创建
f 指定归档文件名称
t 显示归档文件中的内容
r 向归档文件中添加文件
--get 取出单个文件
--delete 删除单个文件
x 取出归档文件中的所有内容
-C 指定解档目录
-z gz格式压缩
-j bz2格式压缩
-J xz格式压缩
[root@localhost mnt]# cd
[root@localhost ~]# cd /root/Desktop 将建立文件路径更改到桌面
[root@localhost Desktop]# tar cf etc.tar /etc/ 将/etc/文件归档到桌面
tar: Removing leading `/' from member names
[root@localhost Desktop]# tar tf etc.tar 显示归档内容
etc/
etc/fstab
etc/crypttab 展示部分内容
[root@localhost Desktop]# tar rf etc.tar westos 向文档中添加westos
[root@localhost Desktop]# rm -rf westos 删除桌面的westos
[root@localhost Desktop]# tar xf etc.tar 取出文档所有内容
[root@localhost Desktop]# tar f etc.tar --get westos 取出westos
[root@localhost Desktop]# tar f etc.tar --get etc 取出etc
[root@localhost Desktop]# tar f etc.tar --delete westos 删除归档里面的westos
[root@localhost Desktop]# rsync -r /root/Desktop/etc.tar root@172.25.254.121:/mnt/ 将归档包移动到另外一个虚拟机。
补充一个将压缩包通过rsync命令传到另外一个虚拟机上面:
2.压缩的四种格式以及具体操作:
gz
gzip etc.tar 压缩成gz格式
gunzip etc.tar.gz 解压gz格式压缩包
tar zcf etc.tar.gz /etc 把文件归档为tar并压缩成gz
tar zxf etc.tar.gz 解压并解档gz格式压缩包
bz2
bzip2 etc.tar 压缩成bz2格式
bunzip2 etc.tar.bz2 解压bz2格式压缩包
tar jcf etc.tar.bz2 /etc 把文件归档为tar并压缩成bz2
tar jxf etc.tar.bz2 解压并解档bz2格式压缩包
xz
xz etc.tar 压缩成xz格式
unxz etc.tar.xz 解压xz格式压缩包
tar Jcf etc.tar.xz /etc 把文件归档为tar并压缩成zx
tar Jxf etc.tar.xz 解压并解档xz格式压缩包
zip
zip -r etc.tar.zip etc.tar 压缩成zip格式
unzip etc.tar.zip 解压zip格式压缩包
第一种解压方式具体操作:
[root@localhost Desktop]# ls
etc.tar
[root@localhost Desktop]# zip -r etc.tar.zip etc.tar
adding: etc.tar (deflated 72%)
[root@localhost Desktop]# du -sh etc.tar.zip
8.4M etc.tar.zip
[root@localhost Desktop]# ls
etc.tar etc.tar.zip
[root@localhost Desktop]# rm -fr etc.tar
[root@localhost Desktop]# unzip etc.tar.zip
Archive: etc.tar.zip
inflating: etc.tar
[root@localhost Desktop]# ls
etc.tar etc.tar.zip
[root@localhost Desktop]# du -sh etc.tar
30M etc.tar
第二种解压方式的具体操作:
[root@localhost Desktop]# rm -fr etc.tar.zip
[root@localhost Desktop]# gzip etc.tar
[root@localhost Desktop]# ls
etc.tar.gz
[root@localhost Desktop]# du -sh etc.tar.gz
8.4M etc.tar.gz
[root@localhost Desktop]# gunzip etc.tar
[root@localhost Desktop]# ls
etc.tar
[root@localhost Desktop]# du -sh etc.tar
30M etc.tar
第三种解压方式的具体操作:
[root@localhost Desktop]# bzip2 etc.tar
[root@localhost Desktop]# ls
etc.tar.bz2
[root@localhost Desktop]# du -sh etc.tar.bz2
7.0M etc.tar.bz2
[root@localhost Desktop]# bunzip2 etc.tar.bz2
[root@localhost Desktop]# ls
etc.tar
[root@localhost Desktop]# du -sh etc.tar
30M etc.tar
第四种解压方式的具体操作:
[root@localhost Desktop]# xz etc.tar
[root@localhost Desktop]# ls
etc.tar.xz
[root@localhost Desktop]# du -sh etc.tar.xz
7.5M etc.tar.xz
[root@localhost Desktop]# unxz etc.tar.xz
[root@localhost Desktop]# ls
etc.tar
[root@localhost Desktop]# du -sh etc.tar
30M etc.tar
压缩解压一步走:
[root@localhost Desktop]# tar zcf etc.tar.gz /etc/
tar: Removing leading `/' from member names
[root@localhost Desktop]# tar jcf etc.tar.bz2 /etc/
tar: Removing leading `/' from member names
[root@localhost Desktop]# tar Jcf etc.tar.xz /etc/
tar: Removing leading `/' from member names
[root@localhost Desktop]# du -sh *
30M etc.tar
15M etc.tar.bz2
14M etc.tar.gz
7.5M etc.tar.xz
[root@localhost Desktop]# man tar
[root@localhost Desktop]# tar zxf etc.tar.gz
[root@localhost Desktop]# ls
etc etc.tar etc.tar.bz2 etc.tar.gz etc.tar.xz
[root@localhost Desktop]# rm -fr etc
[root@localhost Desktop]# tar jxf etc.tar.bz2
[root@localhost Desktop]# ls
etc etc.tar etc.tar.bz2 etc.tar.gz etc.tar.xz
[root@localhost Desktop]# tar Jxf etc.tar.xz
[root@localhost Desktop]# ls
etc etc.tar etc.tar.bz2 etc.tar.gz etc.tar.xz