skip to Main Content

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?

2

Answers


  1. This should do what you expected :

    wsl -e bash -c "cd absolute/path/to && zip -r folder.zip folder"
    
    Login or Signup to reply.
  2. In Windows you use TAR -a to write a windows.zip / zip folder.

    So you can include or exclude the folders by navigation (-C <dir> Change to before processing remaining files.)

    Include sub folder:=

    C:temp>tar -a -cf folder.zip folder*.*
    
    C:temp>tar -tf folder.zip
    folder/file.1
    folder/file.2
    folder/file.ext
    folder/file.n
    

    Exclude sub folder by navigate 1st using -C (change):=

    C:temp>tar -a -cf folder.zip -Cfolder *.*
    
    C:temp>tar -tf folder.zip
    file.1
    file.2
    file.ext
    file.n
    

    enter image description here

    use TAR -h to see all options (but note -a is oddly not listed 🙂

    DO store files in your Windows filesystem that you want to access/create/modify using Windows tools AND Linux tools. HOWEVER
    "DO NOT, under ANY circumstances, access, create, and/or modify Linux files inside of your %LOCALAPPDATA% folder using Windows apps, tools, scripts, consoles, etc."
    https://devblogs.microsoft.com/commandline/do-not-change-linux-files-using-windows-apps-and-tools/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search