Untar, ungz, gz, tar - how do you remember all the useful options?

Solution 1:

Or how about using the shell with advanced completion capabilities (like zsh or fresh versions of bash) which will complete the options for you, with comprehensive help? :))

Regarding tar: just look at the "qwerty" keyboard. There are letters "zxcvf" next to each other. You need either "tar czvf file.tar.gz files" or "tar xzvf file.tar.gz".

Solution 2:

Tar option summary

Y'all are welcome to edit this to add more esoteric switches but here are the basics:

  • x - extract files
  • c - create archive
  • t - list files
  • v - verbose (list files as it processes them)
  • j - use bz2 compression
  • z - use gz compression
  • f - read or write files to disk

Examples

Uncompress a tar.gz file: tar zxf tarball.tar.gz

Uncompress a tar.bz2 file: tar jxf tarball.tar.bz2

Create a tar.gz file: tar zcvf tarvall.tar.gz mydir/*

Solution 3:

There's a small Perl script called "unp".

unp filename.tar.gz

...and it extracts everything. Works with any compressed file as long as you have the right binaries. And you just forget about syntax or any of that crap. Check your Linux distribution's repositories. It should be there (at least on Arch, Debian and Ubuntu).

Solution 4:

Really with a frequent usage I make difference between extracting (x) data and compressing (c) data:

To extract:

tar xzf data.tgz

To compress:

tar czf data.tgz

Furthermore you can add two functions too your .bashrc :

function extract () {     
        if ($# -ne 1); then
                echo "Usage: $0  `<compressed archive>"`
                exit 1
        fi
        tar xzf $1
}

function compress () {
        if ($# -ne 2); then
                echo "Usage: $0 `<compressed archive> <files|directories>"`
                exit 1
        fi
        tar czf $1 $2
}

There is another nice extract function, it detect the extension of your compressed file and do the job for you:

extract () {
   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       ;;
           *.Z)         uncompress $1  ;;
           *.7z)        7z x $1        ;;
           *)           echo "don't know how to extract '$1'..." ;;
       esac
   else
       echo "'$1' is not a valid file!"
   fi
 }