unix - 不包含压缩目录本身的 Zip 命令

标签 unix zip cmd

假设结构:

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

我想运行 unix zip实用程序,压缩 bar文件夹及其所有文件和子文件夹,来自 foo文件夹,但没有 bar zip 中的文件夹,仅使用命令行。

如果我尝试使用 -j参数,它不会按照我的意愿在 zip 中创建 bar 文件夹,但不会创建 folder1folder2 .做 -rj不起作用。

(我知道我可以进入 bar 并执行 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 . )

封闭的(paren-grouped)命令在子 shell 中运行,其中的 cd 不会影响 shell 程序 session 。

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.

关于unix - 不包含压缩目录本身的 Zip 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3630167/

相关文章:

java - 找不到类的 Hadoop jar 执行失败

powershell - 通过 Powershell 从 zip 中提取某个文件似乎不在子文件夹中查找

c# - 从 Log4Net 日志文件创建 zip 会导致 System.IO.IOException

python - 如何禁用控制字符的 shell 拦截?

java - 为什么有些命令在 Unix 上需要锁而其他命令则不需要?

c# - UWP 上的“无法加载文件或程序集 System.IO.Compression”

windows - 将一个文件夹中的所有文件及其所有子文件夹移动到一个大文件夹中 - windows xp

windows - 批处理脚本 - 如果尚未设置/定义,则设置/定义环境变量

cmd - 在命令提示符窗口中突出显示文本是否会停止脚本执行或只是阻止输出?

unix - sed - 如何覆盖可选空间?