Working with Linux archives
The use of archives in Linux is a common practice. Many programs, modules, entire datasets, media files, etc. are delivered in archived form. Therefore, the skill to work confidently with archives will be a definite plus for any developer. In this post I will try to describe possible scenarios of working with archives briefly and with examples.
Common Archive Formats
There are several common archive formats that work according to different algorithms and have different compression rates:
| Extension | Created | Compression | Common Usage |
|---|---|---|---|
.tar | 1979 | None | Bundling files, backups without compression |
.zip | 1989 | Medium | General-purpose archiving, file sharing |
.gz | 1992 | Medium | Compressing single log or text files |
.tar.gz .tgz | 1992 | Medium | Linux packaging, source code distribution |
.rar | 1993 | High | Multimedia archives, proprietary use |
.bz2 .tar.bz2 | 1996 | High | Compressing single files, archival |
.7z | 1999 | Very High | Maximum compression, large archives |
.xz .tar.xz | 2009 | Very High | Replacement for .bz2, very compact |
Tape Archive (.tar)
📦 Create a new archive (without compression) with some files and folder inside:
📦 Create a compressed archive with files and folders inside:
📦 Shorthand with .tgz (same command, just a different extension):
📂 Extract files from archive to specified directory:
📂 Extract files from the compressed archive to a specified directory:
📂 Extract .tgz:
🚩 Flags description:
-c: Create a new archive-f: File name of the archive-x: Extract files from an archive-z: Use gzip compression-t: List contents of the archive-v: Shows the files being processed-C: (uppercase C) Change the directory where the files will be extracted to
Zip Archive (.zip)
📦 Create a new archive with some files and folder inside:
📂 Extract files from archive to specified directory:
Gzip Archive (.gz)
.gzcompresses only single files — not folders. To compress multiple files, use it together withtar, e.g.,.tar.gz.
📦 Compress a single file:
📂 Decompress a .gz file to a current directory:
RAR Archive (.rar)
RAR is a proprietary format. You’ll need to install
rar(for creating) andunrar(for extracting) on most Linux systems.
📦 Create a new .rar archive with files and a folder inside:
📂 Extract files from the archive to a specified directory:
Bzip2 Archive (.bz2)
.bz2compresses a single file. To compress multiple files or directories, combine it withtar(i.e., use.tar.bz2).
📦 Compress a single file:
📂 Decompress a .bz2 file to a current directory:
7-Zip Archive (.7z)
Requires the
p7zippackage (p7zip-fullon most distros).
📦 Create a .7z archive with files and folders inside:
📂 Extract files from the archive to a specified directory:
🚩 Flags description:
a: Add files to archive (used to create)x: Extract with full directory structuree: Extract without directory structure (all files to current dir)-o: Set the output directory for extraction (no space after -o)-p: Set a password for encryption/decryption (optional)-m0=lzma2: Specify compression method (LZMA2 is default)
XZ Archive (.xz)
.xzcompresses a single file. To compress folders or multiple files, combine it withtar(see.tar.xz).
📦 Compress a single file:
📂 Decompress a .xz file to a specified directory:
🚩 Flags description:
- (no flag): Compress the specified file
-d: Decompress the file (same as unxz)-k: Keep the original file instead of deleting it after compression/decompression-v: Verbose output-TN: Use N threads for compression (e.g.,-T4)