Skip to content
TaeyoungKim.dev

gzip says a directory cannot be compressed: create and verify a tar.gz

LinuxWritten 3 min readTaeyoungKim
LinkedInX

You run gzip project and get an is a directory error instead of a compressed file. The directory is not necessarily too large, and a permissions problem is not implied. The missing step is collecting multiple files and their directory structure into one archive.

Why does gzip reject a directory?

gzip compresses file data into a .gz file. It does not create a single archive containing all files and subdirectories. Trying to turn one folder directly into project.gz therefore fails:

bash
lab_dir=$(mktemp -d)
mkdir -p "$lab_dir/site/config" "$lab_dir/restored"
printf 'port: 8080\n' > "$lab_dir/site/config/app.yml"
printf 'INFO ready\n' > "$lab_dir/site/app.log"

gzip "$lab_dir/site"
text
gzip: .../site is a directory

Some gzip implementations offer an option to visit directories recursively, but that still differs from putting the directory tree into one archive. For that, use tar to package the tree.

tar preserves the paths of the two example files inside one archive, while gzip compresses that archive. Extraction restores their relative paths.

How do you create a tar.gz file?

tar combines files and directories into an archive. The -z option also applies gzip compression:

bash
tar -czf "$lab_dir/site.tar.gz" -C "$lab_dir" site

Remember each option by its job:

OptionPurpose
-cCreate a new archive
-zApply gzip compression
-fUse the next value as the archive filename
-CChange to the specified directory before adding the named entry

With -C "$lab_dir" site, the archive starts at site/ rather than storing the entire temporary path. That makes the archive easier to move and extract elsewhere.

What should you inspect before extraction?

List the archive members with -t before extracting them. sort makes the display order consistent for this example:

bash
tar -tzf "$lab_dir/site.tar.gz" | sort
text
site/
site/app.log
site/config/
site/config/app.yml

The two files are under the expected site/ prefix, with no unexpected absolute path. Extract into an empty restored directory instead of the current project folder:

bash
tar -xzf "$lab_dir/site.tar.gz" -C "$lab_dir/restored"

Here -x means extract. A successful extraction command does not by itself prove the restored content is identical. Compare important files:

bash
cmp "$lab_dir/site/app.log" "$lab_dir/restored/site/app.log"
cmp "$lab_dir/site/config/app.yml" "$lab_dir/restored/site/config/app.yml"

Both commands should produce no output and exit with status 0, confirming byte-for-byte equality for these two files. This does not verify every recovery requirement, such as ownership, permissions, or links. Test the metadata and the complete restoration procedure when the archive is a backup.

When should you use tar and gzip separately?

For a single log file, gzip app.log can simply reduce its size. For several files and their directory structure in one deliverable, use tar -czf site.tar.gz site. The decision is whether you are compressing one file or packaging multiple entries with their paths.

For an archive received from elsewhere, inspect names with tar -tzf and extract into an empty directory. Extracting over an existing project can overwrite same-named files or place unexpected entries where they are hard to notice.

Key takeaways

gzip compresses data but does not package a folder tree into one archive. Use tar -czf to create a compressed archive, inspect it with tar -tzf, and restore it with tar -xzf ... -C separate-directory. Compare important restored files instead of relying on the extraction command's success alone.

Author

TaeyoungKim

Connecting technical foundations with implementation, verification, and production decisions.

#Linux#gzip#tar.gz#tar command#Extraction

Read next