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.zipoutput is
./CSAN/S1A.SAFE
./MEZO/S1B.SAFEI 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
doneWhat is the good solution?
12 Answers
Use the -n switch. From the unzip man page:
0-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.
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