Opening a compressed archive in Linux begins with identifying the file extension and selecting the appropriate extraction command. If you are wondering how to unzip a file in linux, the process is straightforward once you know the right tools. Linux handles many archive formats, and each one has its own command.
This guide covers the most common methods. You will learn to extract .zip, .tar.gz, .tar.bz2, .gz, and .bz2 files. We also cover installing missing tools and troubleshooting common errors.
How To Unzip A File In Linux
The first step is always checking the file extension. A .zip file uses the unzip command. A .tar.gz or .tgz file uses tar -xzf. A .tar.bz2 file uses tar -xjf. A plain .gz file uses gunzip. A .bz2 file uses bunzip2.
Let’s go through each method in detail. We will use real examples so you can follow along on your own terminal.
Check If The Archive Tool Is Installed
Before you unzip anything, make sure the required software is on your system. Most Linux distributions come with tar pre-installed. But unzip and bzip2 might not be there by default.
Run these commands to check:
which unzip– if it returns a path, you have it.which tar– almost always present.which gunzip– usually present.which bunzip2– may need installing.
If a tool is missing, install it using your package manager. For Debian/Ubuntu:
sudo apt update && sudo apt install unzip bzip2
For Red Hat/Fedora:
sudo dnf install unzip bzip2
For Arch:
sudo pacman -S unzip bzip2
How To Unzip A .Zip File
The unzip command is the standard way to handle .zip archives. Here is the basic syntax:
unzip filename.zip
This extracts all files into the current directory. If you want to extract to a specific folder, use the -d option:
unzip filename.zip -d /path/to/destination
To see the list of files without extracting, use -l:
unzip -l filename.zip
To extract only certain files, list them after the archive name:
unzip filename.zip file1.txt file2.jpg
If the zip file is password-protected, use the -P flag (not recommended for security reasons):
unzip -P password filename.zip
A safer way is to let unzip prompt you for the password:
unzip filename.zip
Then type the password when asked.
How To Extract A .Tar.Gz Or .Tgz File
The tar command handles .tar.gz and .tgz files. The flags are:
-x– extract-z– decompress with gzip-f– specify the archive file-v– verbose (optional)
Basic command:
tar -xzf archive.tar.gz
To extract to a specific directory:
tar -xzf archive.tar.gz -C /target/directory
To list contents without extracting:
tar -tzf archive.tar.gz
For a .tgz file, the same command works:
tar -xzf archive.tgz
How To Extract A .Tar.Bz2 File
For .tar.bz2 archives, replace the -z flag with -j:
tar -xjf archive.tar.bz2
To extract to a specific directory:
tar -xjf archive.tar.bz2 -C /target/directory
To list contents:
tar -tjf archive.tar.bz2
How To Decompress A .Gz File
If you have a single .gz file (not a .tar.gz), use gunzip:
gunzip file.gz
This removes the .gz file and leaves the decompressed version. To keep the original, use:
gunzip -k file.gz
Alternatively, use gzip -d:
gzip -d file.gz
How To Decompress A .Bz2 File
For .bz2 files, use bunzip2:
bunzip2 file.bz2
To keep the original:
bunzip2 -k file.bz2
Or use bzip2 -d:
bzip2 -d file.bz2
How To Extract A .Tar File (No Compression)
A plain .tar file is not compressed. Just use:
tar -xf archive.tar
Add -C for a target directory:
tar -xf archive.tar -C /target/directory
How To Extract A .Tar.Xz File
For .tar.xz archives, use the -J flag:
tar -xJf archive.tar.xz
To a specific directory:
tar -xJf archive.tar.xz -C /target/directory
How To Extract A .Zip File Without Unzip Installed
If you cannot install unzip, you can sometimes use 7z (p7zip) or jar. For example:
7z x filename.zip
Or use jar xf filename.zip if Java is installed. But installing unzip is the best practice.
How To Unzip Multiple Files At Once
Use a loop in the shell. For .zip files:
for file in *.zip; do unzip "$file"; done
For .tar.gz files:
for file in *.tar.gz; do tar -xzf "$file"; done
This extracts each archive in the current directory.
How To Unzip A File To A Different Directory
We already covered this for each format. Here is a quick recap:
- Zip:
unzip file.zip -d /path - Tar.gz:
tar -xzf file.tar.gz -C /path - Tar.bz2:
tar -xjf file.tar.bz2 -C /path - Gz:
gunzip -c file.gz > /path/file - Bz2:
bunzip2 -c file.bz2 > /path/file
How To Handle Permissions And Ownership
When you extract archives, the files usually keep the permissions from the archive. If you need to change ownership, use chown after extraction:
sudo chown -R user:group /extracted/folder
For permissions:
chmod -R 755 /extracted/folder
How To Unzip A File With A Specific Encoding
Sometimes zip files contain filenames with non-ASCII characters. Use the -O option to specify the encoding:
unzip -O UTF-8 filename.zip
Common encodings: UTF-8, CP932 (Japanese), GBK (Chinese).
How To Unzip A File In Linux Using The GUI
If you prefer a graphical interface, most Linux desktops have a file manager that can extract archives. Right-click the file and select “Extract Here” or “Extract to…”.
For GNOME, use File Roller. For KDE, use Ark. These tools support many formats.
Common Errors And Solutions
Error: “unzip: command not found”
Solution: Install unzip using your package manager.
Error: “End-of-central-directory signature not found”
Solution: The file is corrupted or not a valid zip. Try downloading again.
Error: “tar: This does not look like a tar archive”
Solution: You might be using the wrong flags. Check the file extension.
Error: “Permission denied”
Solution: Use sudo or change the output directory to one you own.
Error: “No space left on device”
Solution: Free up disk space or extract to a different drive.
How To Unzip A File In Linux Without Overwriting Existing Files
Use the -n flag with unzip:
unzip -n filename.zip
For tar, use --skip-old-files:
tar -xzf archive.tar.gz --skip-old-files
How To Unzip A File In Linux And Preserve The Directory Structure
All the commands we covered preserve the directory structure by default. The files are extracted into the folders they were stored in. If you want to flatten the structure, you need to use additional tools like find or cp.
How To Unzip A File In Linux And See The Progress
Use the -v flag for verbose output. For unzip:
unzip -v filename.zip
For tar:
tar -xzvf archive.tar.gz
This shows each file as it is extracted.
How To Unzip A File In Linux Using A Script
You can automate extraction with a simple bash script. Here is an example that extracts all archives in a folder:
#!/bin/bash
for file in *.zip; do
unzip "$file" -d "${file%.zip}"
done
This creates a folder for each zip file.
How To Unzip A File In Linux And Check The Integrity
For zip files, use the -t flag:
unzip -t filename.zip
For tar files, you can use tar -tf to list contents, but it does not check integrity. Use gzip -t for .gz files:
gzip -t file.gz
How To Unzip A File In Linux With A Different Compression Level
This applies when creating archives, not extracting. Extraction always decompresses fully.
How To Unzip A File In Linux Using The Command Line Only
All the commands in this guide are command-line based. You do not need a GUI. Just open a terminal and type the commands.
How To Unzip A File In Linux On A Remote Server
Use SSH to connect to the server, then run the same commands. For example:
ssh user@server
unzip file.zip
You can also use scp to copy the archive to your local machine first.
How To Unzip A File In Linux And Handle Large Files
Large archives can take time. Use pv (pipe viewer) to monitor progress:
pv filename.zip | unzip -
Install pv with sudo apt install pv or sudo dnf install pv.
How To Unzip A File In Linux And Ignore Certain Files
For zip files, use the -x option:
unzip filename.zip -x "*.txt"
This excludes all .txt files. For tar, use --exclude:
tar -xzf archive.tar.gz --exclude="*.txt"
How To Unzip A File In Linux And Overwrite Without Prompting
Use the -o flag with unzip:
unzip -o filename.zip
For tar, use --overwrite:
tar -xzf archive.tar.gz --overwrite
How To Unzip A File In Linux And Keep The Original Archive
All the commands we covered keep the original archive. Only gunzip and bunzip2 remove the compressed file by default. Use the -k flag to keep it.
Frequently Asked Questions
What Is The Command To Unzip A File In Linux?
The command is unzip filename.zip for .zip files. For .tar.gz, use tar -xzf filename.tar.gz.
How Do I Unzip A .Gz File In Linux?
Use gunzip file.gz or gzip -d file.gz. Add -k to keep the original.
Can I Unzip A File Without Installing Anything?
Most Linux systems have tar pre-installed. For .zip files, you may need to install unzip using your package manager.
How Do I Unzip A File To A Specific Folder?
For zip: unzip file.zip -d /path. For tar: tar -xzf file.tar.gz -C /path.
What If I Get “Command Not Found” When Trying To Unzip?
Install the missing tool. For Debian/Ubuntu: sudo apt install unzip. For Fedora: sudo dnf install unzip.
Now you know how to unzip a file in linux using multiple methods. Practice with different archive types to build confidence. The terminal is powerful once you master these basic commands.