Avoiding unwanted directory while using zip

ZIP files are one of the most widely supported compression formats, especially when sharing files across different operating systems like Linux, Windows, and Mac. However, when creating ZIP archives from the command line, we often encounter the problem of unwanted directory structures that make the extracted files difficult to navigate.

This article explores techniques to create clean ZIP archives without unnecessary parent directories, using Linux command-line tools and bash built-in commands like pushd and popd.

The Problem with Full Path Compression

When using the zip command with the -r (recursive) option, the entire directory path gets included in the archive. Let's examine this with an example directory structure:

~$ ls -R /home/allan/Source/twinejs/src/vue/
/home/allan/Source/twinejs/src/vue/:
codemirror-theme.less directives index.less mixins transitions.less

/home/allan/Source/twinejs/src/vue/directives:
mouse-scrolling.js mouse-scrolling.less

/home/allan/Source/twinejs/src/vue/mixins:
dom-events.spec.js mount-to.js

Using the standard recursive zip command:

zip -r ~/twine-vue.zip /home/allan/Source/twinejs/src/vue

This creates an archive containing the full path structure. When recipients extract the file, they must navigate through multiple nested folders (home/allan/Source/twinejs/src/vue) to access the actual content, which is inconvenient and cluttered.

Using the -j Option to Flatten Structure

The -j option "junks" the directory paths, keeping only the filenames. This flattens the entire directory structure:

zip -rj ~/twine-vue.zip /home/allan/Source/twinejs/src/vue
adding: codemirror-theme.less (deflated 73%)
adding: mouse-scrolling.less (deflated 37%)
adding: mouse-scrolling.js (deflated 68%)
adding: index.less (stored 0%)
adding: mount-to.js (deflated 31%)
adding: dom-events.spec.js (deflated 69%)
adding: transitions.less (deflated 63%)

While this removes unwanted parent directories, it also eliminates the useful internal folder structure, placing all files in the root of the archive.

The pushd and popd Solution

The optimal approach is to change to the parent directory of the target folder before creating the archive. Bash provides pushd and popd commands that maintain a directory stack, allowing safe navigation without losing track of the original location.

Using pushd to Navigate

The pushd command changes to a specified directory while pushing the current directory onto a stack:

pushd /home/allan/Source/twinejs/src/
~/Source/twinejs/src /usr/local/bin

Now we can create the ZIP archive using a relative path:

zip -r ~/twine-vue.zip vue/
adding: vue/ (stored 0%)
adding: vue/codemirror-theme.less (deflated 73%)
adding: vue/directives/ (stored 0%)
adding: vue/directives/mouse-scrolling.less (deflated 37%)
adding: vue/directives/mouse-scrolling.js (deflated 68%)
adding: vue/index.less (stored 0%)
adding: vue/mixins/ (stored 0%)
adding: vue/mixins/mount-to.js (deflated 31%)
adding: vue/mixins/dom-events.spec.js (deflated 69%)
adding: vue/transitions.less (deflated 63%)

This creates a clean archive where recipients get a single vue folder containing the organized directory structure.

Using popd to Return

To return to the original directory, use popd:

popd
/usr/local/bin

This restores the previous working directory from the stack, making it safe for use in scripts where maintaining the correct working directory is crucial.

Practical Example Script

Here's a complete example for creating clean ZIP archives:

#!/bin/bash
# Navigate to parent directory of target
pushd /path/to/parent/directory
# Create clean ZIP archive
zip -r ~/archive-name.zip target-folder/
# Return to original location
popd

Conclusion

Using pushd and popd provides an elegant solution for creating ZIP archives with clean directory structures. This approach preserves the internal organization of folders while avoiding unwanted parent directory paths, making the archives more user-friendly for recipients across different platforms.

Updated on: 2026-03-17T09:01:38+05:30

282 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements