Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What I'm looking for is essentially the pure commandline counterpart to ark -ba <path> (on KDE), or file-roller -h <path> (on GNOME/Unity).

Unfortunately, both ark and file-roller require X to be running. I'm aware that it is relatively simple to write a tool that detects archives based on their file extension, and then runs the appropiate program:

if [[ -f "$1" ]] ; then
    case $1 in
        *.tar.bz2) tar xjvf $1 ;;
        *.tar.gz) tar xzvf $1 ;;
        *.bz2) bunzip2 $1 ;;
        *.rar) rar x $1 ;;
        *.gz) gunzip $1 ;;
        *.tar) tar xf $1 ;;
        *.tbz2) tar xjvf $1 ;;
        *.tgz) tar xzvf $1 ;;
        *.zip) unzip $1 ;;
        *.Z) uncompress $1 ;;
        *.7z) 7z x $1 ;;
        *) echo "'$1' cannot be extracted with this utility" ;;
    echo "path '$1' does not exist or is not a file"

However, that doesn't take care of subdirectory detection (and in fact, many extraction programs do not even supply such an option).

So might there be a program that does exactly that?

share|improve this question
The question is perfectly on topic and welcome to stay here. If you don't get a good answer, you can always flag it for migration to Unix & Linux. – terdon Dec 9 '12 at 4:51
What do you mean by "subdirectory detection"? – terdon Dec 9 '12 at 4:53
I am also unclear what you mean with subdirectory detection. Do you want to avoid extracting any subdirectories and dump all files in the current directory? Do you want to Avoid that and make sure it end up in a directory (without more subdirs in that dir), etc etc. --- Also, in the example: Why verbose on compressed tarballs but not on regular ones? And why #!/bin/bash rather than the more modern #!/usr/bin/env bash ? – Hennes Dec 9 '12 at 5:18
Both ark and file-roller are able to automatically detect whether or whether not the archive stores the file in a subdirectory - i.e, (as virtual path) "", compared to (again, virtual path) "". In the latter case, both ark and file-roller would create a directory named "somearchive" (or "somearchive-<number>" if the directory already exists in $PWD). That's what I mean with "subdirectory detection". – 该用户不存在 Dec 9 '12 at 5:33
You could look into atool. The aunpack command should be able to do what you need. Also see here. – xuhdev Feb 20 '14 at 7:59
up vote 3 down vote accepted

What you describe as "subdirectory detection" should happen by default. In this example with GNU tar:

$ tree
├── dir1
│   └── file4
├── dir2
│   ├── file5
│   └── file6
├── file1
├── file2
└── file3


$ tar cvf all.tar *
$ mkdir new_dir
$ mv all.tar new_dir
$ cd new_dir
$ tar xvf all.tar
$ tree
├── all.tar
├── dir1
│   └── file4
├── dir2
│   ├── file5
│   └── file6
├── file1
├── file2
└── file3

If you are using an archive program that does not keep the directory structure when creating an archive (are you sure about this by the way? I don't know of any that don't do this), then the information is lost. There is no way to recreate the directory structure unless it has been saved in the archive itself, in which case it should be recreated upon archive extraction by default.

When does the script you have in your question fail?

share|improve this answer
No worries, I'll delete my comment. It was uncalled for, I agree. Nevertheless, I appreciate your effort. My apologies. – 该用户不存在 Dec 9 '12 at 22:50
Fair enough, deleting mine too, sorry I couldn't be of more help. – terdon Dec 9 '12 at 23:42

Your Answer


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.