Archiving collects files; compression attempts to reduce their size. A plain tar archive is not compressed, while tar.gz adds gzip compression. ZIP is not Windows-only. Before running a command, identify the format and whether the operation keeps or replaces its source files.
Create, inspect and extract a ZIP
zip -r project.zip project/
unzip -l project.zip
unzip -t project.zip
mkdir zip-extracted
unzip -n project.zip -d zip-extracted/
The example expects project/ in the current directory. Check that the archive and destination names are unused. -r traverses subdirectories, including on Ubuntu. The unzip options list members, test archive data and avoid overwriting existing files, respectively. See the zip and unzip manuals.
Do not put a password in -P password: it can appear in shell history or process information. Extract normally in a terminal and allow the tool to prompt. If the encryption method is unsupported, use a maintained tool that supports it. Password-protected ZIP files do not all provide equivalent protection.
Archive a directory with tar.gz
tar -czf project.tar.gz project/
tar -tzf project.tar.gz
mkdir tar-extracted
tar -xzf project.tar.gz -C tar-extracted/
-c creates, -t lists, -x extracts and -z selects gzip. The argument after -f is the archive name. The directory supplied to -C must exist. The same gzip options handle .tgz; bzip2 uses -j and xz uses -J, with the corresponding compression tools installed.
Consult the GNU tar manual for options. Do not extract an unknown archive as root or into a live website directory. Use an empty, restricted destination. Listing filenames is useful, but it does not establish trust.
Keep the source when using gzip
A successful plain gzip report.txt operation normally replaces its input with the compressed version. To keep the original and write a separate output:
gzip -c report.txt > report.txt.gz
gzip -t report.txt.gz
gzip -dc report.txt.gz > report-extracted.txt
Shell redirection overwrites an existing destination, so check those filenames first. The GNU gzip manual describes standard-output mode and source preservation. gzip processes a file or stream; a directory tree needs an archive first.
Handle RAR with the right tool
RAR creation and extraction may use separate rar and unrar programs. Check the installed version and licensing. Use unrar l archive.rar to list members and unrar x archive.rar to preserve directory paths during extraction. Run extraction inside an empty destination. The e command discards paths, which can cause surprises with matching filenames. Check RARLAB's resources for official distributions.
The existence of an archive does not prove that a backup can be restored. Test extraction, compare the contents and verify necessary permissions before removing source data. Do not make automatic --remove-files deletion part of a default backup recipe. A password or successful integrity test does not certify that the contents are harmless.
Related Articles