About compressed files in Unix

Compressed files take up less disk space than normal files, but you cannot read them in the usual way; you must first expand, or decompress, the files. How you do that depends on the program used to compress the file in the first place. See the appropriate section below for your compressed file's extension.

On this page:


.Z or .tar.Z

To extract .Z or .tar.Z files, at the shell prompt, enter:

 uncompress filename.Z

Use the ls command to check the resulting files. If uncompress creates a .tar file, you must extract the files by entering:

 tar -xvf filename.tar

Alternatively, to do this in one step and avoid creating the intermediate filename.tar file, enter:

 zcat filename.Z | tar -xvf -

.z or .gz

Files ending in .z or .gz were compressed with gzip, a newer and improved program. (At Indiana University, gzip is installed on UITS shared Unix systems.) To decompress a gzipped file, at the Unix prompt, enter:

 gunzip filename

Replace filename with the name of the file you wish to expand. It doesn't matter if you include the .z or .gz in the filename.

If you don't have enough disk space to decompress the file, or you only want to see the contents once and have the file stay compressed, you can send the contents of the file to the standard output (usually your terminal), by using the zcat command. For example, to read the decompressed contents of myfile.gz one page at a time, enter:

 zcat myfile | more

On some Unix systems, zcat may work only on .Z files, not .z or .gz files. If that seems to be true on your system, but the gzip command is installed, you can replace zcat myfile with gunzip -c myfile in the command above.

.bz2

Files ending in .bz2 have been compressed with bzip2. To decompress these files, enter one of the following commands:

 bunzip2 filename.bz2

or

 bzip2 -d filename.bz2

Replace filename with the name of the file you wish to expand.

.zip

Files created by zip can normally be decoded by programs such as WinZip and StuffIt Expander.

To decompress a zip file in Unix, use the unzip command. At the Unix prompt, enter:

 unzip filename

Replace filename with the name of the zip archive.

To create a zip file, at the prompt, enter:

 zip filename inputfile1 inputfile2

Replace filename with the name you want to give the zip file. The .zip extension is automatically appended to the end of the filename. Replace inputfile1 and inputfile2 with the names of the files you wish to include in the zip archive. You can include any number of files here, or you may use an asterisk (*) to include all files in the current directory.

To include the contents of a directory or directories in a zip archive, use the -r flag:

 zip -r filename directory

Replace directory with the name of the directory you want to include. This will create the archive filename.zip that contains the files and subdirectories of directory.

.tar

If, after you decompress an archive, you have a file whose name ends in .tar, you now have a tar (short for "tape archiving") archive, which is a way of collecting together several files, or even entire directories. For more, see In Unix, what is tar, and how do I use it?

.tgz

A .tgz file has been archived using the tar (tape archive) utility, then compressed using gzip. The gzip utility is an alternative to the compress program on Unix computers, and yields slightly better performance.

To decompress and unarchive the file at the same time, use the zcat program, which sends a gzipped file to "standard out" (the terminal):

 zcat file.tgz | tar xvf -

Replace file.tgz with the name of your file.

A file ending in .taz also has been tarred and gzipped, but is of an older file naming standard. The procedure for decompressing and unarchiving a .taz file is the same as indicated above.

Additional information

For more information about the commands discussed above, you can refer to the Unix man pages. At the Unix prompt, enter one of the following:

 man uncompress man gzip man zcat man tar man bzip2 man unzip man zip 

At Indiana University, for personal or departmental Linux or Unix systems support, see At IU, how do I get support for Linux or Unix?

Back to top

This is document aeqx in the Knowledge Base.
Last modified on 2017-05-16 11:51:57.

  • Fill out this form to submit your issue to the UITS Support Center.
  • Please note that you must be affiliated with Indiana University to receive support.
  • All fields are required.

Please provide your IU email address. If you currently have a problem receiving email at your IU account, enter an alternate email address.

  • Fill out this form to submit your comment to the IU Knowledge Base.
  • If you are affiliated with Indiana University and need help with a computing problem, please use the I need help with a computing problem section above, or contact your campus Support Center.

Please provide your IU email address. If you currently have a problem receiving email at your IU account, enter an alternate email address.