Ubuntu – How to unzip into a given directory

bash

I have a list with the path of .zip files, gathered by the command below. I want to unzip them into that directory, where they exist.

find . -name "*.zip" -print > outfile.txt

outfile.txt is like:

./TWO/two.zip
./ONE/one.zip

I have run_all script to automatize it, but how can I define the output directory?

run_all outfile.txt 'unzip -u $1'

Best Answer

Use the -d flag.

unzip -d output_dir/ zipfiles.zip

To automate it:

#!/bin/bash
for i in `cat outfile.txt`; do
    output_dir=$(dirname $i)
    unzip -d $output_dir $i
done

EDIT: As @dessert suggests, you may do this as a better alternative:

while IFS='' read -r i || [[ -n "$i" ]]; do unzip -d ${i%/*} $i; done <outfile.txt