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

tar 使用管道的困惑

  •  
  •   xltfes · 2021-03-04 20:59:55 +08:00 · 1886 次点击
    这是一个创建于 1120 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这个网页上看到(cd backup && tar c .) | (cd backupArchives && tar xf -)这条命令,对于 tar 直接用管道进行打包解包这个操作直呼少见,man tar中也没看到相关介绍

    • 用-表示标准输入这种操作是哪来的?希望大佬解惑
    • 如果能给出 unix-style 或 gnu-style 的标准写法就更好了\doge
    9 条回复    2021-03-07 15:05:24 +08:00
    codehz
        1
    codehz  
       2021-03-04 21:15:31 +08:00 via Android
    (就是约定俗称,最初的原因就是这玩意天然不适合做文件名,刚好可以用来表达标准输入输出(
    按标准写法的话是直接用-O 表示标准输出,不写文件名就是标准输入(
    polaa
        2
    polaa  
       2021-03-04 21:19:30 +08:00
    man tar 里有
    -f file, --file file
    Read the archive from or write the archive to the specified file. The
    filename can be - for standard input or standard output. The default
    varies by system; on FreeBSD, the default is /dev/sa0; on Linux, the
    default is /dev/st0.
    billlee
        3
    billlee  
       2021-03-04 22:35:37 +08:00
    GNU tar 这里也可以不指定 f 选项,没有 f 时就是读标准输入

    GNU cp 可以用 cp -rT backup backupArchives 来达到同样目的

    但 POSIX 里面 tar 不指定 f 时的输入是不确定的. POSIX cp 也没有 -T 这个选项,所以那个网页才给了个这么绕的命令吧
    billlee
        4
    billlee  
       2021-03-04 22:38:00 +08:00
    codehz
        5
    codehz  
       2021-03-05 00:31:28 +08:00 via Android
    @billlee GNU 的标准也是标准
    Usaki
        6
    Usaki  
       2021-03-05 08:41:39 +08:00 via Android
    最近用 pigz 和 tar 也遇到这个-了,不加还报错
    xltfes
        7
    xltfes  
    OP
       2021-03-05 09:43:22 +08:00
    @polaa 手册居然还有不一样的,用的 tar 不一样么?
    tar --version
    tar (GNU tar) 1.29
    Copyright (C) 2015 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.

    Written by John Gilmore and Jay Fenlason.
    polaa
        8
    polaa  
       2021-03-05 10:36:23 +08:00
    @xltfes macOS 上的 man tar 里的 ,应该是 BSD 的 manual 不算 GNU 的
    julyclyde
        9
    julyclyde  
       2021-03-07 15:05:24 +08:00
    man tar 不介绍是因为这事和 tar 根本没关系
    只是把管道当作文件操作而已
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5340 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 08:21 · PVG 16:21 · LAX 01:21 · JFK 04:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.