How to skip unzipping a file that already exists?

I have a for loop which unzips all .zip into that directory. I want to check if the unzipped file already exists and do the unzipping for only the zipped files.

Here is the code without any if statment:

for i in `cat zipfiles.txt`; do output_dir=$(dirname $i) unzip -d $output_dir $i
done 

zipfiles.txt is like:

./CSAN/S1A.zip
./MEZO/S1B.zip

output is

./CSAN/S1A.SAFE
./MEZO/S1B.SAFE

I tried to add an if loop, but it's still asking if I want to replace the already existing file.

for i in `cat zipfiles.txt`; do file="${dir%/}.SAFE" if [ -e "$file" ] then echo "It exists" else output_dir=$(dirname $i) unzip -d $output_dir $i fi
done

What is the good solution?

1

2 Answers

Use the -n switch. From the unzip man page:

-n never overwrite existing files. If a file already exists, skip the extraction of that file without prompting. By default unzip queries before extracting any file that already exists; the user may choose to overwrite only the current file, overwrite all files, skip extraction of the current file, skip extraction of all existing files, or rename the current file.

0

Okay, I found the right solution:

for i in `cat zipfiles.txt`; do output_dir=$(dirname $i) echo $output_dir file="${i/zip/SAFE}" echo $file if [ -e "$file" ] then echo "It exists" else unzip -d $output_dir $i fi
done
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like