tar 使用管道的困惑

2021-03-04 20:59:55 +08:00
 xltfes

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

1916 次点击
所在节点   Bash
9 条回复
codehz
2021-03-04 21:15:31 +08:00
(就是约定俗称,最初的原因就是这玩意天然不适合做文件名,刚好可以用来表达标准输入输出(
按标准写法的话是直接用-O 表示标准输出,不写文件名就是标准输入(
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
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
2021-03-04 22:38:00 +08:00
codehz
2021-03-05 00:31:28 +08:00
@billlee GNU 的标准也是标准
Usaki
2021-03-05 08:41:39 +08:00
最近用 pigz 和 tar 也遇到这个-了,不加还报错
xltfes
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
2021-03-05 10:36:23 +08:00
@xltfes macOS 上的 man tar 里的 ,应该是 BSD 的 manual 不算 GNU 的
julyclyde
2021-03-07 15:05:24 +08:00
man tar 不介绍是因为这事和 tar 根本没关系
只是把管道当作文件操作而已

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/758589

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX