Unzip the input file using shell script (ksh)

Hi,
I need help in unziping input file through shell script.
I had written script, which checks for input file extention.
If Extension is "zip" or "gz", then I want to do unzip/uncompress that file.
Caould you please let me know that, How to unzip a file through shell script (ksh).

Thanks in advance.:confused:

gzip -d  # uncompress gz file
unzip # unzip zip file

Thanks for the reply.

i had written as,

if [ $EXTN == "zip" ]; then
unzip $FILE
elif [ $EXTN == "gz" ]; then
gzip -d $FILE
elif [ $EXTN == "CSV" ]; then
// doing lots of things
// like parsing file line by line.

I just want to ask one question that,
I need to work on CSV file.If we unzip/uncrompress the file will it be stored in $FILE as extension is "CSV"?

no, it will generate a new file.

for example, a file named as abc.gz , after gzip -d abc.gz, you will get a file named: abc

sometimes, the original file will be deleted automatically.

But if I zip a .CSV file then what happen. After extraction it should be abc.CSV right?

So unzip $FILE will extract file into .CSV, so this changed name should I get into the $FILE or I need to assign it to variable like inputfile=`unzip $FILE` ?

normally gzip will keep the orig file name in new filename, so is the file is abc.csv, after gzip, the file name will be abc.csv.gz, then when uncompress it, the file name will be back to abc.csv

In this case, if you manually rename the gz file from abc.csv.gz to abc.xyz.gz, and uncompress it, you will get file name as abc.xyz

but zip command not, you need provide the dest filename when zip it, after unzip, the orig file name will be always restored.

Thanks a lot :slight_smile:

Should I write FILE = unzip $FILE? will it work to store file name in same variable?

It is easier with the "basename" command.

zipped="abc.csv.gz"
unzipped=`basename "${zipped}" ".gz"`
echo "${unzipped}"
abc.csv

Still it is not working. Here is my code,

unzipped=`basename "$FILE" ".zip"`
echo "unzipped file is $unzipped"
where $FILE = abc.zip (zip of abc.CSV file)

I am getting unzipped file is abc.
Please help me out. :frowning:

My code,

for file in ${DIR}/*\.zip
	do
	       unzip -oqq $file
	done

But I am executing script from another location and I am getting unzipped file into that location.
Actually I need unzipped file into same location DIR.
Please help me out.

with unzip man page:

       [-d exdir]
              An optional directory to which to extract files.  By default, all files and subdirectories are recreated in the current directory; the  -d  option
              allows  extraction  in  an arbitrary directory (always assuming one has permission to write to the directory).  This option need not appear at the
              end of the command line; it is also accepted before the zipfile specification (with the normal options), immediately after the zipfile  specifica-
              tion,  or between the file(s) and the -x option.  The option and directory may be concatenated without any white space between them, but note that
              this may cause normal shell behavior to be suppressed.  In particular, ''-d ~'' (tilde) is expanded by Unix C shells into the name of  the  user's
              home directory, but ''-d~'' is treated as a literal subdirectory ''~'' of the current directory.

Try:

for file in ${DIR}/*\.zip
	do
	       unzip -d $DIR -oqq $file
	done

Thanks a lot. Its working fine :slight_smile:

---------- Post updated at 04:51 AM ---------- Previous update was at 04:47 AM ----------

Also tell me the command for .gz type of file, to do the same as mentioned above.

normally, gunzip put the extracted files in the same directory where files are present.
so your files should be in $DIR already.
please try.

using gunzip I am getting only the name of the file, but not the extension.
I want extention as .CSV after gunzip.

gunzip $file

as told in previous posts, gunzip would just remove the .gz extension and give the original file.

gunzip file.gz would give you file

you can rename to files within your loop in you do want the csv extension.

for file in ${DIR}/*.gz
	do
	       gunzip $file
               mv ${file%\.*} ${file%\.*}.csv
               ## do other things
        done

But this is not satisfying to me.
I want to do something different.
Can we use,

 gzip $file

to uncompresss the file. If i am compressing .CSV file then definetly, I'll get .CSv extention after uncompression.
Please help me out.

Which tool will make .gz files?

If you initially having .csv files and compressing by your own. its ok.

gunzip and gzip -d are the same thing which used to uncompress the files.

if you having file named file.csv, then

gzip file.csv will produce file.csv.gz

and later while uncompressing, it will give the orignal name.

gunzip file.csv.gz will produce file.csv