25 October 2026 · One command a day
How do I extract a tar.gz file on Linux?
tar -xzf unpacks a .tar.gz archive and puts its files and folders back into the directory you are in. Use it when you need to restore a site from a backup, unpack an archive you copied from another server, or install a download. It does the opposite of compressing a folder into a tar.gz file.
I learn my automation tips in the AI Profit Boardroom. Have a look here. Affiliate link.
The command
tar -xzf backup.tar.gztar is the archiving tool that comes with Ubuntu. -x means extract. -z tells tar the archive is compressed with gzip, so it decompresses the file as it reads it. -f means the next word is the file name, so it must come last in the group of flags. backup.tar.gz is the archive you want to unpack. Modern tar can usually spot gzip by itself, so tar -xf also works, but -z makes your intent clear. Add -v to print each file name as it comes out.
What you will see
user@host:~$ tar -xzf backup.tar.gz
user@host:~$ ls
backup.tar.gz sitename
user@host:~$ ls sitename
css index.html package.json publicWhen it works, tar prints nothing and takes you back to the prompt. No output means no errors. The archive stays where it was, and a new folder appears next to it with the name of the folder that was archived. If you see an error such as gzip: stdin: not in gzip format, the file is probably not a real .tar.gz, or the download did not finish.
When to use it
- Restoring a site folder from last night's backup after a deploy goes wrong.
- Unpacking a backup you copied over from another server.
- Installing a tool that ships as a .tar.gz release instead of an apt package.
Watch out for
tar unpacks into your current directory. It overwrites any files with the same names and does not ask first. If you run it inside a live site folder, older files from the backup can quietly replace newer ones. Before you extract, run tar -tzf backup.tar.gz | head to see what is inside and which folder it will create. Then move to somewhere safe, such as a new empty directory, and extract there.
Questions people ask
How do I extract a tar.gz file to a different folder?
Add -C and a path, for example tar -xzf backup.tar.gz -C ~/restore. The folder must already exist, so create it first with mkdir -p ~/restore.
Do I need sudo to extract a tar.gz file?
Only if you are extracting into a folder you do not own, such as one under /var/www. With sudo, tar also restores the original owners saved in the archive. Without it, every file belongs to you, and you can change that afterwards by fixing file ownership.
Restoring your own backups straight from the command line is the kind of thing you can only do on a server you control. You can get the same VPS with 20% off at https://asksteves.co.uk/vps. Affiliate link.
Want more automation tips? The AI Profit Boardroom on Skool is where I get mine. Have a look here. Affiliate link.
Everything on this blog runs on one Hostinger KVM 8. This link takes 20% off the same box.
Affiliate link.