LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-06-2012, 02:59 AM   #16
IlIl7
LQ Newbie
 
Registered: Oct 2010
Location: Europe mainly.. :D
Distribution: BackTrack 5
Posts: 17

Original Poster
Rep: Reputation: 0

It seems there is still a problem... which initially i didn't notice...

1. Before running any script:
Quote:
~/home/somestuff$ls
A B C D
E F G H
and so on
~/home/somestuff/A$ls
aa1.zip aa2.zip
~/home/somestuff/B$ls
bbb.zip
~/home/somestuff/G$ls
gg1.zip gg2.zip gg3.zip gg4.zip
and so on
2. What I get after running the script of my first post from somestuff:

Quote:
~/home/somestuff$ls
A B C D
E F G H
...
aa1 aa2 bbb gg1
gg2 gg3 gg4
and so on
~/home/somestuff/A$ls
aa1.zip aa2.zip
~/home/somestuff/B$ls
bbb.zip
~/home/somestuff/G$ls
gg1.zip gg2.zip gg3.zip gg4.zip
and so on
3. What I get after running the 2nd script provided by sag47 from somestuff:

Quote:
~/home/somestuff$ls
A B C D
E F G H
and so on
~/home/somestuff/A$ls
aa1.zip aa2.zip aa1/ (a folder with aa1 file inside) aa2/ (a folder with aa2 file inside)
~/home/somestuff/B$ls
bbb.zip bbb/ (a folder with bbb file inside)
~/home/somestuff/G$ls
gg1.zip gg2.zip gg3.zip gg4.zip
gg1/ (a folder with gg1 file inside) gg2/ (a folder with gg2 file inside) gg3/ (a folder with gg3 file inside) gg4/ (a folder with gg4 file inside)
and so on
4. BUT WHAT I NEED IS TO RUN A SCRIPT FROM somestuff and get:

Quote:
~/home/somestuff$ls
A B C D
E F G H
and so on
~/home/somestuff/A$ls
aa1.zip aa2.zip aa1 aa2
~/home/somestuff/B$ls
bbb.zip bbb
~/home/somestuff/G$ls
gg1.zip gg2.zip gg3.zip gg4.zip
gg1 gg2 gg3 gg4
and so on
THE REASON I WANT THIS, IS BECAUSE MANY OF THE RESULTING FILES ARE SPLITTED RAR FILES WHICH MUST BE IN THE SAME FOLDER IN ORDER TO BE FURTHER EXTRACTED....
e.g. gg1.r0 gg1.r1. gg1.r3 gg1.rar


so after all i guess makedir should be removed from the script of sag47 but dont know how to do it exactly...
thanks again for your time all
 
Old 07-06-2012, 03:12 AM   #17
IlIl7
LQ Newbie
 
Registered: Oct 2010
Location: Europe mainly.. :D
Distribution: BackTrack 5
Posts: 17

Original Poster
Rep: Reputation: 0
one more time graphically what i need:

A. BEFORE running script:

Quote:
somestuff
|_A
|_aa1.zip
|_aa2.zip
|_B
|_bbb.zip
...
|_G
|_gg1.zip
|_gg2.zip
|_gg3.zip
|_gg4.zip
...
|_lastsubfolder
|_lf.zip
B. AFTER running script from somestuff - WHAT I NEED:

Quote:
somestuff
|_A
|_aa1.zip
|_aa2.zip
|_aa1 (file not folder)
|_aa2 (file not folder)
|_B
|_bbb.zip
|_bbb (file not folder)
...
|_G
|_gg1.zip
|_gg2.zip
|_gg3.zip
|_gg4.zip
|_gg1 (file not folder)
|_gg2 (file not folder)
|_gg3 (file not folder)
|_gg4 (file not folder)
...
|_lastsubfolder
|_lf.zip
|_lf (file not folder)

Last edited by IlIl7; 07-06-2012 at 03:16 AM. Reason: corrected sth
 
Old 07-06-2012, 03:36 AM   #18
IlIl7
LQ Newbie
 
Registered: Oct 2010
Location: Europe mainly.. :D
Distribution: BackTrack 5
Posts: 17

Original Poster
Rep: Reputation: 0
@zhjim
U MADE MY DAY!!!!!!!!


SOLVED USING AFTER FOLLOWING YOUR ADVICE:
Quote:
find . -name '*zip' -execdir unzip -o- {} \;
but i feel so ..idiot...
was so simple....only ...exedir.. instead of exec...

THANKS EVERYBODY FOR YOUR HELP!!!!
 
Old 07-06-2012, 04:28 AM   #19
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Great to see you got it working!
 
Old 07-06-2012, 09:27 AM   #20
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Quote:
Originally Posted by sag47 View Post
Code:
#!/bin/bash
rootdir="$PWD"
find "$rootdir" -type f -name '*.zip' | while read file;do
  cd "$(dirname "$file")"
  if [ "$(dirname "$file")" = "$PWD" ];then
    mkdir "$(basename "$file" | cut -d. -f1)"
    cd "$(basename "$file" | cut -d. -f1)"
    unzip "$file"
  else
    echo "error"
    break
  fi
done
cd "$rootdir"
Here's the new script with comments.
Code:
#!/bin/bash
#assumes you're running the script as ./script.sh and the script is located
#where you need to process the files.
rootdir="$PWD"

#see man find; for more information
find "$rootdir" -type f -name '*.zip' | while read file;do

  #change the current working directory to the directory of the zip file
  cd "$(dirname "$file")"

  #before processing, ensure the current working directory is in fact the same directory where the zip file is located
  #this is relative to the zip file and has nothing to do with $rootdir
  #the point is you want to be sure the directory change worked before you go unzipping a file
  #otherwise it's possible to accidentally unzip all zip files within the same folder if it can't ever change the directory
  #alternatively you could also use the following commented out if statement instead which checks the exit status of the last command (which would be the cd command)
  #if [ "$?" = "0" ];then
  if [ "$(dirname "$file")" = "$PWD" ];then

    #added the -p switch to mkdir so that if the directory exists there is no error
    #if the directory does not exist then create it; else update the directory timestamp
    mkdir -p "$(basename "$file" | cut -d. -f1)"

    #make $PWD the directory of the same name of the zip file
    cd "$(basename "$file" | cut -d. -f1)"

    #$file is a full path name and not a relative path.
    #unzipped $file will output to the current working directory
    unzip "$file"

  else

    #since there was an error "break" out of the while loop and stop processing zip files.
    echo "error"
    break
  fi
done

#I added this at the end in case you want to do post processing after the while loop
#if this isn't here then the script would be working in the last used working directory (which is the last zip file unzipped)
#this may or may not be what you expect so instead of guessing I added this in to force changing the directory back to $rootdir
cd "$rootdir"
Basically I took advantage of unzip outputting to the current working directory. That's why this script works. There are other more short hand ways of doing this but I decided to approach it in a way that was conceptually easy to understand.

If you want to learn more about a script use the man pages. man every command you see and that is how you will learn. I suggest starting with man bash.

Alternatively you could also avoid errors with making the directory like so...
Code:
    if [ ! -d "$(basename "$file" | cut -d. -f1)" ];then
      mkdir "$(basename "$file" | cut -d. -f1)"
    fi
    cd "$(basename "$file" | cut -d. -f1)"
    unzip "$file"
SAM

Last edited by sag47; 07-06-2012 at 09:56 AM.
 
  


Reply

Tags
command line



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unzip multiple zip files, each to separate folder smart_sagittari Linux - Newbie 3 03-21-2012 08:41 AM
What command to delete all files and subfolders, but not root folder ? proleader Linux - Newbie 10 09-16-2011 05:02 AM
[SOLVED] How to copy a file to all subfolders in a directory using a single command? SirTristan Linux - Newbie 7 07-18-2010 11:19 PM
[SOLVED] extracting multiple files into subfolders ebolamayinga Linux - Software 2 10-02-2009 04:47 PM
How to Unrar/unzip multiple archives at once containing a single file? SentralOrigin Linux - Software 11 11-30-2006 05:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:38 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration