Compressing files on Linux
A simple guide to compressing (zipping) and extracting (unzipping) files on the Linux command line.
There are two predominant compression systems in use on the Linux command line; Zip and Tarball. Zip files are stored in an archive with the .zip file extension, whilst Tarballs are stored as either .tar or .tar.gz. A tar file (with the .tar extension) is an archive that combines multiple files into one without compression, while a tar.gz file (with the .tar.gz extension) is a compressed version of a tar file created using the gzip compression method. The tar.gz file is smaller in size and is used for easier storage and transfer of multiple files.
The creation of archives of compressed files is useful for saving space on servers and transferring large numbers of files, or very large files.
Key tasks:
- Compress a single file
- Compress files of type in a single directory
- Preview an archive
- Extract an archive
Compress a single file
To compress a single file into a Tarball, use the following format:
tar -czf [name of tarball] [name of file to compress]
To compress a single file into a Zip file, use the following format:
zip -r [name of zip file] [name of file to compress]
Examples:
tar -czf filename.tar.gz file.xyz
zip -r filename.zip file.xyz
Compress files of type in a single directory
To compress files of a certain type into a single archive, use the following code for a Tarball:
tar -czf [name of tarball] [filetype]
For instance;
tar -czf filename.tar.gz *.pt
The code for a zip file is similar:
zip -r [name of zip file] [filetype]
For example;
zip -r filename.zip *.pt
Preview an archive
To preview a Tarball on the command line, use the following code (where my-data.tar.gz is the file that I want to preview):
tar -ztvf my-data.tar.gz
The code to preview a zip file is as follows (where my-data.zip is the file that I want to preview):
unzip -l my-data.zip
Extract an archive
To extract a Tarball, use the following syntax to unzip to the current directory:
tar -xvzf filename.tar.gz
If you want the file to be extracted elsewhere, do this:
tar -xvzf filename.tar.gz -C /path/to/directory
To unzip a zip file, use the following code:
unzip filename.zip
Comments
No comments have yet been submitted. Be the first!