1. 程式人生 > >打包壓縮命令(十一)

打包壓縮命令(十一)

windows 打包工具 詳細信息 源文件 壓縮文件

打包壓縮命令:tar,zip,gzip,bzip2

常用的打包工具:tar;

壓縮工具:zip、gzip、bzip2


11.1.zip

功能:兼容unix和windows,可以一次性壓縮多個文件

語法:zip 壓縮後的路徑文件 需要壓縮的文件1 文件2

常用選項:

-r:遞歸壓縮


解壓縮:

unzip 需要解壓的文件 (默認解壓到當前目錄)

unzip 需要解壓的文件 -d 解壓後的路徑

示例:

[[email protected] tmp]# zip /tmp/123.zip /etc/*
[[email protected] tmp]# unzip 123.zip -d test/
[[email protected]
/* */ tmp]# ls test/ etc

11.2.gzip

功能:只能壓縮單個文件,壓縮速度相對較快,壓縮率較低,cpu消耗相對較低

語法:gzip 需要壓縮的文件

gzip 需要壓縮的文件1 需要壓縮的文件2

常用選項:

-c:保留源文件壓縮和解壓縮

-v:打印詳細信息

-d 解壓縮的參數

-t 檢查壓縮文件,看是否有錯誤

-# 壓縮等級1-9,越高越好,但也越慢,默認是6


解壓縮:

gunzip 需要解壓的文件

gzip -d 需要解壓的文件

查看壓縮文件:

zcat


問題1:gzip工具能不能壓縮一個目錄?

答案是不能的


示例:

[[email protected] tmp]# gzip *   #壓縮當前目錄下的所有文件
[[email protected]
/* */ tmp]# ll #只有目錄沒有壓縮 total 207200 -rw-r--r--. 1 0 0 32 May 20 13:33 1addf98dffa.gz -rw-r--r--. 1 0 0 31 May 20 13:35 1ddf67dfdf.gz -rw-r--r--. 1 0 0 71460 Jun 4 11:49 20170609.tar.gz -rw-r--r--. 1 0 0 31 May 20 13:33 3dfdf7ghhn.gz drwxr-xr-x. 2 0 0 4096 Jun 4 11:30 etc [[email protected]
/* */ tmp]# gzip -d * #解壓當前目錄的所有文件

11.3.bzip2

功能:只能壓縮單個文件,壓縮率相對較高,壓縮速度相對慢 cpu消耗相對較高

語法:

bzip2 需要壓縮的文件

bzip2 需要壓縮的文件1 需要壓縮的文件2

常用選項:

-c 將壓縮過程中產生的數據輸出到屏幕上

-d 解壓縮參數

-k 保留原文件

-z 壓縮的參數

-v 顯示壓縮比

-# 壓縮等級1-9,和gzip一樣


解壓縮:

bunzip2 需要解壓的文件

bzip2 -d 需要解壓的文件

查看壓縮文件

bzcat


示例:

[[email protected] test]# bzip2 -v9 serial.conf   #打印壓縮比
  serial.conf:  1.784:1,  4.485 bits/byte, 43.93% saved, 1302 in, 730 out.
[[email protected] test]# ls serial.conf.bz2 
serial.conf.bz2
[[email protected] test]# ls serial.conf   #但是原文件沒有了
ls: cannot access serial.conf: No such file or director
[[email protected] test]# bzip2 -z -k serial.conf    #加-k就保留了原文件
[[email protected] test]# ls serial.conf*
serial.conf  serial.conf.bz2

11.4.tar

功能:打包工具

語法:tar [OPTION...] [FILE]...

參數:

-c:創建一個tar包

--delete : 從歸檔文件 (而非磁帶) 中刪除

-f:指定包名

-z:調用gzip壓縮工具壓縮

-j:調用bzip2壓縮工具壓縮

-v:顯示詳細信息

-x:解壓

-t:查看tar包內容

-r:追加文件到tar包

-J:調用xz壓縮工具

-C:指定解壓的路徑

-p :使用原文件的原來屬性(屬性不會依據使用者而變)

-P :可以使用絕對路徑來壓縮!

說明:

參數前面-可有可無

示例:

#把/etc目錄下的文件全部打包成為/tmp/etc.tar,打包壓縮名可以隨便起,常規是以tar,便於區分
[[email protected] ~]# tar cvf /tmp/etc.tar /etc    #只打包,不壓縮
[[email protected] ~]# ll /tmp/etc.tar 
-rw-r--r--. 1 root root 27013120 Jun  4 16:50 /tmp/etc.tar
[[email protected] ~]# tar zcvf /tmp/etc.tar.gz /etc   #打包並以gzip壓縮,.tgz也是一樣
[[email protected] ~]# ll /tmp/etc.tar.gz
-rw-r--r--. 1 root root 9181726 Jun  4 16:52 /tmp/etc.tar.gz
[[email protected] ~]# tar jcvf /tmp/etc.tar.bz2 /etc   #打包並以bzip2壓縮,.tbz2也是一樣
[[email protected] ~]# ll /tmp/etc.tar.bz2 
-rw-r--r--. 1 root root 8161113 Jun  4 16:55 /tmp/etc.tar.bz2
#如何查看打包或者壓縮文件裏都有哪些文件
[[email protected] tmp]# tar tf etc.tar.gz    #帶t就是查看,帶f是指定報名
[[email protected] tmp]# tar tvf etc.tar.gz  #v是詳細信息,就想ls -l的長格式打印
#解壓tar包到指定路徑
[[email protected] tmp]# tar xf etc.tar.gz -C /home/test  #-C是指定路徑
#往tar包追加內容
[[email protected] tmp]# tar -r /var  -f etc.tar.gz  #實踐證明不能對壓縮文件進行追加
tar: Cannot update compressed archives
tar: Error is not recoverable: exiting now
[[email protected] tmp]# tar -r /var/cache  -f etc.tar   #對打包的tar追加報錯
tar: Removing leading `/‘ from member names
[[email protected] tmp]# tar -rP /var  -f etc.tar #追加進去了,這裏用到了P選項
#釋放tar包指定文件到指定目錄
[[email protected] etc]# tar xvf etc.tar etc/passwd -C /tmp
[[email protected] tmp]# tar xvf etc.tar.gz etc/passwd -C /tmp  #壓縮文件也是一樣的方式
etc/passwd
#排除文件後再壓縮文件
[[email protected] tmp]# tar zcvf /tmp/var.tar.tgz /var --exclude=*.html
[[email protected] tmp]# tar tf var.tar.tgz |grep -E "*.html"  #過濾出來的,但不是以.html結尾的
var/www/error/contact.html.var
var/www/error/HTTP_FORBIDDEN.html.var
var/www/error/HTTP_VARIANT_ALSO_VARIES.html.var

註意:

tar工具盡量使用相對路徑,參數f最好放到最後


11.5 練習:


1、找出/etc下面以.conf結尾的文件,並將其復制到/home下的backup目錄中、

[[email protected] tmp]# mkdir /home/backup
[[email protected] tmp]# find /etc -type f -name "*.conf" -exec cp {} /home/backup \;
[[email protected] tmp]# ls /home/backup/
10-autohint.conf                   kexec-disable.conf
10-no-sub-pixel.conf               krb5.conf
10-sub-pixel-bgr.conf              ldap.conf
10-sub-pixel-rgb.conf              ld.so.conf

2、將/home/backup下的所有文件全部打包並壓縮到/tmp目錄下名字為“5天以後的日期.tar.gz”

[[email protected] tmp]# tar czvf /tmp/$(date +%Y%m%d --date=‘5 days‘).tar.gz /home/backup/*
[[email protected] tmp]# date +%Y%m%d
20170604
[[email protected] tmp]# ll
total 653284
-rw-r--r--. 1 root root         0 May 20 21:33 1addf98dffa
-rw-r--r--. 1 root root         0 May 20 21:35 1ddf67dfdf
-rw-r--r--. 1 root root     71460 Jun  4 19:49 20170609.tar.gz

3、將/tmp下剛剛壓縮的包解壓到新建目錄/tmp/test中,並打包成“當前的系統時間.tar”

[[email protected] backup]# mkdir test
[[email protected] backup]# tar xf 20170609.tar.gz -C test/
[[email protected] backup]# tar cvf ./$(date +%T).tar *
[[email protected] backup]# ls *.tar
19:56:09.tar

4、將/tmp/find.test文件追加到剛剛打好的tar包裏

[[email protected] tmp]# cd /tmp
[[email protected] tmp]# touch find.test
[[email protected] tmp]# tar -r find.test -f /tmp/test/home/backup/19\:56\:09.tar
[[email protected] tmp]# tar tf /tmp/test/home/backup/19\:56\:09.tar
xcompose.conf
xim.conf
yum.conf
find.test


本文出自 “爛筆頭” 博客,請務必保留此出處http://lanbitou.blog.51cto.com/9921494/1932163

打包壓縮命令(十一)

相關推薦

打包壓縮命令

windows 打包工具 詳細信息 源文件 壓縮文件 打包壓縮命令:tar,zip,gzip,bzip2常用的打包工具:tar;壓縮工具:zip、gzip、bzip211.1.zip功能:兼容unix和windows,可以一次性壓縮多個文件語法:zip 壓縮後的路徑文件 需要壓縮的文件1

Linux命令——Shell程序設計二循環控制語句

變量 while語句 邏輯 邏輯操作符 字符 內容 循環控制 bre case語句 1.if語句 (1)兩路分支的if語句 (2)多路條件判斷分支的if語句 2.測試語句 (1)文件測試 (2)字符串測試 (3)數值測試 (4)用邏輯操作符進行組合的測試語句 3.case語

Linux常用命令計劃任務管理

侯良金 linux 任務計劃 crontab 基本命令 Linux常用命令(十一)計劃任務管理RHEL6系統中默認已安裝了at、cronie軟件包,通過atd和crond這兩個系統服務實現一次性、周期性計劃任務的功能,並分別通過at、crontab命令進行計劃任務設置。一、at一次性任務設

liunx基本命令系統日誌管理

一、系統日誌預設分類 /var/log/messages ##系統服務及日誌,包括服務的資訊,報錯等等/var/log/secure ##系統認證資訊日誌/var/log/maillog ##系統郵件服務資訊/var/log/cron ##系統定時任務資訊/var/log/

linux命令:nl命令

nl命令在linux系統中用來計算檔案中行號。nl 可以將輸出的檔案內容自動的加上行號!其預設的結果與 cat -n 有點不太一樣, nl 可以將行號做比較多的顯示設計,包括位數與是否自動補齊 0 等等的功能。 1.命令格式: nl [選項]... [檔案]...

Centos之幫助命令

play ken -h spa ... prop 雜項 als exp 幫助命令man (manual) 比如我們可以看下man命令的解釋 [root@localhost ~]# man man MAN(1)

Linux命令 打包或解壓文件 tar

常用 特定 -- 返回 相關信息 href -c 壓縮文件 標識 目錄 1.命令簡介 2.常用參數介紹 3.實例 4.直達底部 命令簡介 tar 命令用於將文件打包或解壓,擴展名一般為 ".tar" ,指定特定的參數可以調用 gzip 或 bzip2 制作壓縮包或解

Linux命令 壓縮或解壓縮文件和目錄 gzip gunzip

配置 硬鏈接 名稱 log logs 壓縮文件 mark 底部 linux 目錄 1.命令簡介 2.常用參數介紹 3.實例 4.直達底部 命令簡介 和 zip 命令類似,gzip 用於文件的壓縮,gzip壓縮後的文件擴展名為 ".gz",gzip默認壓縮後會刪除源文

WPF自學入門WPF MVVM模式Command命令

tps xaml 使用 company change 用戶 通過命令 AC 聯系人 在WPF自學入門(十)WPF MVVM簡單介紹中的示例似乎運行起來沒有什麽問題,也可以進行更新。但是這並不是我們使用MVVM的正確方式。正如上一篇文章中在開始說的,MVVM的

linux系列:nl命令

調整 寫到 實例 空行 格式 指定格式 指定 所有 tab 1、命令格式:   nl [選項] [文件] 2、命令功能:   nl(Number of Lines) 將指定的文件添加行號標註後寫到標準輸出。如果不指定文件或指定文件為"-" ,程序將從標準輸入讀取數據。

docker 學習 映象常用命令

1 建立賬戶,建立倉庫      首先在dockerhub上有自己的賬戶,然後建立一個repository(如上圖), 然後建立一個名字為robinfei/consumer的倉庫。   2  本地映象打標籤(比如本地有個映象叫consumer:late

docker 學習 鏡像常用命令

posit ont 倉庫 login 鏡像 tag dockerhub 下載 repos 1 創建賬戶,創建倉庫   首先在dockerhub上有自己的賬戶,然後創建一個repository(如上圖), 然後創建一個名字為robinfei/consumer的倉庫。

Go語言基礎—— 實現命令列引數對資料的簡單查詢小案例

案例要求: 結合命令列引數、複合型別、包管理、排序等知識,寫一個商品管理系統 商品屬性包括:整型商品ID(gid)、字串型名稱(name)、整型類別(cid)、浮點型價格(price) 自己造一些假資料,實現如下功能: shop.exe -cmd single -gi

Linux 學習之路壓縮歸檔以及RAID

壓縮、解壓縮命令 壓縮格式:gz,bz2,xz,zip,Z 壓縮演算法不同,壓縮比(壓縮後的大小-壓縮前的大小/壓縮前的大小)可能也會不同。 compress:FILENAME.Z uncompress 只能壓縮檔案,預設會刪除原檔案保留壓縮後文件: gzip

耳朵——Linux綜合命令測試

#1.顯示當前主機系統資訊,包括主機名,IPv4地址,作業系統版本,核心版本,CPU型號,記憶體大小,硬碟大 小,儲存指令碼 為sys_info.sh #!/bin/bash info1=hostname #查詢主機名 echo "l

Redis叢集JedisCluster實現keys命令方法

由於JedisCluster叢集類不支援像單機模式下的keys方法,下面的類我們就模擬keys命令方法: 一、獲取叢集類 package redis.v1.client.server; import java.util.ArrayList; import java.

Linux命令 壓縮或解壓縮檔案和目錄 gzip gunzip

目錄 3.例項 命令簡介 和 zip 命令類似,gzip 用於檔案的壓縮,gzip壓縮後的副檔名為 ".gz",gzip預設壓縮後會刪除原始檔。 gunzip用於解壓經過 gzip 壓縮過的檔案。 返回目錄 常用引數介紹 一、gzip 常用引數說明

linux常用命令—— 磁碟管理

1、cd cd(change directory) 功能說明:切換目錄。 語  法:cd [目的目錄] 補充說明:cd指令可讓使用者在不同的目錄間切換,但該使用者必須擁有足夠的許可權進入目的目錄。 2、df df(disk free) 功能說明:

超級終端下必會的命令大全之功能最強在的編輯器——vi

功能最強在的編輯器——vivi是所有UNIX系統都會提供的螢幕編輯器,它提供了一個視窗裝置,通過它可以編輯檔案。當然,對UNIX系統略有所知的人,或多或少都覺得vi超級難用,但vi是最基本的編輯器,所以希望讀者能好好把它學起來,以後在UNIX世界裡必將暢行無阻、遊刃有餘,因

Redis原始碼剖析和註釋--- 雜湊鍵命令的實現(t_hash)

Redis 雜湊鍵命令實現(t_hash) 1. 雜湊命令介紹 Redis 所有雜湊命令如下表所示:Redis 雜湊命令詳解 序號 命令及描述 1 HDEL key field2 [field2]:刪除一個或多個雜湊表字段