0
0
Linux-cliHow-ToBeginner · 3 min read

How to Extract a Tar File in Linux: Simple Commands

To extract a tar file in Linux, use the tar command with the -x option for extract and -f to specify the file, like tar -xf archive.tar. For compressed tar files, add the appropriate option: -z for gzip (.tar.gz) or -j for bzip2 (.tar.bz2).
📐

Syntax

The basic syntax to extract a tar file is:

  • tar: the command to work with tar archives
  • -x: tells tar to extract files
  • -f: specifies the archive file name
  • -z: (optional) decompress gzip compressed files
  • -j: (optional) decompress bzip2 compressed files
bash
tar -xf archive.tar

tar -xzf archive.tar.gz

tar -xjf archive.tar.bz2
💻

Example

This example shows how to extract a gzip compressed tar file named files.tar.gz into the current directory.

bash
tar -xzf files.tar.gz
⚠️

Common Pitfalls

Common mistakes include:

  • Forgetting the -f option, which causes tar to wait for input from stdin.
  • Using -z or -j incorrectly for files not compressed with gzip or bzip2.
  • Extracting files without write permission in the target directory.
bash
Wrong: tar -x files.tar.gz  # Missing -f option
Right: tar -xzf files.tar.gz
📊

Quick Reference

OptionDescriptionExample
-xExtract files from archivetar -xf archive.tar
-fSpecify archive filetar -xf archive.tar
-zUse gzip compressiontar -xzf archive.tar.gz
-jUse bzip2 compressiontar -xjf archive.tar.bz2
-vVerbose output (list files extracted)tar -xvf archive.tar

Key Takeaways

Use tar -xf filename.tar to extract uncompressed tar files.
Add -z for gzip and -j for bzip2 compressed tar files.
Always include the -f option to specify the archive file.
Check file permissions to avoid extraction errors.
Use -v for verbose output to see extracted files.