Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Many times I need to extract different kinds of archived files using commad-line. But not all the time I remember the exact command for any type of file archive. So, I have to waste time and search again. How can I avoid this?

share|improve this question
    
See also atool, with which you can extract/pack multiple formats: see the answer I gave for this question: command-line-archive-manager-extracter. This question is also a duplicate of that one, so they probably should be merged. –  user76204 Aug 29 '13 at 12:00
add comment

2 Answers

up vote 4 down vote accepted

You can use the following shell script (I named it extract and I put it in ~/bin):

#!/bin/bash

if [ $# -lt 1 ];then
  echo "Usage: `basename $0` FILES"
  exit 1
fi

# I found the following function at http://unix.stackexchange.com/a/168/37944
# which I improved it a little. Many thanks to sydo for this idea.
extract () {
    for arg in $@ ; do
        if [ -f $arg ] ; then
            case $arg in
                *.tar.bz2)  tar xjf $arg      ;;
                *.tar.gz)   tar xzf $arg      ;;
                *.bz2)      bunzip2 $arg      ;;
                *.gz)       gunzip $arg       ;;
                *.tar)      tar xf $arg       ;;
                *.tbz2)     tar xjf $arg      ;;
                *.tgz)      tar xzf $arg      ;;
                *.zip)      unzip $arg        ;;
                *.Z)        uncompress $arg   ;;
                *.rar)      rar x $arg        ;;  # 'rar' must to be installed
                *.jar)      jar -xvf $arg     ;;  # 'jdk' must to be installed
                *)          echo "'$arg' cannot be extracted via extract()" ;;
            esac
        else
            echo "'$arg' is not a valid file"
        fi
    done
}

extract $@

Don't forget to make the script executable:

chmod +x ~/bin/extract

Usage:

extract file_1 file_2 ... file_n 
share|improve this answer
    
Nice one. I find that zip sometimes extracts into the current directory, when I want it it its own subdirectory. Using unzip -d basename $arg might help. –  crafter Sep 8 '13 at 20:57
add comment

The dtrx command is your friend on that matter.

It uncompresses any archive file by guessing its type. It will also make sure the files you uncompress will be put in a new directory ; avoiding messing up the current working dir with tons of files.

Install

sudo aptitude install dtrx

Usage

dtrx stuff.zip
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.