UNIX commands — Decompressing files

Websites often allow you to download a single compressed file that contains many other files. This makes it easier and faster to download through your browser since the file is smaller than the folder containing all the files. These compressed files usually end with these extensions:

  • .zip
  • .gz
  • .tar.gz
  • .tar.bz2

You can decompress these on your computer using your computer's software. Otherwise, you could upload the compressed file to your web server and decompress it there. This requires you to log into the server via SSH and run a command.

Flags used to decompress files

The commands unzip and tar are shown below. Each has several flags you can (or must) use to decompress the file.

zip flags

View the zip Linux man page for more information.

tar flags

  • f = Informs the tar command that the next parameter is the file name of the archive.
  • j = Filters the archive through bzip2.
  • v = Verbose (optional). Displays the files as they are decompressed.
  • x = Extracts files from an archive.
  • z = Filters the archive through gzip.
  • C = Sends the output to a specific directory.

.zip (Zip archive)

ZIP files end with the extension .zip. Run the following to decompress:

[server]$ unzip filename.zip
Archive:  filename.zip
  inflating: list
  inflating: of
  inflating: files
  inflating: being
  inflating: uncompressed
  inflating: from
  inflating: the
  inflating: zip
  inflating: archive

All files are extracted in the directory where you ran the command. View the following site for more information:

.gz (Gzip)

GZIP files end with the extension .gz. The gunzip command unzips a file so you can open it normally. This command is particularly important for DreamHost users. In your website's home directory is a log folder which stores your error.log and access.log files. View the following article for more information:

Older logs end with .gz. You can use the following command to unzip a log file and view its contents:

[server]$ gunzip error.log.2018-04-11.gz

There is no output when you decompress the file. Run ls -la to view the directory you just ran the command in. You'll see the file no longer ends with .gz, and you can now open it in any normal text editor to view its contents. View the following site for more information:

.tar

TAR files end with the extension .tar. By default, .tar files are not compressed, they just archive several files into a single file. If a .tar file was compressed, it would end with .tar.gz or tar.bz2.

However, you can still open a .tar file in a similar way as a compressed .tar file. Run the following commands to open a .tar file:

Extract in the current directory

[server]$ tar xvf filename.tar

The contents are extracted into the current directory.

Extract in a different directory

[server]$ tar xvf filename.tar -C /home/user/

The contents are extracted into the /home/user directory. View the following site for more information:

.tar.gz

Gzipped TAR files end with the extension .tar.gz. Run the following commands to decompress a file:

Extract in the current directory

[server]$ tar xvzf filename.tar.gz

This creates a new folder in your directory that is named the same as file.

Extract in a different directory

[server]$ tar -zxvf backup2.tar.gz -C /home/user/

This extracts the files into a folder in your /home/user/ directory. View the following site for more information:

.tar.bz2

Bzipped2 files end with the extension .tar.bz2. Run the following commands to decompress a file:

Extract in the current directory

[server]$ tar xvjf filename.bz2

This creates a new folder in your directory that is named the same as file.

Extract in a different directory

[server]$ tar xvjf filename.bz2 -C /home/user/

This extracts the files into a folder in your /home/user/ directory. View the following site for more information:

See also

Internal links

Did this article answer your questions?

Article last updated PST.