JamesF
JamesF

Reputation: 449

How to unzip files that have been zipped twice?

I've been given a set of data files (csv) that have been put into folders, then zipped, then combined into another zip archive. How do I get all the files out into the same directory, while ensuring that I don't end up with two files being created that have the same name?

For example,

data.zip

contains

20141015.zip 
20141008.zip

20141015.zip contains a directory, 20141015, which has two files in it,

smith.csv
jones.csv

20141008.zip contains a directory, 20141008, which has two more files in it, smith.csv and johnson.csv I would like to end up with one directory,

unzipped_data,

which would contain

20141015-smith.csv
20141015-jones.csv
20141008-smith.csv
20141008-johnson.csv

This question shows me how to unzip all the files in some folders and put them into corresponding folders, but I want all the files in one folder to simplify further operations, and I also have the problem of two layers of zipping to contend with, not just one.

Upvotes: 1

Views: 2482

Answers (1)

adimoise91
adimoise91

Reputation: 568

If I understand your needs, I think this script is good for you. I am waiting for your comment. :)

Instruction: Create a script.sh and copy the script in the directory where is data.zip archive. This script is working with any name of archives and any name of csv. I create a generalized script for your needs. If you need something please leave a comment.

#!/bin/bash

currLoc="$PWD"
path="${currLoc}"

cd ${currLoc}

#EXTRACT THE FIRST ARCHIVE IN A TEMP DIRECTORY

for filename in $path/*;
do  
    extension="${filename##*.}"
    if [ "${extension}" == 'zip' ]; then
        unzip $filename -d $path/temp
    fi

done

count=0

for filename in $path/temp/*;
do
    extension="${filename##*.}"         #EXTRACT THE EXTENSION TO COMPAIR IF IS AN ARCHIVE OR NOT
    name=${filename##*/}                #EXTRACT THE NAME OF ZIP FILE WITH EXTENSION
    name=${name%.*}                     #EXTRACT THE NAME OF ZIP FILE WITHOUT EXTENSION
    if [ "${extension}" == 'zip' ]; then
        ((count++))
        unzip $filename -d $path/temp/$count
        for file in $path/temp/$count/*
        do
            ext="${file##*.}"   
            if [ "${ext}" == 'csv' ]; then
                csvFileName=${file##*/}
                mv $path/temp/$count/$csvFileName $path/$name-$csvFileName
            fi
        done
    fi
done

#REMOVE THE TEMP DIRECTORY
rm -r $path/temp

Upvotes: 3

Related Questions