Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Unzip Multiple Zipfile's

  1. #1
    Join Date
    Aug 2014
    Beans
    529
    Distro
    Lubuntu 22.04 Jammy Jellyfish

    Unzip Multiple Zipfile's

    I have multiple zipfile's (*.zip) in 3 different subdirectories.

    Using the terminal, how would I unzip these into a different directory?
    Not into the current directory.
    Last edited by wyattwhiteeagle; April 24th, 2024 at 03:09 PM.

  2. #2
    Join Date
    Dec 2014
    Beans
    2,634

    Re: Unzip Multiple Zipfile's

    Code:
    cd the/target/directory
    unzip path/to/the/zipfile
    or
    Code:
    unzip path/to/the/zipfile -d the/target/directory
    'unzip' will unpack the given file into the current working directory or the directory given after the '-d' option, including any directory hierarchy inside the archive. If you want all the files without the hierarchy you can give unzip the option '-j' to unpack the files without reproducing the directory hierarchy. Since 'unzip' will only process one archive per call, you'll have to call it multiple times, once per archive. A loop might be useful ('for i in path1/zipfile1 path2/zipfile2 path3/zipfile3 ; do unzip "$i"; done').

    Holger

  3. #3
    Join Date
    Aug 2014
    Beans
    529
    Distro
    Lubuntu 22.04 Jammy Jellyfish

    Re: Unzip Multiple Zipfile's

    Quote Originally Posted by Holger_Gehrke View Post
    Code:
    cd the/target/directory
    unzip path/to/the/zipfile
    or
    Code:
    unzip path/to/the/zipfile -d the/target/directory
    'unzip' will unpack the given file into the current working directory or the directory given after the '-d' option, including any directory hierarchy inside the archive. If you want all the files without the hierarchy you can give unzip the option '-j' to unpack the files without reproducing the directory hierarchy. Since 'unzip' will only process one archive per call, you'll have to call it multiple times, once per archive. A loop might be useful ('for i in path1/zipfile1 path2/zipfile2 path3/zipfile3 ; do unzip "$i"; done').

    Holger
    It appear's a loop is useful, indeed.
    Just so I understand...am I missing anything?
    Code:
    '/home/wyatt/Downloads/File-1.zip /home/wyatt/Desktop/Zipped/File-2.zip ; do unzip "$i"; done'

  4. #4
    Join Date
    Aug 2014
    Beans
    529
    Distro
    Lubuntu 22.04 Jammy Jellyfish

    Re: Unzip Multiple Zipfile's

    Update...

    Quote Originally Posted by wyattwhiteeagle View Post
    It appear's a loop is useful, indeed.
    Just so I understand...am I missing anything?
    Code:
    '/home/wyatt/Downloads/File-1.zip /home/wyatt/Desktop/Zipped/File-2.zip ; do unzip "$i"; done'
    Looks like I'm missing a few things here such as '$i' designation, etc

    I'll post later with the terminal output.
    Last edited by wyattwhiteeagle; April 24th, 2024 at 04:23 PM.

  5. #5
    Join Date
    Dec 2014
    Beans
    2,634

    Re: Unzip Multiple Zipfile's

    Yes, you're missing the 'for i in ' part, which tells the shell that this is a loop and the variable i should be set to one of the listed values at the beginning of each iteration. You also might want to 'cd' into the target directory before running the loop or add a '-d' option with the target directory to the unzip command. And on the command line you don't need the single quotes I enclosed it in. Obviously, with a loop all archives will be unpacked into the same directory unless you do something really fancy - let's say two arrays, one holding the filenames, one holding the paths to extract to, looping over the indices to extract each file to the directory with the same index in the second array. With anything under ten files I probably wouldn't bother with a loop at all and just do the unzipping one at a time.

    Code:
    cd the/target/directory
    for i in /home/wyatt/Downloads/File-1.zip /home/wyatt/Desktop/Zipped/File-2.zip ; do 
      unzip "$i"; 
    done
    The usual warning: filenames with spaces in them can lead to really surprising results when doing shell-scripting ...

  6. #6
    Join Date
    Aug 2014
    Beans
    529
    Distro
    Lubuntu 22.04 Jammy Jellyfish

    Re: Unzip Multiple Zipfile's

    Oops...missed your reply...let me read and try.
    Please don't reply until after my next post.

    What I tried didn't work.
    What all am I missing in the script?
    The script has...
    Code:
    SRC="/home/wyatt/Desktop/Test-Folder-2/Image/Wyatts-Scripts.zip /home/wyatt/Desktop/Everything/Compressed/ZIP/Cheys-Place-GA.zip"
    TGT="/home/wyatt/Downloads"
    unzip "$SRC" -d "$TGT"
    Here's the output...
    Code:
    wyatt@wyatt-hplaptop15ef1xxx:~$    /home/wyatt/Desktop/Test-Script.sh
    unzip:  cannot find or open /home/wyatt/Desktop/Test-Folder-2/Image/Wyatts-Scripts.zip /home/wyatt/Desktop/Everything/Compressed/ZIP/Cheys-Place-GA.zip, /home/wyatt/Desktop/Test-Folder-2/Image/Wyatts-Scripts.zip /home/wyatt/Desktop/Everything/Compressed/ZIP/Cheys-Place-GA.zip.zip or /home/wyatt/Desktop/Test-Folder-2/Image/Wyatts-Scripts.zip /home/wyatt/Desktop/Everything/Compressed/ZIP/Cheys-Place-GA.zip.ZIP.
    wyatt@wyatt-hplaptop15ef1xxx:~$
    Last edited by wyattwhiteeagle; April 24th, 2024 at 05:03 PM.

  7. #7
    Join Date
    Aug 2014
    Beans
    529
    Distro
    Lubuntu 22.04 Jammy Jellyfish

    Re: Unzip Multiple Zipfile's

    Thank you so very much.
    The below worked.
    Marking this as solved.
    Code:
    cd /home/wyatt/Downloads
    for i in /home/wyatt/Desktop/Test-Folder-2/Image/Wyatts-Scripts.zip /home/wyatt/Desktop/Everything/Compressed/ZIP/Cheys-Place-GA.zip ; do unzip "$i"; 
    done
    Last edited by wyattwhiteeagle; April 24th, 2024 at 05:38 PM.

  8. #8
    Join Date
    Dec 2014
    Beans
    2,634

    Re: Unzip Multiple Zipfile's

    As I said, 'unzip' will unpack one file per call. By enclosing the list of files in quotes in post #6, you've turned it into one token. So now unzip looks for one file named '/home/wyatt/Desktop/Test-Folder-2/Image/Wyatts-Scripts.zip /home/wyatt/Desktop/Everything/Compressed/ZIP/Cheys-Place-GA.zip' and can't find it. But even if it would correctly split it into two tokens it wouldn't work; second and following parameters are interpreted as specific files to unpack from the archive given as the first parameter. That way you can selectively unpack only the files you need from an archive. To unpack multiple archives you really do need a loop as you've done in post #7.

    If you need to unpack multiple files in different places, you'd do something like
    Code:
    declare -a filenames=(the/first/file the/second/file the/'third and final'/file)
    declare -a targetdirs=(one/directory/ another/directory/ and/a/final/directory)
    for (( i=0 ; i<${#filenames[*]} ; i++)) ; do 
      unzip ${filenames[i]} -d ${targetdirs[i]}
    done
    This might actually be useful if you have a dozen or so archives to unpack into as many different directories ...

    Holger

  9. #9
    Join Date
    Aug 2014
    Beans
    529
    Distro
    Lubuntu 22.04 Jammy Jellyfish

    Re: Unzip Multiple Zipfile's

    Quote Originally Posted by Holger_Gehrke View Post
    As I said, 'unzip' will unpack one file per call. By enclosing the list of files in quotes in post #6, you've turned it into one token. So now unzip looks for one file named '/home/wyatt/Desktop/Test-Folder-2/Image/Wyatts-Scripts.zip /home/wyatt/Desktop/Everything/Compressed/ZIP/Cheys-Place-GA.zip' and can't find it. But even if it would correctly split it into two tokens it wouldn't work; second and following parameters are interpreted as specific files to unpack from the archive given as the first parameter. That way you can selectively unpack only the files you need from an archive. To unpack multiple archives you really do need a loop as you've done in post #7.

    If you need to unpack multiple files in different places, you'd do something like
    Code:
    declare -a filenames=(the/first/file the/second/file the/'third and final'/file)
    declare -a targetdirs=(one/directory/ another/directory/ and/a/final/directory)
    for (( i=0 ; i<${#filenames[*]} ; i++)) ; do 
      unzip ${filenames[i]} -d ${targetdirs[i]}
    done
    This might actually be useful if you have a dozen or so archives to unpack into as many different directories ...

    Holger
    It appear's as though you were typing your reply while I was editing my post.
    Anyhow, that too might be useful.
    Again, thanks bunches

  10. #10
    Join Date
    Mar 2011
    Location
    U.K.
    Beans
    Hidden!
    Distro
    Ubuntu 22.04 Jammy Jellyfish

    Re: Unzip Multiple Zipfile's

    Not being an expert in bash scripts I take the easy route of Krusader double panel manager (similar to Double Commander). Although these tools do have a large footprint of dependencies.

    Copy the zip(s) from ~/Downloads to target panel and just click to expand in its target.

    There are additional features in Krusader Useractions.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •