LinuxQuestions.org

LinuxQuestions.org (http://www.linuxquestions.org/questions/index.php)
-   Linux - General (http://www.linuxquestions.org/questions/forumdisplay.php?f=1)
-   -   question about bash functions to unarchive files (http://www.linuxquestions.org/questions/showthread.php?t=4175446437)

dchmelik 01-20-2013 03:14 AM

question about bash functions to unarchive files
 
I found a bash function to unarchive files, and my modified version is below. However, when I use it on a tar.gz, for example, it only opens it into a tar, not the tar contents. I triple-checked the command, which I had been using for over a decade anyway. Actually, it does not even need the 'z' in there; it would work anyway with modern tar. What is going wrong, and how would I also make it output to the screen?

Code:

function unarc()
{
  if [ -f "$1" ] ; then
    case "$1" in
      *.bz2) bunzip2 "$1" ;;
      *.cab) cabextract "$1" ;;
      *.gz) gunzip "$1" ;;
      *.jar) unzip "$1" ;;
      *.lha) lha e "$1" ;;
      *.tar.bz2) tar xvjf "$1" ;;
      *.tar.gz) tar xvzf "$1" ;;
      *.tar.Z) tar xvzf "$1" ;;
      *.rar) unrar x "$1" ;;
      *.7z) 7z x "$1" ;;
      *.tar) tar xvf "$1" ;;
      *.tbz2) tar xvjf "$1" ;;
      *.tgz) tar xvzf "$1" ;;
      *.txz) tar xzJf "$1" ;;
      *.xz) tar xzJf "$1" ;;
      *.Z) uncompress "$1" ;;
      *.zip) unzip "$1" ;;
      *) echo "'$1' cannot be extracted." ;;
    esac
  else echo "'$1' is not a file."
  fi
}

I attempted to write a mass unarchiver, but it loops on the first file (which of course I tested on something other than a *.tar.*.) How would I fix this?

Code:

function massunarc()
{
  for f in *.[bB][zZ]2 *.[cC][aA][bB] *[gG][zZ] *[jJ][aA][rR] *.[lL][hH][aA] *.[tT][aA][rR][.][bB][zZ]2 *.[tT][gG][zZ] *.[xX][zZ] *.[zZ] *.[zZ][iI][pP]; do
    if [ -f "$1" ] ; then
      case "$1" in
        *.bz2) bunzip2 "$1" ;;
        *.cab) cabextract "$1" ;;
        *.gz) gunzip "$1" ;;
        *.jar) unzip "$1" ;;
        *.lha) lha e "$1" ;;
        *.tar.bz2) tar xjvf "$1" ;;
        *.tar.gz) tar xzvf "$1" ;;
        *.tar.Z) tar xzvf "$1" ;;
        *.rar) unrar x "$1" ;;
        *.7z) 7z x "$1" ;;
        *.tar) tar xvf "$1" ;;
        *.tbz2) tar xjf "$1" ;;
        *.tgz) tar xzvf "$1" ;;
        *.txz) tar xJvf "$1" ;;
        *.xz) tar xz "$1" ;;
        *.Z) uncompress "$1" ;;
        *.zip) unzip "$1" ;;
        *) echo "'$1' cannot be extracted." ;;
      esac
    else echo "'$1' is not a file."
    fi
  done
}


414N 01-20-2013 03:52 AM

The pattern "*.gz" if before the "*.tar.gz" one, so the shell executes "gunzip" on the tar.gz file, leaving you with a tar file instead of the archive contents.
With modern tar you don't need to add special flags for decompressing bz2/gz/xz archives anymore, so you could remove those lines from your script.
To solve the problem, however, I guess you can just move the "most general patterns" (*.gz, *.bz2) to the end of the switch, so they don't match with compressed tarballs.

dchmelik 01-20-2013 03:59 AM

Oh, yeah; I should have thought of that! I put the extensions in that order so it would be easier to see what was there. I will replace those with '*.tar.*.' I think I see what the problem may be with the mass unarchiver, but I have not thought of a good way to fix it. I actually did get it working, but mainly I want to use this on old sound sample pack archive files that have no folder in them, so I need to modify it to make folders for each file and copy them in, but this seems challenging.

It is unfortunate than in KDE file managers, when you go to extract a file with whatever they may have built in, or something like a 7z GUI, it can only do one at a time. That is why I am trying to write a mass unarchiver.


All times are GMT -5. The time now is 06:16 AM.