Linux Archive Commands Guide

Archive Commands

Commands for compressing, decompressing, and archiving files.

tar - Archive Files

Combines multiple files into a single archive file or extracts files from archives.

Option Description
-c Create a new archive
-x Extract files from archive
-t List contents of archive
-f [file] Specify archive filename
-v Verbose mode (show processed files)
-z Compress/decompress with gzip (.tar.gz, .tgz)
-j Compress/decompress with bzip2 (.tar.bz2)
-J Compress/decompress with xz (.tar.xz)
-C [dir] Extract to specified directory
--exclude=[pattern] Exclude files matching pattern

Examples:

tar -cvf archive.tar file1 file2 dir1 - Archive files and directories
tar -xvf archive.tar - Extract archive
tar -tvf archive.tar - List contents of archive
tar -czvf archive.tar.gz directory/ - Archive and compress directory with gzip
tar -xzvf archive.tar.gz - Extract gzip-compressed archive
tar -xzvf archive.tar.gz -C /path/to/extract - Extract to specified directory
tar -czvf backup-$(date +%Y%m%d).tar.gz /home/user/data - Create backup file with date in filename
tar --exclude='*.log' --exclude='tmp/' -czvf archive.tar.gz directory/ - Archive excluding specific files and directories
tar -czvf - directory/ | ssh user@remote "cat > archive.tar.gz" - Archive and transfer directly to remote server
tar -xvf archive.tar --wildcards '*.txt' - Extract only files matching pattern from archive

gzip/gunzip - gzip Compression/Decompression

Compresses or decompresses files using the gzip format.

Command/Option Description
gzip [file] Compress file (original file is replaced with .gz file)
gzip -k [file] Compress file while keeping original
gzip -d [file.gz] Decompress file (equivalent to gunzip)
gzip -l [file.gz] Display information about compressed file
gzip -[1-9] Specify compression level (1 is fastest with less compression, 9 is slowest with best compression)
gunzip [file.gz] Decompress gzip file

Examples:

gzip file.txt - Compress file (creates file.txt.gz and removes original)
gzip -k file.txt - Compress file while keeping original
gzip -9 file.txt - Compress file with maximum compression level
gunzip file.txt.gz - Decompress file
gzip -d file.txt.gz - Decompress file (equivalent to gunzip)
find . -type f -name "*.log" -exec gzip {} \; - Compress all log files
gzip -c file.txt > file.txt.gz - Output compressed result to stdout and redirect to save (keeps original)
gzip -r directory/ - Recursively compress all files in directory
gzip -tv file.txt.gz - Test integrity of compressed file

bzip2/bunzip2 - bzip2 Compression/Decompression

Compresses or decompresses files using the bzip2 format. Higher compression ratio than gzip but slower.

Command/Option Description
bzip2 [file] Compress file (original file is replaced with .bz2 file)
bzip2 -k [file] Compress file while keeping original
bzip2 -d [file.bz2] Decompress file (equivalent to bunzip2)
bzip2 -[1-9] Specify compression level (1 is fastest with less compression, 9 is slowest with best compression)
bunzip2 [file.bz2] Decompress bzip2 file

Examples:

bzip2 file.txt - Compress file (creates file.txt.bz2 and removes original)
bzip2 -k file.txt - Compress file while keeping original
bunzip2 file.txt.bz2 - Decompress file
bzip2 -d file.txt.bz2 - Decompress file (equivalent to bunzip2)
bzip2 -v file.txt - Show compression details
bzip2 -c file.txt > file.txt.bz2 - Output compressed result to stdout and redirect to save (keeps original)
find . -name "*.dat" -exec bzip2 -9 {} \; - Compress all .dat files with maximum compression
tar -cf - directory/ | bzip2 > directory.tar.bz2 - Combine tar and bzip2 to archive directory

xz - xz Compression/Decompression

Compresses or decompresses files using the xz format. Higher compression ratio than bzip2 but slower.

Option Description
-z Compress (default)
-d Decompress
-k Keep original files
-l Display information about compressed file
-[0-9] Specify compression level (0 is fastest with no compression, 9 is slowest with best compression)

Examples:

xz file.txt - Compress file (creates file.txt.xz and removes original)
xz -k file.txt - Compress file while keeping original
xz -d file.txt.xz - Decompress file
xz -l file.txt.xz - Display information about compressed file
xz -9 -e file.txt - Compress with maximum compression level and additional compression flags
xz -T0 large_file.dat - Compress using all available CPU threads (parallel processing)
find . -name "*.log" -size +10M -exec xz {} \; - Compress all log files larger than 10MB
tar -cf - directory/ | xz -9 > directory.tar.xz - Combine tar and xz with high compression to archive directory

zip/unzip - ZIP Compression/Decompression

Compresses or decompresses files using the ZIP format. High compatibility with Windows/macOS.

Command/Option Description
zip [archive.zip] [files] Compress files into ZIP format
zip -r [archive.zip] [directory] Recursively compress directory
zip -u [archive.zip] [files] Update existing ZIP archive
zip -e [archive.zip] [files] Compress with password protection
unzip [archive.zip] Extract ZIP archive
unzip -l [archive.zip] List contents of ZIP archive
unzip -d [directory] [archive.zip] Extract ZIP archive to specified directory

Examples:

zip archive.zip file1.txt file2.txt - Compress multiple files
zip -r archive.zip directory/ - Recursively compress directory
unzip archive.zip - Extract ZIP archive
unzip -l archive.zip - List contents of ZIP archive
unzip archive.zip -d /path/to/extract - Extract to specified directory
zip -e secure.zip confidential.txt - Compress file with password protection
zip -9 -r backup.zip documents/ -x "*.tmp" "*/temp/*" - Compress with maximum compression level excluding specific files
find . -name "*.jpg" -mtime -7 | zip -@ recent_photos.zip - Compress image files modified in the last 7 days
unzip -j archive.zip "*.txt" -d /path/to/extract - Extract only text files to specified directory ignoring paths

7z - 7-Zip Compression/Decompression

7-Zip is a high-compression archive format that can handle various archive formats.

Command/Option Description
7z a [archive.7z] [files] Add files to archive (compress)
7z x [archive.7z] Extract archive with full paths
7z e [archive.7z] Extract archive ignoring paths
7z l [archive.7z] List contents of archive
7z t [archive.7z] Test integrity of archive
7z u [archive.7z] [files] Update archive

Examples:

7z a archive.7z file1.txt file2.txt - Compress files in 7z format
7z a -r archive.7z directory/ - Recursively compress directory
7z x archive.7z - Extract 7z archive
7z l archive.7z - List contents of 7z archive
7z x archive.7z -o/path/to/extract - Extract to specified directory
7z a -p archive.7z confidential_files/ - Create password-protected archive
7z a -t7z -m0=lzma2 -mx=9 -mfb=64 -md=32m -ms=on archive.7z directory/ - Create archive with advanced compression settings
7z x archive.7z -r "*.txt" -o/path/to/extract - Extract only text files
find . -type f -size +10M | 7z a -si large_files.7z - Compress files larger than 10MB from standard input

zcat/zless - Display Contents of Compressed Files

Display contents of compressed files without decompressing them.

Command Description
zcat [file.gz] Display contents of gzip compressed file
bzcat [file.bz2] Display contents of bzip2 compressed file
xzcat [file.xz] Display contents of xz compressed file
zless [file.gz] Display contents of gzip compressed file with pager
bzless [file.bz2] Display contents of bzip2 compressed file with pager
xzless [file.xz] Display contents of xz compressed file with pager

Examples:

zcat file.txt.gz - Display contents of gzip compressed file
bzcat file.txt.bz2 - Display contents of bzip2 compressed file
zcat file.txt.gz | grep "pattern" - Search for text in compressed file
zless large_file.txt.gz - View large compressed file with pager
zcat file1.gz file2.gz > combined.txt - Combine contents of multiple compressed files
zcat log.gz | awk '/ERROR/ {print $0}' - Extract errors from compressed log file
find . -name "*.log.gz" -exec zcat {} \; | grep "CRITICAL" - Search for critical messages in multiple compressed log files
zcat -f file.txt file.txt.gz - Process both compressed and uncompressed files (with -f option)