文件的打包与压缩
打包:是指将一些文件或目录,打包成一个总的文件
压缩:是指将一个大的文件通过一些压缩算法变成压缩成一个相对较小的文件
linux上的压缩包文件格式,除了Windows上最常见的*.zip、*.rar、*.7z后缀的压缩文件以外,还有.gz、.xz、.bz2、.tar、.tar.gz、.tar.xz、.tar.bz2等格式
文件后缀
*.zip ## zip 程序打包压缩的文件
*.rar ## rar 程序压缩的文件
*.7z ## 7zip 程序压缩的文件
*.tar ## tar 程序打包,未压缩的文件
*.gz ## gzip 程序(GNU zip)压缩的文件
*.xz ## xz 程序压缩的文件
*.bz2 ## bzip2 程序压缩的文件
*.tar.gz ## tar 打包,gzip 程序压缩的文件
*.tar.xz ## tar 打包,xz 程序压缩的文件
*.tar.bz2 ## tar 打包,bzip2 程序压缩的文件
*.tar.7z ## tar 打包,7z 程序压缩的文件
打包压缩工具:tar
在linux上最常用的一个打包工具,tar通过对gzip、xz、bzip2等压缩工具的支持,实现了通过不同的参数,进行打包和压缩的操作,比较方便
打包:
参数:
-c ##创建一个tar包
-f ##指定tar包的文件名,文件名需紧跟在-f参数后
-t ##显示tar包中的内容,不解压
-r ##向tar包中添加文件
--get ##从tar包中取出指定文件
--delete ##删除tar包中的指定文件
-x ##取出tar包中的所有文件
-C ##指定解压缩目录
试验:
[root@localhost tmp]# touch file{1..5} ##创建测试文件
[root@localhost tmp]# ls
file1 file2 file3 file4 file5
[root@localhost tmp]# tar -cf file.tar file* ##将测试文件打包
[root@localhost tmp]# ls
file1 file2 file3 file4 file5 file.tar
[root@localhost tmp]# tar tf file.tar ##查看tar包的内容,不解压
file1
file2
file3
file4
file5
[root@localhost tmp]# rm -rf file{1..5} ##删除测试文件
[root@localhost tmp]# ls
file.tar
[root@localhost tmp]# tar -f file.tar --get file5 ##从tar包中取出file5
[root@localhost tmp]# ls
file5 file.tar
[root@localhost tmp]# tar -f file.tar --delete file5 ##将tar包中的file5文件删除
[root@localhost tmp]# tar -tf file.tar
file1
file2
file3
file4
[root@localhost tmp]# tar -rf file.tar file5 ##将file5文件放入tar包中
[root@localhost tmp]# tar -tf file.tar
file1
file2
file3
file4
file5
[root@localhost tmp]# mkdir file ##创建一个文件夹
[root@localhost tmp]# tar -xf file.tar -C file ##将tar包中的文件解压至指定文件夹
[root@localhost tmp]# ls file
file1 file2 file3 file4 file5
##保留文件属性和跟随链接(符号链接或软链接)
##有时候我们使用 tar 备份文件,当你在其他主机还原时希望保留文件的属性(-p 参数)
##和备份链接指向的源文件而不是链接本身(-h 参数):
[root@localhost tmp]# rm -rf *
[root@localhost tmp]# tar -cphf etc.tar /etc
tar: Removing leading `/' from member names
tar: Removing leading `/' from hard link targets
tar: /etc/grub2.cfg: File removed before we read it
[root@localhost tmp]# ls
etc.tar
##打包过程中会自动去掉绝对路径符/,可以使用(-P参数)保留绝对路径符
##可视化,创建过程可见(-v参数)
压缩:在打包解包过程中,添加相应压缩格式参数,即可实现对打包文件的压缩
压缩文件格式 参数
*.tar.gz -z
*.tar.xz -J
*.tar.bz2 -j
试验:
[root@localhost tmp]# tar czf etc.tar.gz /etc
[root@localhost tmp]# tar cJf etc.tar.xz /etc
[root@localhost tmp]# tar cjf etc.tar.bz2 /etc
[root@localhost tmp]# file etc.tar.gz etc.tar.xz etc.tar.bz2
etc.tar.gz: gzip compressed data, from Unix, last modified: Fri Apr 20 16:40:15 2018
etc.tar.xz: XZ compressed data
etc.tar.bz2: bzip2 compressed data, block size = 900k
##Linux系统中,文件扩展名是为了标记文件的类型,方便识别,文件实际格式需要通过file命令查看
[root@localhost tmp]# du -sh etc* /etc ##查看打包与压缩后文件的大小区别
194M etc.tar
9.7M etc.tar.bz2
12M etc.tar.gz
7.6M etc.tar.xz
43M /etc
文件的远程传输1、scp远程复制,是基于ssh的登录进行安全的远程文件拷贝命令
scp FILE|DIR USER@IP:/DIR ##上传文件
scp USER@IP:/FILE|DIR /DIR ##下载文件
##如果传输的是目录,需要添加-r参数
2、rsync远程同步,同步文件的同时可以保持原有文件的权限、时间、软硬链接等信息
rsync FILE|DIR USER@IP:/DIR
rsync USER@IP:/FILE|DIR /DIR
参数:
-r ##同步目录时使用
-l ##不忽略链接
-p ##不忽略文件权限
-t ##不忽略文件时间戳
-g ##不忽略文件所属组
-o ##不忽略文件所有人
-D ##不忽略设备文件