Commands for compressing, decompressing, and archiving 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 directoriestar -xvf archive.tar
- Extract archivetar -tvf archive.tar
- List contents of archivetar -czvf archive.tar.gz directory/
- Archive and compress directory with gziptar -xzvf archive.tar.gz
- Extract gzip-compressed archivetar -xzvf archive.tar.gz -C /path/to/extract
- Extract to specified directorytar -czvf backup-$(date +%Y%m%d).tar.gz /home/user/data
- Create backup file with date in filenametar --exclude='*.log' --exclude='tmp/' -czvf archive.tar.gz directory/
- Archive excluding specific files and directoriestar -czvf - directory/ | ssh user@remote "cat > archive.tar.gz"
- Archive and transfer directly to remote servertar -xvf archive.tar --wildcards '*.txt'
- Extract only files matching pattern from archive
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 originalgzip -9 file.txt
- Compress file with maximum compression levelgunzip file.txt.gz
- Decompress filegzip -d file.txt.gz
- Decompress file (equivalent to gunzip)find . -type f -name "*.log" -exec gzip {} \;
- Compress all log filesgzip -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 directorygzip -tv file.txt.gz
- Test integrity of compressed file
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 originalbunzip2 file.txt.bz2
- Decompress filebzip2 -d file.txt.bz2
- Decompress file (equivalent to bunzip2)bzip2 -v file.txt
- Show compression detailsbzip2 -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 compressiontar -cf - directory/ | bzip2 > directory.tar.bz2
- Combine tar and bzip2 to archive directory
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 originalxz -d file.txt.xz
- Decompress filexz -l file.txt.xz
- Display information about compressed filexz -9 -e file.txt
- Compress with maximum compression level and additional compression flagsxz -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 10MBtar -cf - directory/ | xz -9 > directory.tar.xz
- Combine tar and xz with high compression to archive directory
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 fileszip -r archive.zip directory/
- Recursively compress directoryunzip archive.zip
- Extract ZIP archiveunzip -l archive.zip
- List contents of ZIP archiveunzip archive.zip -d /path/to/extract
- Extract to specified directoryzip -e secure.zip confidential.txt
- Compress file with password protectionzip -9 -r backup.zip documents/ -x "*.tmp" "*/temp/*"
- Compress with maximum compression level excluding specific filesfind . -name "*.jpg" -mtime -7 | zip -@ recent_photos.zip
- Compress image files modified in the last 7 daysunzip -j archive.zip "*.txt" -d /path/to/extract
- Extract only text files to specified directory ignoring paths
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 format7z a -r archive.7z directory/
- Recursively compress directory7z x archive.7z
- Extract 7z archive7z l archive.7z
- List contents of 7z archive7z x archive.7z -o/path/to/extract
- Extract to specified directory7z a -p archive.7z confidential_files/
- Create password-protected archive7z a -t7z -m0=lzma2 -mx=9 -mfb=64 -md=32m -ms=on archive.7z directory/
- Create archive with advanced compression settings7z x archive.7z -r "*.txt" -o/path/to/extract
- Extract only text filesfind . -type f -size +10M | 7z a -si large_files.7z
- Compress files larger than 10MB from standard input
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 filebzcat file.txt.bz2
- Display contents of bzip2 compressed filezcat file.txt.gz | grep "pattern"
- Search for text in compressed filezless large_file.txt.gz
- View large compressed file with pagerzcat file1.gz file2.gz > combined.txt
- Combine contents of multiple compressed fileszcat log.gz | awk '/ERROR/ {print $0}'
- Extract errors from compressed log filefind . -name "*.log.gz" -exec zcat {} \; | grep "CRITICAL"
- Search for critical messages in multiple compressed log fileszcat -f file.txt file.txt.gz
- Process both compressed and uncompressed files (with -f option)