• 请不要在回答技术问题时复制粘贴 AI 生成的内容
lurenjiauser
V2EX  ›  程序员

如果只用 zip 命令,似乎没有办法不进入某个目录而仅压缩该目录下的内容(不希望包括该目录名)

  •  
  •   lurenjiauser · Aug 21, 2024 · 2490 views
    This topic created in 807 days ago, the information mentioned may be changed or developed.

    不考虑 7z 等其他可取代的命令。

    $ tree top
    top
    ├── a.txt
    └── b
        └── b.txt
    

    假设我想将 top 下面的两个文件(a.txt, b/b.txt)和一个文件夹(b/)打包到一个 zip 文件,我只能进入这个 top 目录然后再进行压缩:

    $ cd top
    $ zip -r ../top.zip ./
    

    如果我想在 top 目录之外进行压缩:

    $ zip -r top.zip top/
    

    则怎么都不行,-j参数将所有的目录(比如 b/)都去除了。

    我期望的 zip entry 是:

    a.txt
    b/b.txt
    

    不希望是:

    top/a.txt
    top/b/b.txt
    
    7 replies  •  2024-08-22 09:53:08 +08:00
    jayeli
    jayeli
    Aug 21, 2024
    1
    bsdtar --strip-components=1
    sagaxu
    sagaxu
    Aug 21, 2024
    2
    假设 zip 支持这个特性,
    zip --strip-components=1 -r top.zip top/ 跟 ( cd top && zip -r ../top.zip ./ ) 有区别?
    amrnxcdt
    amrnxcdt
    Aug 21, 2024❤️1
    3
    @sagaxu #2 大概是在批量处理的脚本中可以不切换路径、减少路径到处飞的情况。针对这个情况可以用子进程$(cd && zip)
    yukafans
    yukafans
    Aug 21, 2024 via Android
    4
    popd
    ltux
    ltux
    Aug 22, 2024❤️1
    5
    zip 不支持你说的特性。
    proxytoworld
    proxytoworld
    Aug 22, 2024
    6
    pushd
    cd
    popd
    lurenjiauser
    lurenjiauser
    OP
    Aug 22, 2024
    7
    谢谢大家的回复,感谢 @ltux 确认了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2186 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 68ms · UTC 03:05 · PVG 11:05 · LAX 19:05 · JFK 22:05
    ♥ Do have faith in what you're doing.