Linux compression command and decompression script

Time:2021-2-23
suffix tool compress decompression
.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

The decompression command of Linux is too cumbersome, even the old driver of operation and maintenance will feel headache. Here I share a shell script, which can decompress the following files with one click:

.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, .cba

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 "[email protected]"
    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

Installation steps:

  1. Edit the. Bashrc file:vim ~/.bashrc
  2. Copy the shell code to the. Bashrc file
  3. Start bash shell:source ~/.bashrc

Decompression test:

  1. Download Python source code package:wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
  2. Decompression tar.xz File:extract Python-3.7.0.tar.xz