How to Compress and Extract Files on Ubuntu (zip, tar.gz, tar.xz) — Commands, GUI, and Troubleshooting

目次

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 = create
  • z = gzip compression
  • v = 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.

FormatFeaturesMain use
zipHigh Windows compatibilitySharing with other OSes
tar.gzLinux standard formatGeneral-purpose compression
tar.bz2Higher compression than gzipPrioritize smaller size
tar.xzVery high compressionLarge 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:

pwd

List files:

ls -l

Check 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 -v

If you get an error, install it:

sudo apt update
sudo apt install zip unzip

Common 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.txt

Result:

  • sample.zip is created
  • The original file is not deleted

Compress Multiple Files Together

zip sample.zip file1.txt file2.txt

2.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_folder

2.4 How to Extract a zip File

Basic command:

unzip sample.zip

Extract to a specified folder:

unzip sample.zip -d extracted_folder

Notes

  • 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.txt

You 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.zip

Compare with the original size:

ls -lh

Why 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:

  1. Bundle files with tar (archive)
  2. 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 = create
  • z = gzip compression
  • v = verbose (show processed items)
  • f = file (specify output filename)

Single File

tar -czvf archive.tar.gz file.txt

Multiple Files

tar -czvf archive.tar.gz file1.txt file2.txt

3.2 How to Extract a tar.gz Archive

Basic command:

tar -xzvf archive.tar.gz

Option breakdown:

  • x = extract
  • z = gzip decompression
  • v = show extracted items
  • f = 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/
  └─ contents

If 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 -h

3.4 Compress with gzip Only

Compress a single file only:

gzip file.txt

Result:

file.txt.gz

Decompress:

gunzip file.txt.gz

Note

  • 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 -C in 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 = create
  • j = bzip2 compression
  • v = verbose output
  • f = specify output

How to Extract

tar -xjvf archive.tar.bz2

4.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.xz

4.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

top

It’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 J in 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-utils

5. 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

  1. Select the file or folder you want to compress
  2. Right-click
  3. Select “Compress”
  4. Select a format
    • .zip
    • .tar.xz
    • .7z (may appear depending on the environment)
  5. Click “Create”

The compressed file is created in the same directory.

5.2 How to Extract

Steps

  1. Right-click the compressed file
  2. Select “Extract Here” or “Extract To…”
  3. 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_DESKTOP

If 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 *.log

Notes

  • * (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 -9

Note: the syntax may differ depending on the environment.

6.4 Create a Password-Protected zip

zip -e secure.zip file.txt

To force encryption:

zip -P password secure.zip file.txt

Note

  • -P is 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.gz

Common 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 found

Cause

The command is not installed.

Solution

sudo apt update
sudo apt install zip unzip

For tar-related tools:

sudo apt install xz-utils

7.2 “Permission denied”

Example:

tar: folder/file.txt: Cannot open: Permission denied

Cause

Insufficient permissions to access the file or directory.

Solution

Check ownership:

ls -l

Use 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 device

Cause

Not enough disk space.

How to Check

df -h

Remove unnecessary files:

sudo apt clean

Check 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.zip

Note: varies by environment.

7.5 Accidental Overwrites

Existing files may be overwritten during extraction.

Check in advance:

tar -tzvf archive.tar.gz

For zip:

unzip -l sample.zip

7.6 High CPU Load

It’s normal for CPU usage to increase during xz compression.

Check:

top

If 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 caseRecommended formatReason
Sharing with WindowszipHigh compatibility
Standard use within Linuxtar.gzGood balance
Size is the top prioritytar.xzHigh compression
Backup storagetar.gz / tar.xzPrioritize stability
Temporary transfertar.gzPrioritize speed

8.2 Format Comparison (Speed vs Compression Ratio)

FormatCompression ratioSpeedCPU load
zipNormalFastLow
tar.gzNormalFastLow to medium
tar.bz2HighSlowMedium
tar.xzVery highVery slowHigh

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.gz

8.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