To untar (extract) a .tar
file in Linux or macOS using the tar
command, follow the steps below. The tar
command is a powerful utility that allows you to extract .tar
, .tar.gz
, .tar.bz2
, and other compressed tar files.
Basic Syntax
tar -xf <file.tar>
-x
: Extract files from the archive.-f
: Specifies the file name of the archive.
Steps to Untar a File
- Navigate to the Directory
Open a terminal and navigate to the directory where your.tar
file is located:cd /path/to/your/file
- Untar the File
Use thetar
command to extract the file:tar -xf file.tar
This will extract all files and directories in the
.tar
archive into the current directory.
Extracting Compressed .tar
Files
- For
.tar.gz
or.tgz
Files
These files are compressed with gzip:tar -xzf file.tar.gz
-z
: Indicates gzip compression.
- For
.tar.bz2
Files
These files are compressed with bzip2:tar -xjf file.tar.bz2
-j
: Indicates bzip2 compression.
- For
.tar.xz
Files
These files are compressed with xz:tar -xJf file.tar.xz
-J
: Indicates xz compression.
Extract to a Specific Directory
To extract files to a specific directory, use the -C
option:
tar -xf file.tar -C /path/to/extract
View Contents Before Extracting
If you want to see the contents of a .tar
file without extracting it:
tar -tf file.tar
-t
: List the files in the archive.
Verbose Output
To see detailed output during extraction, add the -v
option:
tar -xvf file.tar
-v
: Enables verbose mode to display file names being extracted.
Examples
- Extract a
.tar
file:tar -xf archive.tar
- Extract a
.tar.gz
file:tar -xzf archive.tar.gz
- Extract to a specific directory:
tar -xf archive.tar -C /home/user/documents
- List contents of a
.tar
file:tar -tf archive.tar
Common Errors and Fixes
- Error:
tar: command not found
Installtar
using:sudo apt install tar # Ubuntu/Debian sudo yum install tar # CentOS/RedHat
- Error: Permission Denied
Usesudo
to run the command with root privileges:sudo tar -xf file.tar