Last Updated: February 25, 2016
· dideler

Easily extract files

If you're like me and you can never remember which command and options to use to extract a file, and you use the friendly interactive shell (though it shouldn't be difficult to convert for other shells), then you'll like this.

It's a function to extract files based on the extension.

Example usage: extract foo.tar.gz baz.bz2 qux.rar.

Save it as ~/.config/fish/functions/

function extract --description "Expand or extract bundled & compressed files"
  for file in $argv
    if test -f $file
      echo -s "Extracting " (set_color --bold blue) $file (set_color normal)
      switch $file
        case *.tar
          tar -xvf $file
        case *.tar.bz2 *.tbz2
          tar -jxvf $file
        case *.tar.gz *.tgz
          tar -zxvf $file
        case *.bz2
          bunzip2 $file
          # Can also use: bzip2 -d $file
        case *.gz
          gunzip $file
        case *.rar
          unrar x $file
        case *.zip *.ZIP
          unzip $file
        case *.pax
          pax -r < $file
        case '*'
          echo "Extension not recognized, cannot extract $file"
      echo "$file is not a valid file"

Want to improve it? Send me a Pull Request!

There's a great fish tutorial available if you're unfamiliar with the syntax.