I've figured out that extracting a .zip you need the path to the directory as well when you are recursively looking through subdirectories. So how do you store the path? This is nearly there but doesn't work properly when there is a space in the {zip_file}.

zip_dir=$PWD/$(basename "${zip_file}")
  • I see you've aksed a lot of questions recently - what's the overall aim you are after? :) We may be able to show you an easier method.
    – Tim
    Commented May 24, 2015 at 14:35
  • As I was shown before, I am sorry for the backslashes. I use those in echo lines, so I'm sorry about that. This line as is works fine. I agree with Tim, what is the goal here? There might be a problem with what you are trying to do next with the zip_dir variable.
    – Terrance
    Commented May 24, 2015 at 14:49
  • For my understanding, I am breaking the problem into bits. This script (dbforums.com/…) is close to what I want but I don't just want to copy a "magic" script not knowing how it works. The problem with this script is I don't want a folder called... myfile.zip and I want it to recursively search subfolders. Commented May 24, 2015 at 14:52
  • @JohnnyBizzle okay, unix.stackexchange.com/questions/4367/… may help, as may stackoverflow.com/questions/107995/…. Also, when you reply try to remember to include the @Name so they are notified!
    – Tim
    Commented May 24, 2015 at 15:00
  • 1
    @JohnnyBizzle so after your zip_dir=$PWD/$(basename "${zip_file}") add another line defining it again as zip_dir=${zip_dir%.*} to remove the .zip from the name. It is OK to have multiple lines defining things with the same variable as long as the last one is the one you want.
    – Terrance
    Commented May 24, 2015 at 15:09

2 Answers 2


I created this from your other question as well. It took me a bit, but this is what I was able to come up with to create the folders based on the zip file name, removing the .zip from the folder name, then extracting the zip file into that folder.


echo "Start folder create..."
find . -type f -iname "*.zip" | while read filename
 mkdir -p "$foldername"
 unzip "$filename" -d "$foldername"
 echo "Created directory $foldername and extracted files to it." 

The line filename1=${filename:2} strips off the ./ of the name.

  • That nearly worked perfectly but one folder had a space so it ignored that when doing the unzip Commented May 24, 2015 at 15:52
  • My mistake -I forgot the quotes! Commented May 24, 2015 at 15:53
  • @JohnnyBizzle I also made some simple corrections for deeper folders if existing, but should still work.
    – Terrance
    Commented May 24, 2015 at 15:54

Edit: I must not have tested this correctly.

Generally you don't need quotes when doing a variable assignment, just when dereferencing a variable. When making an assignment you don't need to put quotes around a variable being dereferenced, but since the dereferencing happens in a sub shell, quotes are needed around the ${zip_file} part so that it's value is properly passed to basename. Quotes are NOT in fact needed around the entire line, because then it's back in the context of variable assignment, where quotes aren't needed as bash will do the right thing with passing the value from the subshell to the assignment line.

Thanks @janos for pointing this out. I'm not sure what I screwed up in testing the original line that took me down the path of excessive quotes. You are correct, the following is fine:

zip_dir=$PWD/$(basename "${zip_file}")

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .