檔案壓縮和解壓——gzip
現在test目錄下有一個檔案test_txt和一個目錄tar-test,tar-test目錄下也有一個檔案,使用gzip對檔案test_txt進行壓縮和解壓。
[lx@localhost test]$ ls
tar-test test_txt
[lx@localhost test]$ ls ./tar-test/
test_txt
[lx@localhost test]$ gzip test_txt
[lx@localhost test]$ ls
tar-test test_txt.gz
[lx@localhost test]$ gzip tar-test/
gzip: tar-test/ is a directory -- ignored //gzip隻能對檔案進行壓縮
[lx@localhost test]$ gzip -d test_txt.gz
[lx@localhost test]$ ls
tar-test test_txt
另一種壓縮比更高的壓縮指令——bzip2
和gzip操作幾乎一樣,隻是壓縮方式不同,注意字尾。
[lx@localhost test]$ ls
tar-test test_txt
[lx@localhost test]$ bzip2 test_txt
[lx@localhost test]$ ls
tar-test test_txt.bz2
[lx@localhost test]$ bzip2 -d test_txt.bz2
[lx@localhost test]$ ls
tar-test test_txt
以上兩種壓縮指令都能對單個檔案操作,tar則可以将檔案打包,也可以加入壓縮功能。
打包壓縮和解壓——tar
格式:
對于壓縮:tar [參數] 目标目标檔案名 要壓縮的目錄
對于解壓:tar [參數] 要解壓的檔案名 要解壓到哪
參數:
-c:打封包件
-x:解開包
–delete:從包中删除檔案
-r:追加檔案到包中
-t:列出打封包件的内容
-C:指定解壓、壓縮路徑
-f:指定打包後的檔案名
-j:使用bzip2格式壓縮或解壓
-z:使用gzip格式壓縮或解壓
–remove-file:打包後删除源檔案
例子:
将./tar-test/目錄打包并以gzip格式壓縮,命名為tar-test.tar.gz,命名可以随意,但是加上字尾可以讓人一目了然
[lx@localhost test]$ tar -czf tar-test.tar.gz ./tar-test/
[lx@localhost test]$ ls
tar-test.tar.gz tar-test test_txt
将tar-test.tar.gz解壓到./tar-test/目錄:
lx@localhost test]$ tar -xzf tar-test.tar.gz -C ./tar-test/
[lx@localhost test]$ ls ./tar-test
tar-test test_txt