文件的打包與壓縮
打包:是指將一些文件或目錄,打包成一個總的文件
壓縮:是指將一個大的文件通過一些壓縮算法變成壓縮成一個相對較小的文件
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 ##不忽略設備文件