Linux 壓縮命令與解壓腳本

後綴 工具 壓縮 解壓
.tar tar tar cvf FileName.tar DirName tar xvf FileName.tar
.gz gzip gzip FileName gzip -d FileName.gz
.bz2 bzip2 bzip2 -z FileName bzip2 -d FileName.bz2
.Z compress compress FileName compress -d FileName.Z
.xz xz xz -z FileName xz -d FileName.xz
.zip (un)zip zip FileName.zip DirName unzip FileName.zip
.rar rar rar a FileName.rar DirName rar x FileName.rar
.lha lha lha -a FileName.lha FileName lha -e FileName.lha
.tar.gz tar+ tar zcvf FileName.tar.gz DirName tar zxvf FileName.tar.gz
.tgz tar+ tar zcvf FileName.tar.gz DirName tar zxvf FileName.tar.gz
.tar.bz2 tar+ tar jcvf FileName.tar.bz2 DirName tar jxvf FileName.tar.bz2
.tar.Z tar+ tar Zcvf FileName.tar.Z DirName tar Zxvf FileName.tar.Z
.tar.xz tar+ tar Jcvf FileName.tar.xz DirName tar Jxvf FileName.tar.xz

Linux 的解壓命令太繁瑣了,即使是運維老司機也會感到頭痛,這裏我分享一個 Shell 腳本,能夠一鍵解壓如下格式的文件:python

.zip, .rar, .bz2, .gz, .tar, .tbz2, .tgz, .Z, .7z, .xz, .exe, .tar.bz2, .tar.gz, .tar.xz, .arj, .cab, .chm, .deb, .dmg, .iso, .lzh, .msi, .rpm, .udf, .wim, .xar .cpio, .cbr, .cbz, .cb7, .cbt, .cbashell

SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

function extract {
 if [ -z "$1" ]; then
    # display usage if no parameters given
    echo "Usage: extract <path/file_name>.<zip|rar|bz2|gz|tar|tbz2|tgz|Z|7z|xz|ex|tar.bz2|tar.gz|tar.xz>"
    echo "       extract <path/file_name_1.ext> [path/file_name_2.ext] [path/file_name_3.ext]"
 else
    for n in "$@"
    do
      if [ -f "$n" ] ; then
          case "${n%,}" in
            *.cbt|*.tar.bz2|*.tar.gz|*.tar.xz|*.tbz2|*.tgz|*.txz|*.tar) 
                         tar xvf "$n"       ;;
            *.lzma)      unlzma ./"$n"      ;;
            *.bz2)       bunzip2 ./"$n"     ;;
            *.cbr|*.rar)       unrar x -ad ./"$n" ;;
            *.gz)        gunzip ./"$n"      ;;
            *.cbz|*.epub|*.zip)       unzip ./"$n"       ;;
            *.z)         uncompress ./"$n"  ;;
            *.7z|*.arj|*.cab|*.cb7|*.chm|*.deb|*.dmg|*.iso|*.lzh|*.msi|*.pkg|*.rpm|*.udf|*.wim|*.xar)
                         7z x ./"$n"        ;;
            *.xz)        unxz ./"$n"        ;;
            *.exe)       cabextract ./"$n"  ;;
            *.cpio)      cpio -id < ./"$n"  ;;
            *.cba|*.ace)      unace x ./"$n"      ;;
            *)
                         echo "extract: '$n' - unknown archive method"
                         return 1
                         ;;
          esac
      else
          echo "'$n' - file does not exist"
          return 1
      fi
    done
fi
}

IFS=$SAVEIFS

安裝步驟:vim

  1. 編輯 .bashrc 文件:vim ~/.bashrc
  2. 複製 Shell 代碼到 .bashrc 文件
  3. 啓動 bash shell:source ~/.bashrc

解壓測試:bash

  1. 下載 Python 源碼包:wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
  2. 解壓 tar.xz 文件:extract Python-3.7.0.tar.xz
相關文章
相關標籤/搜索