ホームページ  >  記事  >  運用・保守  >  Linuxファイルのパッケージ化と圧縮の方法は何ですか?

Linuxファイルのパッケージ化と圧縮の方法は何ですか?

WBOY
WBOY転載
2023-05-17 16:56:472640ブラウズ

パッケージ化と圧縮

ファイルまたはフォルダーをパッケージに結合し、圧縮アルゴリズムを通じてデータを圧縮して、パッケージのサイズを削減し、ネットワーク送信を容易にします。

windows:
  zip
  rar

linux:
  zip
  tar
  gz
  bz2
  tar.gz
  tar.bz2

压缩算法:
  gzip
  bzip2

zip

は、Windows および Linux で一般的に使用されるパッケージ化および圧縮ツールです。サポートされている圧縮アルゴリズムは zip です。

zip工具需要安装
  yum install zip

zip ファイルを圧縮します

# 格式
  zip [参数] 压缩包名称  文件路径

[root@abc ~]# zip 123.zip 123.log 
  adding: 123.log (deflated 87%)
[root@abc ~]# ls -l

zip 圧縮フォルダー

# 需要一个-r参数去递归压缩文件夹下的所有内容
[root@abc ~]# zip -r dir.zip dir/
  adding: dir/ (stored 0%)
  adding: dir/one/ (stored 0%)
  adding: dir/123.log (deflated 87%)

zip のサイレント出力

# -q:参数就是不输出任何打包信息
[root@abc opt]# zip -r -q etc.zip /etc/
[root@abc opt]# ls -l

zip 解凍コマンド (unzip)

# 格式
  unzip [参数] 压缩包路径

# unzip解压命令只能解压由zip打包的压缩文件
[root@abc ~]# unzip dir.zip 
Archive:  dir.zip
  inflating: dir/123.log             
[root@abc ~]# 

# 其他压缩包由unzip解压时随即报错。
[root@abc opt]# unzip nginx-.tar.gz
Archive:  nginx-.tar.gz
  End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.
unzip:  cannot find zipfile directory in one of nginx-.tar.gz or
        nginx-.tar.gz.zip, and cannot find nginx-.tar.gz.ZIP, period.


# 查看压缩包中压缩那些内容,不解压?
# 只查看压缩包内容不解压需要使用 -l 参数
[root@abc opt]# unzip -l dir.zip 
Archive:  dir.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  03-11-2021 12:04   dir/
---------                     -------
        0                     1 file

# 解压到指定目录(-d)
[root@abc ~]# unzip -d /root/  etc.zip 
[root@abc opt]# cd /root/
[root@abc ~]# ls
]        anaconda-ks.cfg  dir.zip  index.html           test.pdf.gz  xxxeth0xxx           系统优化.md
123.log  demo.txt         etc      nginx-0.1.22.tar.gz  test.txt     上传与下载.md
123.zip  dir              eth0xxx  test                 xxxeth0      文件管理_(高级).pdf

# 静默输出(-q)
[root@abc ~]# rm -rf etc
[root@abc ~]# unzip -q -d /root/ /opt/etc.zip 
[root@abc ~]# ls -l

tar

tar 圧縮は複数の圧縮アルゴリズムをサポート

tar.gz gzip (最も一般的に使用される)

tar.bz2 bzip2

gzip

gzip 圧縮アルゴリズムを使用して、ファイルを特定のボリュームに圧縮します。これは、送信に役立ちます。パッケージ化はサポートされていません

[root@abc ~]# ls -l
total 4828
-rw-r--r--   1 root root  244977 Mar 10 12:12 index.html
[root@abc ~]# gzip index.html 
[root@abc ~]# ls -l
gzip 圧縮ディレクトリ
[root@abc etc]# gzip -r /etc
[root@abc etc]# ls

gzip 解凍 (-d)

[root@abc ~]# ls -l
-rw-r--r--   1 0 0   22652 Mar 10 12:12 index.html.gz
[root@abc ~]# gzip -d index.html.gz 
[root@abc ~]# ls -l

bzip2

bzip2 圧縮アルゴリズムを使用して、特定のボリュームのファイルを圧縮します。

[root@abc ~]# ls -l
total 4828
-rw-r--r--   1 root root  646165 Mar  9 10:31 123.log     
[root@abc ~]# bzip2 123.log 
[root@abc ~]# ls -l
total 4240
-rw-r--r--   1 root root       0 Mar 10 12:04 ]

bzip2 解凍 (-d)

bzip2 解凍は、bzip2 で圧縮された圧縮パッケージを解凍するためのものです。

[root@abc ~]# ls -l
total 4240
-rw-r--r--   1 root root   42210 Mar  9 10:31 123.log.bz2
[root@abc ~]# bzip2 -d 123.log.bz2 
[root@abc ~]# ls -l

tar

tar は実際にはパッケージ化ツールであり、圧縮関数はありませんが、パラメータを使用して圧縮ツールを呼び出して解凍することができます。

tar参数
  -c : 创建压缩
  -f ; 指定压缩包名称
  -z : 使用gzip压缩工具进行压缩
  -j : 使用bzip2压缩工具进行压缩
  -J : 使用xz压缩工具进行压缩
  -t : 显示压缩包内容,不解压
  -v : 显示压缩过程
  -P : 允许使用绝对路径进行压缩
  -x : 解压
  -C : 指定解压路径
  -h : 打包软连接
  --exclude : 排除某些文件
  --exclude-from :
パラメータ
  • -c: 圧縮パッケージの作成

  • -f: 圧縮パッケージ名を指定

[root@abc ~]# tar -c -f test.tar 123.log 
[root@abc ~]# ls -l
  • -z: 圧縮に gzip 圧縮ツールの使用を指定します

[root@abc ~]# tar  -c -z -f test-one.tar 123.log 
[root@abc ~]# ls -l 
total 5084
-rw-r--r--   1 root root   85279 Mar 11 15:56 test-one.tar

# 注:使用-z参数,不会自动添加.gz后缀

[root@abc ~]# tar -c -z -f anaconda.tar.gz  anaconda-ks.cfg 
[root@abc ~]# ls -l
  • -j: 圧縮に bzip2 圧縮ツールを使用することを指定します

  • #
[root@abc ~]# tar -c -j -f 123-bask-one.tar 123.log 
[root@abc ~]# ls -l
    #-J: 圧縮に xz 圧縮ツールの使用を指定します
  • #

    [root@abc test-tar]# tar -c -J  -f etc.tar.xz /etc/
    [root@abc ~]# ls -l
-t: 圧縮パッケージの内容を表示します
  • ##
    [root@abc ~]# tar -t -f 123-bak.tar.bz2 
    123.log
    [root@abc ~]#

    #-v: 圧縮パッケージの圧縮プロセスを表示します。圧縮パッケージ
    #
[root@abc ~]# tar -x -v -f etc.tar -C /opt/
  • -P: パッケージ化に絶対パスの使用を許可します
  • #
    [root@abc ~]# tar -c -P -f 123-three.tar /etc/passwd
    [root@abc ~]# tar -c -f 123-three.tar /etc/passwd
    tar: Removing leading `/' from member names
    [root@abc ~]#
    • #-x: 解凍

    # tar解压是按照原来的路径进行解压
    [root@abc test]# tar -x -f etc.tar 
    
    # tar会自动识别压缩功能
    • -C: 解凍パスを指定

    [root@abc ~]# tar -x -f etc.tar -C /opt/
    tar: Removing leading `/' from member names
    [root@abc ~]# cd /opt/
    [root@abc opt]# ls
    abc23  dir  dir.zip  etc  nginx-0.1.22.tar.gz  nginx-.tar.gz  xxx
    [root@abc opt]#
    • -exclude: 特定のファイルを除外します

    [root@abc test-tar]# tar -c -f abc.tar ./* --exclude=abc7 --exclude=abc5   --exclude=abc1 
    [root@abc test-tar]# tar -t -f abc.tar 
    ./abc2
    ./abc3
    ./abc4
    ./abc6
    ./abc8
    ./abc9
    [root@abc test-tar]#
    • –exclude-from: 特定のファイル リストに基づいて複数のファイルを除外します

    [root@abc test-tar]# cat list.txt 
    abc995
    abc996
    abc997
    abc998
    abc999
    [root@abc test-tar]# tar -c -f abc.tar ./* --exclude-from=list.txt
    • -h: パッケージ ソフト リンク

    [root@abc test-tar]# tar -c -h -f bin-h.tar /bin

    以上がLinuxファイルのパッケージ化と圧縮の方法は何ですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

    声明:
    この記事はyisu.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。