Command to unzip or extract tar.gz file on Linux or macOS?


If you move to the Linux Dististituions such as Ubuntu or Fedora, or the macOS for Mac and Macbook, it's often that you will come across .tar.gz files, and will make you wonder "How do I open this tar.gz file?

The tar command

It is a General User Command that you can find at location /usr/bin/tar. Using this command you can archive or unarchive files.

How to untar a tar.gz file?

  • There are two options or flags that you need to make use of with tar, -x (--extract) and -f (--file) to extract it,

    # tar -xf data-file.tar.gz

    As you can see the myDir is the extracted directory that was created using tar -xf command,

    # ls -l
    total 8
    -rw-r--r-- 1 root root  213 Nov  5 10:24 data-file.tar.gz
    drwxr-xr-x 2 root root 4096 Nov  5 10:23 myDir
    
    # tree -L 2 myDir/
    myDir/
    |-- data.txt
    |-- insurance.xlsx
    |-- sales-report-2022.xlsx
    `-- sales.csv
    
    0 directories, 4 files
    Unzip a tar.gz file using tar command


Troubleshooting

  • If you get an error "bash: tar: command not found", then on you do not have the tar package installed on your system. You will need to make use of a Package manager to get it installed first.

    For Ubuntu:

    sudo apt-get tar

    For Alpine Linux:

    apk get tar

    For Amazon Linux:

    yum install tar

    Note that the order of options matter, -xf, if you provide -fx you will get an error,

    # tar -fx data-file.tar.gz 
    tar: You must specify one of the `-Acdtrux' or `--test-label'  options
    Try `tar --help' or `tar --usage' for more information.
-


Have Questions? Post them here!

Topics Coverage:


🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.

Follow Us: GitHub | Facebook | Twitter | Instagram