The gzip command in Linux compresses individual files, not folders directly. To zip a folder, you must first archive it with tar, then compress the archive with gzip. This creates a single .tar.gz file that preserves the folder structure and saves disk space. No extra tools are needed—tar and gzip are standard on nearly all Linux distributions.

What You'll Need

  • A Linux system with tar and gzip installed (standard on Ubuntu, Debian, Fedora, CentOS, and most others)
  • Terminal access
  • Permission to read the folder you want to compress and write to the destination directory

Step-by-Step

  1. Open your terminal by pressing Ctrl+Alt+T or searching for "Terminal" in your application menu.
  2. Navigate to the directory containing the folder you want to compress using the cd command. For example, if your folder is named "documents" in your home directory, type: cd ~/documents and press Enter.
  3. Check that the folder exists and is accessible by running: ls -la. Look for the folder name in the output. If it’s not there, verify the path or correct the folder name.
  4. Run the tar command to create an archive and compress it in one step. Use: tar -czvf foldername.tar.gz foldername/. Replace "foldername" with the actual name of your folder. The flags mean: -c creates a new archive, -z compresses with gzip, -v shows progress verbosely, and -f specifies the output filename.
  5. Wait for the command to finish. The terminal will display each file being added. Once done, you’ll see a new file named "foldername.tar.gz" in the current directory.
  6. Verify the archive was created by running: ls -lh foldername.tar.gz. The size should be significantly smaller than the original folder’s total size.
  7. Optionally, move or copy the .tar.gz file to another location using mv foldername.tar.gz /path/to/destination/ or cp foldername.tar.gz /path/to/destination/.

Common Mistakes

  • Using gzip foldername/ directly—this fails because gzip only compresses files, not directories.
  • Forgetting the trailing slash in the folder name (e.g., foldername instead of foldername/), which can cause tar to behave unpredictably if the folder is a symlink or has special permissions.
  • Using -z without -c or -f, which results in incomplete or malformed archives.
  • Running the command without write permission in the target directory, which causes "Permission denied" errors.
  • Typing the folder name incorrectly (case-sensitive or misspelled), which makes tar report "Cannot stat: No such file or directory".

Tips

  • To compress without seeing file-by-file output, omit the -v flag: tar -czf foldername.tar.gz foldername/. This is faster and cleaner for scripts.
  • Use absolute paths if compressing from outside the folder’s directory: tar -czvf /home/user/backup.tar.gz /home/user/documents/.
  • For better compression, use -9 as the compression level: tar -czvf9 foldername.tar.gz foldername/. This takes longer but reduces file size further.
  • Always test the archive after creation by extracting it to a temporary folder: mkdir test_extract && tar -xzvf foldername.tar.gz -C test_extract/.
  • For large folders, consider splitting the archive into chunks with split for easier transfer: tar -czf - foldername/ | split -b 1G - foldername.tar.gz.

Troubleshooting

  • "tar: Exiting with failure status due to previous errors"—this usually means one or more files in the folder are unreadable. Run ls -l foldername/ to check file permissions. If needed, use sudo before the tar command, but only if you’re certain the files are safe to access.
  • "No such file or directory"—double-check the folder name. Use tab completion (press Tab after typing part of the name) to auto-fill and avoid typos.
  • "Permission denied"—confirm you have read access to the source folder and write access to the destination. Use chmod or sudo if required, but avoid sudo unless necessary.
  • The .tar.gz file is the same size as the original—this happens if the folder contains already-compressed files (like .jpg, .mp4, .zip). gzip doesn’t compress these further. Consider using a different format like .zip if cross-platform compatibility is needed.
  • Command not found—rare, but if tar or gzip isn’t installed, install them with your package manager: sudo apt install tar gzip (Debian/Ubuntu) or sudo yum install tar gzip (CentOS/RHEL).

Frequently Asked Questions

Can I compress multiple folders into one .tar.gz file?

Yes. List them all after the output filename: tar -czvf archive.tar.gz folder1/ folder2/ folder3/. The archive will preserve each folder’s structure inside.

Does gzip delete the original folder after compression?

No. The original folder remains unchanged. gzip only creates a new compressed file. You must manually delete the folder afterward if you want to free up space.

How do I extract a .tar.gz file?

Use: tar -xzvf filename.tar.gz. The -x flag extracts, -z decompresses gzip, -v shows progress, and -f specifies the file. It extracts into the current directory by default.

Can I use gzip to compress a folder without tar?

No. gzip only works on single files. You must use tar to bundle the folder into one file first, then compress it.

Is .tar.gz the same as .zip?

No. .tar.gz combines archiving (tar) and compression (gzip). .zip combines both in one tool. .zip is more common on Windows and macOS, while .tar.gz is standard on Linux. Both work similarly, but .tar.gz often compresses better for text files.

Why is my .tar.gz file larger than expected?

If your folder contains mostly binary files (images, videos, executables), they are already compressed and won’t shrink much. gzip works best on text, logs, or code files. Try using -9 for maximum compression, or consider if compression is even needed for your data type.