034 檔案壓縮及解壓縮命令 tar

2021-10-05 21:05:40 字數 3700 閱讀 6740

tar

#將檔案打包(壓縮)成以tar.gz為字尾的檔案,也可以解壓以tar.gz為字尾的壓縮檔案

tar   引數   想要打成的包名.tar.gz  要被打包的檔名
#壓縮時常用的引數

-z #通過gzip來進行歸檔

-c #建立乙個新的歸檔(檔案)

-f #指定新的歸檔(檔案)的檔名

-v #詳細顯示壓縮或解壓縮的過程

-h #把軟鏈結檔案所指向的檔案內容也一起打包出來,而不是只是打包這個軟鏈結

#解壓時常用的引數

-x #解壓歸檔檔案

-c #解壓檔案時,指定把檔案解壓到其它路徑

#了解引數

-t #不解壓壓縮包,列出壓縮包中的內容,需要配合-f引數來使用

--exclude #排除檔案

#### 範例1:將/etc/打包成full-etc.tar.gz,打包的檔案儲存到/root/tools目錄下

[root@db01 ~]

# cd / && tar zcfh /root/tools/full-etc.tar.gz etc

[root@db01 /]

# ll /root/tools/full-etc.tar.gz

-rw-r--r-- 1 root root 12757051 7月 15 17:08 /root/tools/full-etc.tar.gz

#### 範例2:排除/etc/services檔案,將/etc/打包成part-etc.tar.gz,打包的檔案儲存到/root/tools目錄下

第一種方法:結合find來完成

[root@db01 ~]

# cd / && tar zcfh /root/tools/par-etc.tar.gz $(find etc -maxdepth 1 -type f ! -name "services")

[root@db01 ~]

# ll /root/tools/par-etc.tar.gz

-rw-r--r-- 1 root root 121696 7月 15 17:12 /root/tools/par-etc.tar.gz

第二種方法:用tar命令的排除功能

[root@db01 ~]

# cd / && tar zcfh /root/tools/par-etc1.tar.gz --exclude=etc/services etc

[root@db01 /]

# ll /root/tools/par-etc1.tar.gz

-rw-r--r-- 1 root root 12757094 7月 15 17:14 /root/tools/par-etc1.tar.gz

#### 範例3:將/root/tools/par-etc.tar.gz檔案解壓到/tmp目錄下

[root@db01 ~]

# tar xf /root/tools/par-etc.tar.gz -c /tmp/

[root@db01 ~]

# ls -ld /tmp/etc/

drwxr-xr-x 2 root root 4096 7月 15 17:17 /tmp/etc/

[root@db01 ~]

# find /tmp/etc/ -maxdepth 1 -type f -name "services"

[root@db01 ~]

##### 範例4.將/root/chenliang目錄下的檔案進行打包,只打包檔案(不是/root/chenliang目錄哈),打包人的檔案儲存到/root/tools/目錄下,檔名為file.tar.gz

[root@db01 ~]

# ll /root/chenliang/

總用量 0

-rw-r--r-- 1 root root 0 7月 15 17:15 file01

-rw-r--r-- 1 root root 0 7月 15 17:15 file02

-rw-r--r-- 1 root root 0 7月 15 17:15 file03

-rw-r--r-- 1 root root 0 7月 15 17:15 file04

-rw-r--r-- 1 root root 0 7月 15 17:15 file05

-rw-r--r-- 1 root root 0 7月 15 17:15 file06

-rw-r--r-- 1 root root 0 7月 15 17:15 file07

-rw-r--r-- 1 root root 0 7月 15 17:15 file08

-rw-r--r-- 1 root root 0 7月 15 17:15 file09

-rw-r--r-- 1 root root 0 7月 15 17:15 file10

[root@db01 ~]

# cd /root/chenliang && tar zcfh /root/tools/file.tar.gz ./*

[root@db01 chenliang]

# ll /root/tools/file.tar.gz

-rw-r--r-- 1 root root 207 7月 15 17:20 /root/tools/file.tar.gz

#### 範例5:檢視/root/tools/file.tar.gz包裡面有哪些檔案(不能解壓出來看)

[root@db01 ~]

# tar tf /root/tools/file.tar.gz

./file01

./file02

./file03

./file04

./file05

./file06

./file07

./file08

./file09

./file10

#### 範例6:將/root/tools/file.tar.gz檔案解壓到/mnt目錄下

[root@db01 ~]

# tar xf /root/tools/file.tar.gz -c /mnt/

[root@db01 ~]

# ll /mnt/

總用量 0

-rw-r--r-- 1 root root 0 7月 15 17:15 file01

-rw-r--r-- 1 root root 0 7月 15 17:15 file02

-rw-r--r-- 1 root root 0 7月 15 17:15 file03

-rw-r--r-- 1 root root 0 7月 15 17:15 file04

-rw-r--r-- 1 root root 0 7月 15 17:15 file05

-rw-r--r-- 1 root root 0 7月 15 17:15 file06

-rw-r--r-- 1 root root 0 7月 15 17:15 file07

-rw-r--r-- 1 root root 0 7月 15 17:15 file08

-rw-r--r-- 1 root root 0 7月 15 17:15 file09

-rw-r--r-- 1 root root 0 7月 15 17:15 file10

Linux命令 4 檔案壓縮解壓縮

linux檔案壓縮解壓縮命令 unzip 解壓縮命令 tar命令 1.unzip 解壓縮命令用於解壓縮.zip檔案 參考linux unzip命令 linux tar.gz tar bz2 zip 等解壓縮 壓縮命令詳解 語法 unzip 引數 zip file d 解壓縮目錄 可選引數 例子 ro...

25 檔案的壓縮和解壓縮

一 技術方案 1.第三方框架 ssziparchive 2.依賴的動態庫 libz.dylib 二 壓縮1 1.第乙個方法 zipfile 產生的zip檔案的最終路徑 directory 需要進行的壓縮的資料夾路徑 ssziparchive createzipfileatpath zipfile w...

壓縮及解壓縮

1.壓縮一組檔案為tar.gz字尾。tar cvf pgdump.tar pgdump tar cvf pgdump.tar pgdump dev null 2 1 gzip q pgdump.tar or tar cvfz pgdump.tar.gz pgdump tar cvfz pgdump....