Sunday, February 27, 2011

How to untar and ungzip .tar.gz files on Windows

If you need to extract the contents of a tarred, gzipped file (*.tar.gz) and you use Windows, do yourself a favor and download Cygwin. Once you've installed Cygwin, run it and you'll get something that resembles a Windows command prompt. Be careful and only run commands you are confident of, because Cygwin is quite powerful and you could unintentionally modify or damage files if you are not careful.

By default the Cygwin prompt's current working directory should be something like C:/cygwin/home/<your windows username>/

You can either copy the tarred, gzipped file here to unzip and untar it, or you can navigate to its current location on your hard drive.

If you wish to navigate to the file's existing location on your hard drive you need to access it through the /cygdrive/<drive letter>/ directory. e.g. if the file is located in C:/Users/johndoe/Downloads, you would execute the following:

cd /cygdrive/c/Users/johndoe/Downloads

Once you are in the same directory as the file, just replace "MyFile.tar.gz" with your file's name and "output-directory" with a directory name of your choosing below, and run the following commands. The first will create a directory for the files and the second will unzip and untar them:
mkdir output-directory
tar -zxvf MyFile.tar.gz -C output-directory

I use Cygwin for a variety of other utilities, such as verifying file checksums using md5sum. grep and sed also very nice to have on a Windows machine. These utilities just scratch the surface of Cygwin's capabilities.