假设结构:
/foo/bar/
--file1
--file2
--file3
--folder1
--file4
--folder2
--file5
Run Code Online (Sandbox Code Playgroud)
我想运行unix zip实用程序,从bar文件夹压缩文件夹及其所有文件和子foo文件夹,但bar只使用命令行在zip中没有文件夹.
如果我尝试使用该-j参数,它不会根据需要在zip中创建bar文件夹,但不会创建folder1和folder2.做-rj不起作用.
(我知道我可以进入内部栏并且zip -r bar.zip .我想知道是否有可能完成$/foo/bar/zip -r bar.zip .但是从$/foo执行它).
你要做的cd /foo/bar,然后zip -r bar.zip .,但是,您可以用括号来在子shell中运行它们分组:
# instead of: cd /foo/bar; zip -r bar.zip; cd -
( cd /foo/bar; zip -r bar.zip . )
Run Code Online (Sandbox Code Playgroud)
附带的(paren-grouped)命令在子shell中运行,其中的cd不会影响外壳会话.
见sh手册.
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.
Run Code Online (Sandbox Code Playgroud)
zip没有像tar那样的-C(更改目录)命令
你可以做:
cd folder1 && zip -r ../bar.zip *
Run Code Online (Sandbox Code Playgroud)
从命令行shell中
或者您可以使用bsdtar,它是libarchive中可以创建zip的tar版本
bsdtar cf bar.zip --format zip -C folder1.
(这会创建一个名为./的文件夹 - 不确定方法)