24 October 2026 · One command a day
How do I compress a folder into a tar.gz file on Linux?
The tar command packs a whole folder into a single compressed file. You use it before a risky change, or before you move a site, or any time you want one tidy file instead of thousands of loose ones. It comes with Ubuntu, so there is nothing to install.
I learn my automation tips in the AI Profit Boardroom. Have a look here. Affiliate link.
The command
tar -czf backup.tar.gz folder/-c creates a new archive. -z compresses it with gzip, which is where the .gz comes from. -f tells tar that the next word is the name of the archive file, so here it writes backup.tar.gz. The last part, folder/, is what goes into the archive. You can list more than one folder or file there.
What you will see
$ tar -czf sitename-backup.tar.gz sitename/
$ ls -lh sitename-backup.tar.gz
-rw-rw-r-- 1 user user 48M Oct 24 09:12 sitename-backup.tar.gz
$ sudo tar -czf ~/nginx-config.tar.gz /etc/nginx/
tar: Removing leading `/' from member namesWhen it works, tar says nothing and just gives you the prompt back. To check, list the new file with ls -lh and look at its size. The message about removing the leading / is not an error. tar stores paths without the leading slash, so that extracting the file later cannot overwrite /etc/nginx by accident. The second example uses sudo because some files in /etc are only readable by root.
When to use it
- Before you edit a site's files or run an update, so you can put everything back if it goes wrong.
- Before you move an app to another server. One file is much quicker to send than thousands of small ones. You can then copy the file to or from a server with scp.
- To save a copy of config folders such as
/etc/nginxbefore you change anything.
Watch out for
The f must come last in the group of flags, right before the file name. If you type tar -cfz backup.tar.gz folder/, tar takes z as the archive name. You end up with an uncompressed file called z, and backup.tar.gz is packed into it as if it were one of your folders. If that file does not exist, tar stops with an error instead. Also, do not save the archive inside the folder you are archiving. tar will try to add the archive to itself.
Questions people ask
How do I extract a tar.gz file?
Run tar -xzf backup.tar.gz to extract it into the current folder. Add -C /path/to/dir to extract it somewhere else, and run tar -tzf backup.tar.gz first if you want to see what is inside.
How do I leave out a folder like node_modules?
Put --exclude before the folder name, for example tar -czf backup.tar.gz --exclude='node_modules' appname/. You can repeat --exclude for each pattern you want to skip.
Packing up whole folders, system config included, 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.