开发者

Zip command without including the compressed dir itself

开发者 https://www.devze.com 2023-01-14 11:44 出处:网络
Suppose the structure: /foo/bar/ --file1 --file2 --file3 --fo开发者_高级运维lder1 --file4 --folder2 --file5

Suppose the structure:

/foo/bar/
        --file1
        --file2
        --file3
        --fo开发者_高级运维lder1
          --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).


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.


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)


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 ..
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号