[Linux]指令tar、gz查詢、壓縮、解壓縮

  • 978
  • 0
linux指令tar、gz查詢、壓縮、解壓縮
參數說明:
-z :是否同時具有 gzip 的屬性
-x :解開一個壓縮檔案的參數指令
-t :查看 tarfile 裡面的檔案
-c :建立一個壓縮檔案的參數指令
-v :壓縮的過程中顯示檔案
-f :使用檔名,請留意,在 f 之後要立即接檔名喔!不要再加參數!
   例如使用『 tar -zcvfP tfile sfile』就是錯誤的寫法,要寫成『 tar -zcvPf tfile sfile』
-p :使用原檔案的原來屬性(屬性不會依據使用者而變)
-P :可以使用絕對路徑
-N :比後面接的日期(yyyy/mm/dd)還要新的才會被打包進新建的檔案中!

tar壓縮:
[root @test /root]# tar -zcvf directory.tar.gz directory
將目錄打包外,同時 gzip 壓縮
[root @test /root]# tar -zcvf filename.tar.gz /home/test/*
將 /home/test/ 這個目錄下的檔案全部打包並壓縮成為一個 filename.tar.gz 的檔案

tar解壓縮:
[root @test /root]# tar -zxvf directory.tar.gz
解加上 gzip 的壓縮的結果
[root @test /root]# tar -zxvf directory.tar.gz files/file01/file.txt
解加上 gzip 的壓縮的結果,指定路徑解壓縮

查詢目錄(tar.gz):
[root @test /root]# tar –ztvf directory.tar.gz
這個 t 可以用來查看 tar 裡面的檔案
[root @test /root]# tar -tf directory.tar.gz
列出所有檔案
[root @test /root]# tar -tf directory.tar.gz | grep 20211123
所有檔案列出含有20211123檔案

查詢目錄(.gz):
[root @test /root]# less directory.gz
檔案僅gz無tar可用less
[root @test /root]# less directory(*可以模糊一併查詢).gz | grep 20211123
所有檔案列出含有20211123檔案

gzip壓縮/解壓縮
[root @test /root]# gzip filename.log
[root @test /root]# gzip -d filename.log.gz