14

How can I map 'untar' as a command to 'tar -xvfz' ? Sorry, but I almost always forget the arguments necessary to 'tar' for this operation.

25

alias untar='tar -xvzf'

Place in your .bashrc file to persist across logins/shell sessions, or in your /etc/bash.bashrc file to persist for logins from all users on your system.

  • 5
    Depending on your Operating system you might want to leave out the -z parameter. At least on debian tar automatically detects the compression type and using -z on a tar file that is not compressed with gzip causes an error there – Caotic Sep 19 '09 at 19:38
  • Also, the -z option is a GNU extension. – Richard Hoskins Sep 19 '09 at 19:55
  • 1
    @Richard: subby tagged it Linux @ledbettj: z option is unnecessary on recent (<4 years, at least) gnu tar, it's handled automagically and it does croak if there is not gzipping or if it's bzipp'd instead. Also "-" is unnecessary and does print a warning on occasions. – niXar Sep 20 '09 at 1:36
19

You might also be interested in the following:

x(){
    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 "Unable to extract '$1'" ;;
            esac
    else
            echo "'$1' is not a valid file"
    fi
}

With the above code in your .bashrc, typing an x followed by a filename will extract most archives you come across (assuming you have the packages needed to extract that type of archive).

NOTE: This code is slightly modified from what I found here a long time ago.

  • 1
    wow. that's awesome too. – meder omuraliev Sep 20 '09 at 20:06
  • .rar has been included twice. I don't think the second version will ever be executed. Or will it? – Wolf Sep 22 '09 at 11:46
  • My apologies, the second *.rar line should not be there; it will never be executed. – Richie Marquez Sep 22 '09 at 21:36
  • With the catchall at the end of the case block, will the else block ever trigger? – killermist Aug 17 '12 at 12:53
2

I'm always remembering it by saying it out loud:
tar e X tract Z ip F ile V erbose

  • 1
    For me consciously remembering this isn't an issue because my left hand fingers "automatically" come up with "zxfv"... :) – Jonik Sep 22 '09 at 7:38
  • @Jonik so true ^^ – Oskar Duveborn Sep 22 '09 at 7:52
1

You should try dtrx - it'll work out the correct arguments for many types of files, including "tar, zip, cpio, deb, rpm, gem, 7z, cab, rar, gz, bz2, lzma, xz, and many kinds of exe files, including Microsoft Cabinet archives, InstallShield archives, and self-extracting zip files." It also puts the contents into a single directory, regardless of whether the archive was packed like that or not.

  • Not even there in the Ubuntu repos. Not a great way for installation! – Lakshman Prasad Sep 22 '09 at 7:58
  • It was only added to Debian earlier this year, so it's only in karmic, but you should be able to install the .deb on jaunty with no problems. – TRS-80 Sep 22 '09 at 8:54
1

Does no one else use atool? It's a command-line tool for format-agnostic archiving and extraction.

To unpack any supported archive: aunpack archive.zip To pack files into any supported archive: apack archive.tar.bz2 *.txt To list files in any supported archive: als archive.tgz

I can't remember the last time I've directly used any archive-specific tool.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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