Search code examples
bashmakefilezip

How to zip a file in Debian using bash with a single directory popping when unzipping


I am attempting to zip a directory called "project" into a .zip file called "BARBIER_MAM.zip". However, when unzipping the .zip file, all the files contained show up and I have to manually put them back in a directory. Here is how I currently do. In the "project" directory, I have a makefile with an "archive" target :

archive: clean
    zip -r BARBIER_MAM.zip .

I have also tried :

archive: clean
    cd ..
    zip -r BARBIER_MAM.zip project

But I got an error :

zip warning: name not matched: project

In case you would be asking, yes, it has to be done in the makefile contained in the directory that has to be zipped.


Solution

  • From the make documentation(emphasis mine):

    When it is time to execute recipes to update a target, they are executed by invoking a new sub-shell for each line of the recipe, unless the .ONESHELL special target is in effect (see Using One Shell) (In practice, make may take shortcuts that do not affect the results.)

    Please note: this implies that setting shell variables and invoking shell commands such as cd that set a context local to each process will not affect the following lines in the recipe. If you want to use cd to affect the next statement, put both statements in a single recipe line.

    So in your second example, change that to:

    archive: clean
        cd .. && zip -r BARBIER_MAM.zip project