windowsubuntuzipwindows-subsystem-for-linux

How to zip using the zip command, but exclude parent directories (cd isn't an option)?


I want to start off by saying, I'm well aware that this question has been asked plenty of times, across multiple Stack Exchange sites, but none of them have proven useful to me.

The questions in question:

Unix zipping sub directory not including parent directory

zip all files and subfolder in directory without parent directory (unix.stackexchange.com)

Zip an archive without including parent directory (askubuntu.com)

How to zip a directory without zipping the parents directories? [duplicate] (askubuntu.com)

I am currently writing a small script, which, if ran, zips a specified folder using wsl zip. The problem I am encountering, is the fact that if I use absolute paths to tell the command what to zip, it includes the whole path, instead of only starting at the last folder, the folder I actually want to zip.

Many of the questions that have been answered about this, suggest using cd or pushd before trying to zip, and ommit the absolute path from the zip command. However, that is where it fails in my case.

I am using the Windows Subsystem for Linux to access the zip command, but I am doing so from within the Windows console. The command in question I am using:

> wsl -e zip -r absolute/path/to/folder.zip absolute/path/to/folder

But this will result in a zip file structure as the following:

folder.zip
  absolute
    path
      to
        folder
          *

While the result I am looking for is:

folder.zip
  folder
    *

I tried to use the command cd, to ommit the absolute path from the zip command, but with no luck. Because I need to run the command from within the Windows console, opposed to in the WSL console, the following is not an option:

> wsl ~
~$ cd absolute/path/to
absolute/path/to$ zip -r folder.zip folder

Instead, I tried using a command like this:

> wsl -e "cd absolute/path/to && zip -r folder.zip folder"

But that resulted in the following error:

<3>WSL (10963) ERROR: CreateProcessCommon:559: execvpe(cd) failed: No such file or directory

The same error occurs, if I simplify the command to this:

> wsl cd absolute/path/to

So here's the question again, how can I ommit the full directory from the zip, when I cannot use cd or pushd?


Solution

  • This should do what you expected :

    wsl -e bash -c "cd absolute/path/to && zip -r folder.zip folder"
    

    The first argument of wsl -e must be a command. To run a series of commands, you use bash -c ....