I want to zip more than one file using gzip, but when I unzip it I should get multiple files.

I tried tar with zip but tar created extra folders that I don't want.

The files I want to zip are cp1.txt, cp2.txt, cp3.txt.

After unzipping the files should be cp1.txt, cp2.txt, cp3.txt

But using the tar command with gzip it is creating files like

/bbpre/spool/COPY/ZIP/cp1.txt,cp2.txt,cp3.txt

I don't want /bbpre/spool/COPY/ZIP/ I only want the files.

  • Can you edit your question to include the commands you are using to zip and unzip the files? – Burgi May 1 '16 at 14:53
  • Ugh. I loathe the terminology being used here. You don't zip files with gzip. You gzip files with gzip. If you're zipping files, you do so with a command such as the "zip" command (which typically refers to Info-Zip), or maybe another command (like 7za). gzip only handles one file per .gz archive. The workaround to that limitation is to use another format to archive multiple files and then have gzip compress that archive. In Unix (I note that this question has the Unix tag), the most traditional format to use for that task is tar. – TOOGAM May 1 '16 at 15:36

By tar during extraction you can strip-off the first n leading components of the filename/path.

In your case if you have done a tar from files:

/bbpre/spool/COPY/ZIP/cp1.txt  
/bbpre/spool/COPY/ZIP/cp2.txt
/bbpre/spool/COPY/ZIP/cp3.txt

by extract with strip-components flag you can remove unwanted path filename levels:

tar -xvzf yourtar.tar --strip-components=4

will remove the /bbpre/spool/COPY/ZIP and will put cp1.txt, cp2.txt, cp3.txt in the current working directory.

If I am understanding you correctly you want to take multiple files and zip them at once and then unzip them back into multiple files at once. Here is how to do this:

$ touch 1.txt 2.txt 3.txt
$ gzip 1.txt 2.txt 3.txt
$ gzip -d 1.txt.gz 2.txt.gz 3.txt.gz

I just tried this in a VM and it created 3 files, gzipped them separately, and then unzipped them separately.

  • below command making separate zip files i want one zip file and when i will unzip i want it separate files $ gzip 1.txt 2.txt 3.txt – Nitin Apr 29 '16 at 7:06
  • Yes, that is what my post advised. Did this work for you? – torchhound Apr 29 '16 at 7:07
  • no my requirement is create one zip file for all files and when i will unzip it i should get all files – Nitin Apr 29 '16 at 7:10
  • So you want to zip a directory with arbitrary files inside and then when you unzip have those files again? – torchhound Apr 29 '16 at 7:12
  • 1
    Is there a reason you cannot make a directory, place the files in said directory, and then zip the directory? – torchhound Apr 29 '16 at 7:22

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.