LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-20-2013, 03:14 AM   #1
dchmelik
Member
 
Registered: Nov 2008
Location: Washington state, USA
Distribution: BSD, GNU/Linux (Slackware, etc.,) openSolaris
Posts: 197

Rep: Reputation: 21
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
}

Last edited by dchmelik; 01-20-2013 at 03:19 AM.
 
Old 01-20-2013, 03:52 AM   #2
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 609

Rep: Reputation: 180Reputation: 180
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.
 
Old 01-20-2013, 03:59 AM   #3
dchmelik
Member
 
Registered: Nov 2008
Location: Washington state, USA
Distribution: BSD, GNU/Linux (Slackware, etc.,) openSolaris
Posts: 197

Original Poster
Rep: Reputation: 21
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.

Last edited by dchmelik; 01-31-2013 at 12:17 AM. Reason: another comment
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about moving a range of files with a Bash script surfrock66 Linux - Newbie 6 11-22-2009 03:37 PM
Question about bash history temp files WingnutOne Linux - General 4 05-16-2008 05:53 AM
Unable to unarchive Mplayer tarball deepumnit Linux - Software 6 01-10-2008 10:19 AM
bash read files to variable question babag Programming 7 06-25-2007 05:04 AM
how to unarchive a tar.gz? crealkillerI75 Linux - Newbie 5 08-15-2002 11:38 AM


All times are GMT -5. The time now is 06:21 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration