- 1 1. Basic Knowledge of Compression and Extraction on Ubuntu
- 2 2. How to Compress and Extract in zip Format on Ubuntu
- 3 3. How to Compress with tar.gz (tgz) (Linux Standard)
- 4 4. Choosing High-Compression Formats (bz2 / xz)
- 5 5. How to Compress Using the GUI (File Manager)
- 6 6. Practical Compression Techniques for Real Work
- 7 7. Common Errors During Compression and How to Fix Them
- 8 8. Summary: Which Ubuntu Compression Command to Use (Quick Reference)
1. Basic Knowledge of Compression and Extraction on Ubuntu
The main purposes of performing “compression” on Ubuntu are the following three:
- Reduce file size to save storage space
- Bundle multiple files into one for sending and sharing
- Create backups and archives (a packaged set for storage)
On Ubuntu, command-line (Terminal) operation is the standard. You can also do this via GUI (file manager), but in real work and server environments, command-line usage is the norm.
The first key point you should understand is that “compression” and “archiving” are different concepts.
1.1 The Difference Between Compression and Archiving
This is the point that most beginners find confusing.
- Compression
→ Processing that reduces the data size
Examples: gzip, bzip2, xz - Archive
→ Processing that bundles multiple files and folders into a single file
Example: tar
An Important Fact
The tar command is not “compression.”
Strictly speaking, it’s a command that “bundles only.”
If you want to compress at the same time, you combine it with gzip or xz.
Example:
tar -czvf archive.tar.gz folder/Meaning:
c= createz= gzip compressionv= verbose output (show processed items)f= specify the output filename
1.2 Common Compression Formats on Ubuntu
The following formats are commonly used on Ubuntu.
| Format | Features | Main use |
|---|---|---|
| zip | High Windows compatibility | Sharing with other OSes |
| tar.gz | Linux standard format | General-purpose compression |
| tar.bz2 | Higher compression than gzip | Prioritize smaller size |
| tar.xz | Very high compression | Large datasets |
How to Choose
- Share with Windows → zip
- Stay within Linux → tar.gz
- Size is the top priority → tar.xz
1.3 Basic Commands to Check Before Compressing
Check the current directory:
pwdList files:
ls -lCheck size:
du -sh folder/Common Stumbling Points
- Running commands without checking the current directory and creating files in an unexpected location
- Mistyping a relative path (e.g.,
../folder) - Skipping a size check before compressing and running out of disk space
1.4 Common Misunderstandings and Cautions
1. Images and Videos Don’t Compress Much
Because JPEG and MP4 are already compressed formats, their size usually won’t shrink much.
2. tar Alone Does Not Reduce Size
The following “bundles only.”
tar -cvf archive.tar folder/The size will be almost the same.
3. Large Files Increase CPU Load
CPU usage can get high, especially when using xz.
4. Watch Out for Permission Errors
If you handle system directories, you’ll need sudo.
2. How to Compress and Extract in zip Format on Ubuntu
The zip format offers the best compatibility when exchanging files between Ubuntu and Windows.
Many users who search for “ubuntu compression” are looking for zip first.
On Ubuntu, you use the zip and unzip commands.
First, check whether they are installed.
2.1 Check Whether zip Is Installed
You can check with the following command:
zip -vIf you get an error, install it:
sudo apt update
sudo apt install zip unzipCommon Mistakes
- Unable to extract because unzip is not installed
- Forgetting sudo and getting “Permission denied”
2.2 How to Compress Files with zip
Compress a single file:
zip sample.zip file.txtResult:
sample.zipis created- The original file is not deleted
Compress Multiple Files Together
zip sample.zip file1.txt file2.txt2.3 How to Compress a Folder (Important)
When compressing a folder, the -r option (recursive) is required.
zip -r sample.zip folder/If you omit -r, files inside the folder will not be compressed.
Common Stumbling Points
- Forgetting
-r - Mistyping a relative path
- Getting the command order wrong (output filename comes first)
Correct order:
zip -r output.zip target_folder2.4 How to Extract a zip File
Basic command:
unzip sample.zipExtract to a specified folder:
unzip sample.zip -d extracted_folderNotes
- If the destination already exists, you will be prompted to confirm overwriting
- If files with the same name exist, they may be overwritten
- If the path contains spaces, wrap it in quotes
Example:
unzip "sample file.zip"2.5 Create a Password-Protected zip
You can encrypt it for security purposes.
zip -e secure.zip file.txtYou will be prompted to enter a password.
Notes
- zip encryption is not very strong (not suitable for high-security use)
- Losing the password = difficult to recover
2.6 Check Size and Compression Ratio
Show zip file info:
unzip -l sample.zipCompare with the original size:
ls -lhWhy the Compression Ratio Might Be Low
- Images/videos are already compressed
- Text and logs usually compress well
3. How to Compress with tar.gz (tgz) (Linux Standard)
The most commonly used format in Linux environments is tar.gz.
It’s widely used on Ubuntu for source code distribution and backups.
tar.gz is a two-step process:
- Bundle files with tar (archive)
- Compress with gzip
Understanding this mechanism makes advanced usage easier.
3.1 Basic Command to Create a tar.gz Archive
Example: compress a folder
tar -czvf archive.tar.gz folder/Meaning of each option:
c= createz= gzip compressionv= verbose (show processed items)f= file (specify output filename)
Single File
tar -czvf archive.tar.gz file.txtMultiple Files
tar -czvf archive.tar.gz file1.txt file2.txt3.2 How to Extract a tar.gz Archive
Basic command:
tar -xzvf archive.tar.gzOption breakdown:
x= extractz= gzip decompressionv= show extracted itemsf= specify file
Extract to a Specific Directory
tar -xzvf archive.tar.gz -C target_folder/-C specifies the destination directory.
3.3 Important Notes When Using tar
1. Watch the Extracted Path Structure
The directory structure at creation time is preserved during extraction.
Example:
tar -czvf archive.tar.gz folder/When extracted:
folder/
└─ contentsIf you want to “extract only the contents,” you need to adjust paths when creating the archive.
2. Beware of Accidental Overwrites
If files already exist, they may be overwritten.
To check in advance:
tar -tzvf archive.tar.gz-t lists the archive contents.
3. Not Enough Disk Space
Compression may temporarily use extra space.
Check beforehand:
df -h3.4 Compress with gzip Only
Compress a single file only:
gzip file.txtResult:
file.txt.gzDecompress:
gunzip file.txt.gzNote
- gzip removes the original file (it replaces it after compression)
- Not suitable for bundling multiple files
3.5 Common Mistakes
- Mixing up the option order
- Forgetting to put the filename after
f - Putting
-Cin the wrong position - Misunderstanding that tar alone reduces size
4. Choosing High-Compression Formats (bz2 / xz)
If you want to reduce size as much as possible, use bzip2 (bz2) or xz, which typically compress better than gzip.
However, the higher the compression ratio, the higher the CPU load and the longer the processing time tends to be.
The basic guideline is as follows:
- Prioritize speed → gzip
- Prioritize compression ratio → xz
- Middle ground → bzip2
4.1 Create and Extract tar.bz2
How to Compress
tar -cjvf archive.tar.bz2 folder/Option breakdown:
c= createj= bzip2 compressionv= verbose outputf= specify output
How to Extract
tar -xjvf archive.tar.bz24.2 Create and Extract tar.xz
xz is currently one of the highest-compression formats on Linux.
How to Compress
tar -cJvf archive.tar.xz folder/Note: J must be uppercase (important).
How to Extract
tar -xJvf archive.tar.xz4.3 Differences in Compression Ratio and Processing Time
General trends (varies by environment):
- gzip → fast, standard compression ratio
- bzip2 → higher compression than gzip, somewhat slower
- xz → very high compression, slowest
Check CPU Load
topIt’s normal for CPU usage to rise when compressing large files with xz.

4.4 How to Choose in Real Work
For Backups
→ tar.xz (prioritize smaller size)
For Server Log Storage
→ tar.gz (balance)
For Temporary Transfers
→ tar.gz or zip
4.5 Common Mistakes and Cautions
- Typing
Jin lowercase and getting an error - xz not installed on older environments
- Server becomes slow due to CPU load
- Running out of space during compression
If xz is not installed:
sudo apt install xz-utils5. How to Compress Using the GUI (File Manager)
If you’re not comfortable with command-line operations, you can compress using Ubuntu’s file manager (usually “Files” = Nautilus).
On desktop environments, this is the easiest method.
5.1 Steps to Compress via Right-Click
Steps
- Select the file or folder you want to compress
- Right-click
- Select “Compress”
- Select a format
- .zip
- .tar.xz
- .7z (may appear depending on the environment)
- Click “Create”
The compressed file is created in the same directory.
5.2 How to Extract
Steps
- Right-click the compressed file
- Select “Extract Here” or “Extract To…”
- Extraction completes
5.3 Check When You Can’t Use the GUI
On minimal install environments or Ubuntu Server, the GUI is not available.
In that case, you must use command-line operations.
Check desktop environment:
echo $XDG_CURRENT_DESKTOPIf nothing is shown, you may not have a GUI environment.
5.4 Notes When Using the GUI
1. Processing Can Be Slow for Many Files
The progress indicator may be hard to understand.
2. You Can’t Specify Detailed Compression Levels
You can’t set gzip levels in the GUI.
3. CPU Usage Is Hard to See
If the PC becomes slow during processing, it may be hard to understand why.
4. Not Suitable for Server Use
Not available in SSH-based environments.
5.5 Common Mistakes
- Selecting a compression format without understanding the differences
- Not checking the destination and then “losing” the extracted files
- Overwriting existing files
6. Practical Compression Techniques for Real Work
Here we explain techniques that come up frequently in real work and server operations, beyond simple compression/extraction.
These include excluding logs, compressing only specific extensions, and password-protected zip—highly practical operations.
6.1 Compress Only Specific File Extensions
Example: compress only log files
tar -czvf logs.tar.gz *.logNotes
*(wildcard) is expanded by the shell automatically- Only targets files in the current directory
- Subfolders are not included
To include subfolders as well:
find . -name "*.log" -print0 | tar --null -czvf logs.tar.gz --files-from=-6.2 Exclude Specific Files When Compressing
Example: exclude .log files
tar --exclude="*.log" -czvf archive.tar.gz folder/Exclude multiple patterns:
tar --exclude="*.log" --exclude="*.tmp" -czvf archive.tar.gz folder/Common Mistakes
- Omitting quotes and causing the wildcard to expand
- Mistyping the exclude path
6.3 Specify Compression Level (gzip)
gzip allows specifying the compression level (1–9).
gzip -9 file.txt- 1 = fast, low compression
- 9 = slow, high compression
When specifying via tar:
tar -czvf archive.tar.gz folder/ --gzip -9Note: the syntax may differ depending on the environment.
6.4 Create a Password-Protected zip
zip -e secure.zip file.txtTo force encryption:
zip -P password secure.zip file.txtNote
-Pis not recommended because it remains in shell history- zip encryption is not very strong (not suitable for high-security use)
6.5 Split Archives (For Large Files)
To split a large archive:
zip -r -s 100m split.zip folder/It will be split into 100MB parts.
Notes
- All split parts are required to extract
- If any part is missing/corrupted, recovery is not possible
6.6 Compare Sizes Before and After Compression
Before compression:
du -sh folder/After compression:
ls -lh archive.tar.gzCommon reasons for low compression ratio:
- Already-compressed data such as images/videos
- Encrypted data compresses poorly
7. Common Errors During Compression and How to Fix Them
When compressing/extracting on Ubuntu, beginners often run into common errors.
Here are representative errors and specific solutions.
7.1 “command not found” Error
Example:
zip: command not foundCause
The command is not installed.
Solution
sudo apt update
sudo apt install zip unzipFor tar-related tools:
sudo apt install xz-utils7.2 “Permission denied”
Example:
tar: folder/file.txt: Cannot open: Permission deniedCause
Insufficient permissions to access the file or directory.
Solution
Check ownership:
ls -lUse sudo:
sudo tar -czvf archive.tar.gz folder/Note
Overusing sudo increases security risk.
7.3 “No space left on device”
Example:
No space left on deviceCause
Not enough disk space.
How to Check
df -hRemove unnecessary files:
sudo apt cleanCheck disk usage:
du -sh *7.4 Garbled Filenames When Extracting
This can happen when you receive a zip file created on Windows.
Cause
Differences in character encoding (UTF-8 vs Shift-JIS, etc.).
Example Fix
unzip -O cp932 sample.zipNote: varies by environment.
7.5 Accidental Overwrites
Existing files may be overwritten during extraction.
Check in advance:
tar -tzvf archive.tar.gzFor zip:
unzip -l sample.zip7.6 High CPU Load
It’s normal for CPU usage to increase during xz compression.
Check:
topIf needed, lower the compression level, or switch to gzip.
8. Summary: Which Ubuntu Compression Command to Use (Quick Reference)
Here’s a practical, use-case-based summary of what we covered.
This helps users searching “ubuntu compression” reach the conclusion quickly.
8.1 Recommended Formats by Use Case
| Use case | Recommended format | Reason |
|---|---|---|
| Sharing with Windows | zip | High compatibility |
| Standard use within Linux | tar.gz | Good balance |
| Size is the top priority | tar.xz | High compression |
| Backup storage | tar.gz / tar.xz | Prioritize stability |
| Temporary transfer | tar.gz | Prioritize speed |
8.2 Format Comparison (Speed vs Compression Ratio)
| Format | Compression ratio | Speed | CPU load |
|---|---|---|---|
| zip | Normal | Fast | Low |
| tar.gz | Normal | Fast | Low to medium |
| tar.bz2 | High | Slow | Medium |
| tar.xz | Very high | Very slow | High |
Note: compression ratio and speed vary depending on the data and environment.
8.3 Minimum Commands You Should Memorize
zip Compression
zip -r archive.zip folder/tar.gz Compression
tar -czvf archive.tar.gz folder/tar.gz Extraction
tar -xzvf archive.tar.gz8.4 A Simple Conclusion for Beginners
- If you’re unsure, use tar.gz
- If you need Windows sharing, use zip
- If you want the smallest possible size, use tar.xz
Your top priority is to memorize tar -czvf and tar -xzvf.
8.5 Common Bad Choices
- Misunderstanding that tar alone can compress
- Always choosing xz and slowing down processing
- Not understanding the difference between GUI and CLI
- Not checking disk usage before compressing


