Sunday, January 19, 2025
HomeComputer ScienceTAR - How to Untar a File

TAR – How to Untar a File

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

  1. Navigate to the Directory
    Open a terminal and navigate to the directory where your .tar file is located:

    cd /path/to/your/file
    
  2. Untar the File
    Use the tar 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

  1. For .tar.gz or .tgz Files
    These files are compressed with gzip:

    tar -xzf file.tar.gz
    
    • -z: Indicates gzip compression.
  2. For .tar.bz2 Files
    These files are compressed with bzip2:

    tar -xjf file.tar.bz2
    
    • -j: Indicates bzip2 compression.
  3. 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

  1. Extract a .tar file:
    tar -xf archive.tar
    
  2. Extract a .tar.gz file:
    tar -xzf archive.tar.gz
    
  3. Extract to a specific directory:
    tar -xf archive.tar -C /home/user/documents
    
  4. List contents of a .tar file:
    tar -tf archive.tar
    

Common Errors and Fixes

  • Error: tar: command not found
    Install tar using:

    sudo apt install tar        # Ubuntu/Debian
    sudo yum install tar        # CentOS/RedHat
    
  • Error: Permission Denied
    Use sudo to run the command with root privileges:

    sudo tar -xf file.tar
    
RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x