V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
zhwguest
V2EX  ›  程序员

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

  •  
  •   zhwguest · 29 天前 · 1277 次点击

    不考虑 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 条回复    2024-08-22 09:53:08 +08:00
    jayeli
        1
    jayeli  
       29 天前
    bsdtar --strip-components=1
    sagaxu
        2
    sagaxu  
       29 天前
    假设 zip 支持这个特性,
    zip --strip-components=1 -r top.zip top/ 跟 ( cd top && zip -r ../top.zip ./ ) 有区别?
    amrnxcdt
        3
    amrnxcdt  
       29 天前   ❤️ 1
    @sagaxu #2 大概是在批量处理的脚本中可以不切换路径、减少路径到处飞的情况。针对这个情况可以用子进程$(cd && zip)
    yukafans
        4
    yukafans  
       29 天前 via Android
    popd
    ltux
        5
    ltux  
       28 天前   ❤️ 1
    zip 不支持你说的特性。
    proxytoworld
        6
    proxytoworld  
       28 天前
    pushd
    cd
    popd
    zhwguest
        7
    zhwguest  
    OP
       28 天前
    谢谢大家的回复,感谢 @ltux 确认了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2169 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 16:13 · PVG 00:13 · LAX 09:13 · JFK 12:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.