Question

I am trying to design a loop that implements a lot of single elements I have seen before and the combination is throwing me off. Basically I have a structure like this:

/bin/script.sh
/conf/patches/1.zip
/conf/patches/2.zip
/conf/patches/3.zip
/sharedir

I want a loop that will go through however many patches I have in /conf/patches, unzip each patch into a separate directory in /sharedir. Each directory should be named the name of the file.

What I was trying so far was:

for file in '../conf/patches/*.zip'
do
    unzip "${file%%.zip}" -d /sharedir/$file
done

As you can see...there is definitely something I am missing in this combination.

Was it helpful?

Solution

Try this:

for file in /conf/patches/*.zip
do
  f="${file##*/}"
  mkdir -p "/sharedir/${f%.zip}"
  unzip -d "/sharedir/${f%.zip}" "${file}"
done

OTHER TIPS

Remove quotes from glob pattern otherwise it is not expanded:

for file in ../conf/patches/*.zip
do
    unzip "${file%%.zip}" -d /sharedir/
done

EDIT: You can try

for f in ../conf/patches/*.zip; do
   echo g="${f%%/*}"
   unzip -d "sharedir/${g%*.zip}" "$f"
done
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top