Zip command without including the compressed dir itself

ⅰ亾dé卋堺 提交于 2020-01-23 07:22:34

问题


Suppose the structure:

/foo/bar/
        --file1
        --file2
        --file3
        --folder1
          --file4
        --folder2
          --file5

I want to run the unix zip utility, compressing the bar folder and all of it's files and subfolders, from foo folder, but not have the bar folder inside the zip, using only command line.

If I try to use the -j argument, it doesn't create the bar folder inside the zip as I want, but doesn't create folder1 and folder2. Doing -rj doesn't work.

(I know I can enter inside bar and do zip -r bar.zip . I want to know if it's possible to accomplish what $/foo/bar/ zip -r bar.zip . but doing it from $/foo).


回答1:


zip doesn't have a -C (change directory) command like tar does

you can do:

cd folder1 && zip -r ../bar.zip *

from within a command line shell

or you can use bsdtar which is a version of tar from libarchive that can create zips

bsdtar cf bar.zip --format zip -C folder1 .

(this creates a folder called ./ -- not sure a way around that)




回答2:


You have to do cd /foo/bar then zip -r bar.zip ., however, you can group them with parentheses to run in a subshell:

# instead of: cd /foo/bar; zip -r bar.zip; cd -

( cd /foo/bar; zip -r bar.zip . )

The enclosed (paren-grouped) commands are run in a subshell and cd within it won't affect the outer shell session.

See sh manual.

Compound Commands
    A compound command is one of the following:

    (list) list is executed in a subshell environment (see COMMAND EXECUTION ENVIRONMENT below).  
           Variable assignments and builtin commands that affect the shell's environment do not remain in effect after the command completes.  
           The return status is the exit status of list.



回答3:


I can't speak for the OP's reasoning. I was looking for this solution as well.

I am in the middle of coding a program that creates an .ods by building the internal xml files and zipping them together. They must be in the root dir of the archive, or you get an error when you try and run OOo.

I'm sure there is a dozen other ways to do this: create a blank .ods file in OOo named blank.ods, extract to dir named blank, then try running:

cd blank && zip -r ../blank.ods *

The way I wrote mine, the shell closes after one command, so I don't need to navigate back to the original directory, if you do simply add && cd .. to the command line:

cd blank && zip -r ../blank.ods * && cd ..


来源:https://stackoverflow.com/questions/3630167/zip-command-without-including-the-compressed-dir-itself

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!