ARCHIVED: In Unix, how can I read a file that ends in .Z, .z, .gz, or .bz2?

This content has been archived, and is no longer maintained by Indiana University. Information here may no longer be accurate, and links may no longer be available or reliable.

A file whose name ends in .Z, .z, .gz or .bz2 has been compressed, so it takes up less disk space. Unfortunately, you cannot read compressed files the way you do normal files. You must first expand, or uncompress, the files. How you do that depends on the program used to compress the file in the first place.

Files whose names end in .Z were compressed with the compress program. To expand such a file, at the Unix prompt, enter:

  uncompress filename

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

Files ending in .z or .gz were compressed with gzip, a newer and improved program. To uncompress 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 uncompress 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 uncompressed contents of myfile.gz one page at a time, enter:

  zcat myfile | more

Note: 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.

Files ending in .bz2 have been compressed with bzip2. To uncompress 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.

If, after you uncompress the files, you have a file whose name ends in .tar, you now have a tar archive, which is a way of collecting together several files, or even entire directories. To extract the files from an archive, at the Unix prompt, enter:

  tar -xvf filename.tar

Replace filename with the name of your file. If you don't see any error messages when you run the tar command, you may want to delete the tar archive so you do not waste disk space.

If you know in advance that you will be receiving a .tar file, and you want to save space and an extra step, you can combine uncompressing and extracting by entering:

  zcat filename | tar -xvf -

Be sure to include the - at the end of the command. If zcat does not work on .gz or .z files on your system, in the command above, replace zcat with gunzip -c. If the file was encoded with bzip2, replace zcat with bz2cat or bzip2 -dc.

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 tar
  man uncompress
  man zcat 
  man gzip
  man bzip2

At Indiana University, for personal or departmental Linux or Unix systems support, see Get help for Linux or Unix at IU.

This is document afcc in the Knowledge Base.
Last modified on 2018-01-18 09:59:58.