Ubzip many files

Quick to answer questions about finding your way around Linux Mint as a new user.
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions use the other forums in the support section.
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
Spacecadet
Level 2
Level 2
Posts: 81
Joined: Thu Jul 13, 2017 10:03 am

Ubzip many files

Post by Spacecadet »

Is there a way to unzip many files or does one need to go one by one? I've downloaded a ton of stuff and it is all zipped. It's time consuming.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
vanadium
Level 4
Level 4
Posts: 324
Joined: Sun Dec 27, 2015 1:07 pm

Re: Ubzip many files

Post by vanadium »

Yes there is. Select several files then select "Extract here". Else, unzip from the command line in a for loop.
Spacecadet
Level 2
Level 2
Posts: 81
Joined: Thu Jul 13, 2017 10:03 am

Re: Ubzip many files

Post by Spacecadet »

Is there a way to extract them each into their own folder so the files do not get mixed up together?
djph
Level 7
Level 7
Posts: 1928
Joined: Thu Jun 27, 2019 5:43 am
Location: ::1

Re: Ubzip many files

Post by djph »

Depends on how the creator of the archive did things.
  • If the archives are a zipped directory, unzip *zip will unzip everything into their own directories
  • If the archives are just jumbled files, you'll have to do more work. Something like the below.

Code: Select all

#!/bin/bash

#unzip.sh -- quick and dirty script to unzip archives into their own directories.  
#Spaces in filenames / starting directory may cause fun(tm). Best to avoid them.

# Usage: die message ...
die() {
  printf '%s\n' "$*" >&2
  exit 1
}

read -p "Please enter starting directory: " stdir

if [[ ! -d $stdir ]] ; then
  printf "${stdir} is not a valid directory."
  exit 1
fi

cd $stdir || die

for f in *zip; do
  g="${f%.zip}"
  mkdir ./"$g"
  cd ./"$g" || die
  unzip ../"$f"
  cd .. || die
done

printf "processing complete."
Spacecadet
Level 2
Level 2
Posts: 81
Joined: Thu Jul 13, 2017 10:03 am

Re: Ubzip many files

Post by Spacecadet »

I'm very inexperienced with the terminal. I know how to change directory. Do I just copy and paste this?
User avatar
absque fenestris
Level 12
Level 12
Posts: 4110
Joined: Sat Nov 12, 2016 8:42 pm
Location: Confoederatio Helvetica

Re: Ubzip many files

Post by absque fenestris »

Spacecadet wrote: Fri Sep 24, 2021 2:40 am I'm very inexperienced with the terminal. I know how to change directory. Do I just copy and paste this?
Hi Spacecadet
do you have the possibility to use a Mint or otherwise a Linux in the VirtualBox?
A virtual Linux experimental system can really do a lot to reduce inhibitions and fears of the terminal. If something goes completely wrong, just delete the virtual system. Your data and your settings on your main installation are not at risk at any time.
Spacecadet
Level 2
Level 2
Posts: 81
Joined: Thu Jul 13, 2017 10:03 am

Re: Ubzip many files

Post by Spacecadet »

Yes, I put Virtual Box on this computer but I don't know how to use it. If I can get it set up you recommend playing around with the terminal in it? Before I let myself loose on the real one?
User avatar
absque fenestris
Level 12
Level 12
Posts: 4110
Joined: Sat Nov 12, 2016 8:42 pm
Location: Confoederatio Helvetica

Re: Ubzip many files

Post by absque fenestris »

In principle, virtual systems are ideal playgrounds for such exercises. You can really try out all terminal commands, but also scripts like the one above at your leisure. Even several times, until it works and you feel confident.
The prerequisite, however, is 8 GB RAM. Whether it works e.g. with 4 GB RAM is questionable, 10 years ago this was no problem, meanwhile the memory requirement has increased strongly.
djph
Level 7
Level 7
Posts: 1928
Joined: Thu Jun 27, 2019 5:43 am
Location: ::1

Re: Ubzip many files

Post by djph »

Spacecadet wrote: Fri Sep 24, 2021 2:40 am I'm very inexperienced with the terminal. I know how to change directory. Do I just copy and paste this?
yeah, that'd get copy/pasted into a file (e.g. "unzip.sh") and then you would run it with bash unzip.sh.

Here's a few random zipped files I found on my system, and threw into ~/ziptest.

Code: Select all

$ pwd
/home/djph/ziptest
$ ls -l 
total 64
-rw-r--r-- 1 djph djph 48158 Sep 24 08:01 Final Code.zip
-rw-r--r-- 1 djph djph   296 Sep 24 08:01 openvolt.zip
-rw-r--r-- 1 djph djph  5584 Sep 24 08:01 rc.zip
-rw-r--r-- 1 djph djph   832 Sep 24 08:01 test.zip
And running the script (it's in the directory /home/djph/Programming/src, just to show it'll work anywhere) NOTE - you have to use the full/correct path. It doesn't deal with bash expansions like $HOME or ~

Code: Select all

$ pwd 
/home/djph/Programming/src
$ bash unzip.sh
Please enter starting directory: /home/djph/ziptest/
Archive:  ../Final Code.zip
  inflating: CODE FINAL.txt
Archive:  ../openvolt.zip
  inflating: openvolt
Archive:  ../rc.zip
  inflating: rc.lua
Archive:  ../test.zip
  inflating: testfile.txt
  inflating: test.txt
  inflating: test.sh
processing complete.
And the result in ~/ziptest. As you can see, a new subdirectory is created for each zip archive, and the contents are extracted into the given subdirectory (as opposed to all of them unzipping and jumbling up the files in the parent 'ziptest' directory).

Code: Select all

$ pwd
/home/djph/ziptest
$ ls -l 
total 80
drwxr-xr-x 2 djph djph  4096 Sep 24 08:06 Final Code
-rw-r--r-- 1 djph djph 48158 Sep 24 08:01 Final Code.zip
drwxr-xr-x 2 djph djph  4096 Sep 24 08:06 openvolt
-rw-r--r-- 1 djph djph   296 Sep 24 08:01 openvolt.zip
drwxr-xr-x 2 djph djph  4096 Sep 24 08:06 rc
-rw-r--r-- 1 djph djph  5584 Sep 24 08:01 rc.zip
drwxr-xr-x 2 djph djph  4096 Sep 24 08:06 test
-rw-r--r-- 1 djph djph   832 Sep 24 08:01 test.zip
$ cd Final\ Code
$ ls -l 
total 196
-rw-r--r-- 1 djph djph 199556 Jun  4 19:57 CODE FINAL.txt
Locked

Return to “Beginner Questions”