To extract a .zip file in Linux, run unzip file.zip. That is the whole command for the common case. But most Linux archives are not zips - they are .tar.gz or .tar.bz2, which need tar, not unzip. So the real answer is format-dependent: unzip for .zip, tar -xzf for .tar.gz, tar -xjf for .tar.bz2. The two mistakes people make are using the wrong tool for the format and dumping a hundred files into the current directory because the archive had no top-level folder.
Table of contents
- Extracting a .zip
- List before you extract
- tar for .tar.gz and .tar.bz2
- Extracting to a specific place, and a subset
- Other formats and the gzip-alone case
- How this fits the rest of the stack
- FAQ
Extracting a .zip
unzip file.zip # extract here
unzip file.zip -d /target/dir # extract into a specific directory
unzip file.zip extracts everything into the current directory. The -d flag sends the output somewhere specific, which is the safer habit - it keeps the extraction contained instead of spraying files where you happen to be standing.
unzip may not be installed by default on minimal systems. If you get command not found:
sudo apt install unzip # Debian/Ubuntu
sudo dnf install unzip # Fedora/RHEL
It is a tiny package and almost always available. Once installed, unzip handles password-protected archives (-P password), overwrite prompts, and listing - the essentials are covered below. For the plain case, unzip file.zip is all you need.
List before you extract
The single best habit with any archive: look inside before extracting. This tells you whether it will make a tidy folder or explode files everywhere.
unzip -l file.zip # list contents without extracting
tar -tzf archive.tar.gz # list a tar.gz without extracting
unzip -l (list) shows every file the archive contains. If the top level is a single folder, extraction will be tidy. If the top level is dozens of loose files, extracting in the current directory will scatter them among your existing files - the archive-bomb annoyance.
When the listing shows loose files, extract into a fresh directory:
mkdir extracted && unzip file.zip -d extracted
Thirty seconds of unzip -l saves you from a directory full of mixed-up files you then have to sort out by hand. Make listing-first a reflex for any archive you did not create yourself.
tar for .tar.gz and .tar.bz2
Most software you download for Linux comes as a compressed tarball, and unzip does nothing for those. Use tar:
tar -xzf archive.tar.gz # extract a gzip-compressed tarball
tar -xjf archive.tar.bz2 # bzip2-compressed
tar -xJf archive.tar.xz # xz-compressed
tar -xf archive.tar # uncompressed tar
Decode the flags once and you own tar: -x extract, -f file (name follows), and the compression letter - -z for gzip, -j for bzip2, -J for xz. So tar -xzf is extract, gzip, file. Add -v for verbose if you want to watch the file list scroll by.
Modern tar can often auto-detect the compression, so tar -xf archive.tar.gz frequently just works without the -z. But knowing the letters means you can read any tar command you find and know what it does, rather than copying flags on faith. -xzf is the one you will type most.
Extracting to a specific place, and a subset
Both tools let you control where files land and which files come out.
Extract a tarball into a chosen directory:
tar -xzf archive.tar.gz -C /opt/app # -C changes to that dir first
-C (uppercase) tells tar to extract into /opt/app rather than the current directory - the tar equivalent of unzip’s -d. Note the directory must exist first; tar will not create it.
Extract just one file or a pattern:
unzip file.zip "docs/*" # only the docs folder
tar -xzf archive.tar.gz path/to/file # just one file from the tarball
Pulling a single file out of a large archive - one config from a backup, one document from a bundle - is much faster than extracting everything and deleting the rest. Both tools support it, and it is worth knowing when you only need one thing from a big archive.
Other formats and the gzip-alone case
A few more you will meet:
gunzip file.gz # a lone .gz (single file, no tar)
unxz file.xz # a lone .xz
7z x archive.7z # a .7z (needs p7zip)
unrar x archive.rar # a .rar (needs unrar)
The distinction that trips people: .gz alone versus .tar.gz. A bare file.gz is a single compressed file - gunzip it and you get one file back. A .tar.gz is many files bundled by tar and then gzipped - it needs tar -xzf. Running gunzip on a .tar.gz leaves you with a .tar you still have to tar -xf, which is why people think gunzip did not work.
For .7z and .rar, you usually need to install a package first (p7zip, unrar). They are less common on Linux but appear when someone shares an archive made on Windows. Match the tool to the extension, list before extracting, and every archive format becomes routine.
How this fits the rest of the stack
Extracting archives is basic server hygiene, and the format-matching discipline - the right tool for the right extension - is the kind of small fluency that makes working on any Linux box faster. When your deploys package and unpack artifacts as part of a build pipeline, that handling happens for you, so the manual tar flags become something you rarely have to reach for. The RunxBuild hosting calculator lays out the service, database, storage, and bandwidth as separate line items, and the RunxBuild dashboard is where the team watches deploys, logs, and restarts as they happen.
Useful related references:
- How to Unzip a File in Linux: unzip, tar, gunzip, and 7z
- How to Change File Permissions Linux: chmod, chown, and umask
- How to Rename a File in Linux: mv, rename, and git mv
- Services on RunxBuild
FAQ
How do I unzip a file in Linux?
Run unzip file.zip to extract into the current directory, or unzip file.zip -d /target to extract into a specific folder. If unzip is not installed, add it with sudo apt install unzip on Debian/Ubuntu. Most Linux archives, though, are tarballs that need tar instead.
How do I extract a tar.gz file in Linux?
Use tar -xzf archive.tar.gz. The flags mean extract (-x), gzip (-z), and file (-f). For .tar.bz2 use -xjf and for .tar.xz use -xJf. Modern tar can often auto-detect compression, so tar -xf frequently works too.
What is the difference between .gz and .tar.gz?
A lone .gz is a single compressed file - gunzip file.gz gives one file back. A .tar.gz is many files bundled by tar and then gzipped, so it needs tar -xzf. Running gunzip on a .tar.gz leaves a .tar you still have to extract, which is why it seems not to work.
How do I list the contents of an archive before extracting?
Use unzip -l file.zip for a zip or tar -tzf archive.tar.gz for a tarball. Listing first shows whether the archive extracts into a tidy folder or scatters loose files, so you can extract into a fresh directory when needed and avoid mixing files into your current one.
How do I extract only one file from an archive?
For a zip, unzip file.zip "path/to/file"; for a tarball, tar -xzf archive.tar.gz path/to/file. Pulling a single file out is much faster than extracting everything and deleting the rest when you only need one item from a large archive.