Extracting archives is a pain on Linux. There are just so many types and so many programs to extract each type. A .tar.gz file is extracted using the program GNU tar1, but for a .zip file, you’d need unzip2. What’s that? You’ve got a .7z file? Yeah-no, can’t use either of the before mentioned extractors, you need 7-zip3. Got a .rar, you’d need… well, you get the point.

What’s more, some of these programs require flags you’ll need to use to actually extract an archive. For example, to extract a tar file, you might do something like tar xvf <filename>. For a 7z file though, it’d be 7z x <filename>. Other extraction programs don’t require flags at all though, just the name of the extraction program followed by the file to extract. This sounds simple enough, but wait… what was the name of the program to unzip .bz2 files again?

Considering I don’t actually extract archives that often, I’ll never memorize all of this. So what to do? Luckily I, like many others, stumbled across a simple function I could put in my .bashrc file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 extract () {
  if [ -f $1 ] ; then
    case $1 in
      *.tar.bz2)   tar xvjf $1    ;;
      *.tar.gz)    tar xvzf $1    ;;
      *.tar.xz)    tar xvJf $1    ;;
      *.bz2)       bunzip2 $1     ;;
      *.rar)       unrar x $1     ;;
      *.gz)        gunzip $1      ;;
      *.tar)       tar xvf $1     ;;
      *.tbz2)      tar xvjf $1    ;;
      *.tgz)       tar xvzf $1    ;;
      *.zip)       unzip $1       ;;
      *.Z)         uncompress $1  ;;
      *.7z)        7z x $1        ;;
      *.xz)        unxz $1        ;;
      *.exe)       cabextract $1  ;;
      *)           echo "$1: unrecognized file compression" ;;
    esac
  else
    echo "$1 is not a valid file"
  fi
}

What this function does is simple. In order to extract any archive, in the terminal you just issue extract <filename>, and the file gets extracted. Obviously you’d also need to install the programs listed in the second column there, for this function to work. I have found this function4 on the Arch forums by a user named sausageandeggs.

Considering I didn’t need all of those extraction methods and I wanted to shorten the extract command even more, I slightly altered it and mine looks like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
function ex () {
   if [ -f $1 ] ; then
       case $1 in
           *.tar.bz2)   tar xvjf $1    ;;
           *.tar.gz)    tar xvzf $1    ;;
           *.bz2)       bunzip2 $1     ;;
           *.rar)       unrar x $1     ;;
           *.gz)        gunzip $1      ;;
           *.tar)       tar xvf $1     ;;
           *.tbz2)      tar xvjf $1    ;;
           *.tgz)       tar xvzf $1    ;;
           *.zip)       unzip $1       ;;
           *.7z)        7z x $1        ;;
           *.xz)        tar xvJf $1    ;;
           *)           echo "I don't know how to extract '$1'..." ;;
       esac
   else
       echo "$1 is not a valid file!"
   fi
}

Sono more remembering which program name goes with which archive, or which flags are needed to extract a specific type of archive. Just ex <filename> and you’re done.

📄 Original code by sausageandeggs on the Arch Linux Forums.