V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
zhwguest
V2EX  ›  程序员

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

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