Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am trying compress a folder so I can FTP it to my web server.

I am compressing on osx snow leopard and decompressing on ubuntu 8.04

These are the two commands I am running

sudo git archive master | gzip > v-1.25.gz

sudo gunzip v1.25.gz

When I decompress on my local machine it works fine, but on the server the output is a file not a directory

What am I doing wrong?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

If you're coming from a Windows world, you're used to using tools like zip or rar, which compress collections of files. In the typical Unix tradition of doing one thing and doing one thing well, you tend to have two different utilities; a compression tool and a archive format. People then use these two tools together to give the same functionality that zip or rar provide.

There are numerous different compression formats; the common ones used on Linux these days are gzip (sometimes known as zlib) and the newer, higher performing bzip2. Unfortunately bzip2 uses more CPU and memory to provide the higher rates of compression. You can use these tools to compress any file and by convention files compressed by either of these formats is .gz and .bz2. You can use gzip and bzip2 to compress and gunzip and bunzip2 to decompress these formats.

There are also several different types of archive formats available, including cpio, ar and tar, but people tend to only use tar. These allow you to take a number of files and pack them into a single file. They can also include path and permission information. You can create and unpack a tar file using the tar command. You might hear these operations referred to as "tarring" and "untarring". (The name of the command comes from a shortening of Tape ARchive. Tar was an improvement on the ar format in that you could use it to span multiple physical tapes for backups).

  # tar -cf archive.tar list of files to include

This will create (-c) and archive into a file -f called archive.tar. (.tar is the convention extention for tar archives). You should now have a single file that contains five files ("list", "of", "files", "to" and "include"). If you give tar a directory, it will recurse into that directory and store everything inside it.

  # tar -xf archive.tar
  # tar -xf archive.tar list of files

This will extract (-x) the previously created archive.tar. You can extract just the files you want from the archive by listing them on the end of the command line. In our example, the second line would extract "list", "of", "file", but not "to" and "include". You can also use

  # tar -tf archive.tar

to get a list of the contents before you extract them.

So now you can combine these two tools to replication the functionality of zip:

  # tar -cf archive.tar directory
  # gzip archive.tar

You'll now have an archive.tar.gz file. You can extract it using:

  # gunzip archive.tar.gz
  # tar -xf archive.tar

We can use pipes to save us having an intermediate archive.tar:

  # tar -cf - directory | gzip > archive.tar.gz
  # gunzip < archive.tar.gz | tar -xf -

You can use - with the -f option to specify stdin or stdout (tar knows which one based on context).

We can do slightly better, because, in a slight apparent breaking of the "one job well" idea, tar has the ability to compress its output and decompress its input by using the -z argument (I say apparent, because it still uses the gzip and gunzip commandline behind the scenes)

 # tar -czf archive.tar.gz directory
 # tar -xzf archive.tar.gz

To use bzip2 instead of gzip, use bzip2, bunzip2 and -j instead of gzip, gunzip and -z respectively (tar -cjf archive.tar.bz2). Some versions of tar can detect a bzip2 file archive with you use -z and do the right thing, but it is probably worth getting in the habit of being explicit.

Going back to your particular problem. git archive can create either a tar file or a zip file. By default it creates a tar file.

 # git archive master | gzip > v-1.25.tar.gz

This would create a compressed tar archive. As we saw above, we can extract this using:

 # tar -xzf v-1.25.tar.gz

You can also use:

 # git archive --format=zip master > v-1.25.zip
 # unzip v-1.25.zip

More info:

share|improve this answer
    
A bit encyclopedic answer :) –  AlberT Oct 13 '09 at 10:22
    
was bored, and figured if you're gonna do something, might as well do it properly :) –  David Pashley Oct 13 '09 at 10:45

g(un)zip doesn't usually directly support archiving - just compression of a single file. It is most common to use the tar archiver then gzip the tar file. I am guessing that the gzip variant you are using to compress the data is noticing the input is a directory and adding the extra step automatically (and doing the same when decompressing).

Try tar -xvzf v1.25.gz or gunzip v1.25.gz | tar -xv
(remove the v from the tar options if you don't want verbous output, which lists the files as they are extracted)
Or if you already have the file unzipped you can also try tar -xvf v1.25 as the file should, if I am guessing right, be a tar archive.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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