坑人的macos tar 命令 (实际上是bsdtar)换用 gnu tar

周末 看着笔记本上好用的朗文当代高级词典(mac版)和其它两部词典,准备复制到黑苹果台式机上去。考虑到词典内容有太多小文件,普通复制传输太慢,毫无疑问用 tar 打包肯定快而且能保留原始文件的各种信息。命令如下:

bash 复制代码
time tar czf ~/tmp/Diction.tgz ./LDOCE5.dictionary ./ccald.dictionary 

大约3分钟左右结束,sftp 上传台式机。运行 解压命令:

bash 复制代码
$ tar xf ~/Diction.tgz 
tar: Special header too large
tar: Error exit delayed from previous errors.

这什么意思?赶快在笔记本上测试一遍,结果一样。用了这么多年Linux和macos 这是第一次碰到tar命令创建成功 但解压却失败,如果这是备份关键文件,后果无法想象。网上搜索了一下,看到下面的内容:

https://forums.freebsd.org/threads/tar-special-header-too-large-error.73612/

bash 复制代码
tar: Special header too large error | The FreeBSD Forums
Jan 8, 2020 · I'm piping a bzip2 compressed tar file into tar on FreeBSD 11.2-RELEASE and seeing this error. Anyone any idea what this may indicated? I suspect that after this 

嗯 对解决问题毫无用途,不过发现一点,这个应该是 bsdtar出现的问题。

bash 复制代码
$ ls -l `which tar`
lrwxr-xr-x  1 root  wheel  6 Feb 26 13:47 /usr/bin/tar -> bsdtar

去GNU官网 ​​​​​​https://ftp.gnu.org/gnu/tar/ 下载最新版 tar ,命令行编译:

bash 复制代码
$ tar xf ~/Downloads/tar-1.35.tar.xz
$ cd tar-1.35
$ ./configure
$ make

.......
  CC       xattrs.o
  CCLD     tar
Undefined symbols for architecture x86_64:
  "_iconv", referenced from:
      _utf8_convert in utf8.o
  "_iconv_open", referenced from:
      _utf8_convert in utf8.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [tar] Error 1
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2
make  35.08s user 11.97s system 90% cpu 52.118 total

本以为是个简单的库文件问题,结果搜索半天毫无进展,花的时间可以直接传输几遍了,不过这么简单的问题都解决不了,还怎么开发程序?终于下定决心,直接改Makefile,为保险起见,先添加 -v 参数到Link命令参数中:

bash 复制代码
$ mvim src/Makefile
  LDFLAGS = -v
$ make
  CCLD     tar
Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: x86_64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -lto_library /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib -dynamic -arch x86_64 -platform_version macos 12.0.0 13.3 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -o tar -L/usr/local/lib buffer.o checkpoint.o compare.o create.o delete.o exit.o exclist.o extract.o xheader.o incremen.o list.o map.o misc.o names.o sparse.o suffix.o system.o tar.o transform.o unlink.o update.o utf8.o warning.o xattrs.o ../lib/libtar.a ../gnu/libgnu.a -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/14.0.3/lib/darwin/libclang_rt.osx.a

仔细看 没有 iconv库,虽然 Makefile 中有 LIBICONV = -liconv

但链接时却没有库,原因未知,哎 算了,简单点直接在 src/Makefile 中修改LDFLAGS 如下:

bash 复制代码
LDFLAGS = -liconv -v

再次 make 成功!用 gnu tar 打包 ,没有任何问题。那么,用 bsdtar 就没有办法了?还是有的,用man 查看了 bsdtar的用法 可用下述命令解决:

bash 复制代码
time bsdtar -c --no-mac-metadata -z -f ~/tmp/Diction4.tgz ./LDOCE5.dictionary ./ccald.dictionary

没错,加个 --no-mac-metadata 参数即可,当然这会损失部分mac特有的元数据信息。

网上有很多对比 bsdtar 和 gnu tar 的,都认为 bsdtar 在 稀疏文件有优势,不过经历这个创建文件不报错,解压却会报错的特例,还是小心为上,最好备份后验证一下;或者选用 gnu tar 吧好像还没听说过有类似问题。

一个小小的问题,浪费大半天时间,还是写下来 以备参考。

相关推荐
寒水馨19 小时前
macOS下载、安装openclaw-v2026.7.1(附安装包OpenClaw-2026.7.1.dmg)
macos·大模型·github·开源软件·ai助手·openclaw·gpt-5.6
白玉cfc1 天前
【iOS】MRC和ARC
macos·ios·cocoa
飞雪金灵1 天前
mac电脑 Maven下载安装和配置环境变量
macos·maven·maven下载安装·mac电脑环境
雨声不在1 天前
macos 12使用docker
macos·docker·容器
一叶龙洲1 天前
ubuntu26.04 xfce美化成mac
服务器·ubuntu·macos
李小白杂货铺1 天前
Oh My Zsh 简记
macos·macbook·zsh·主题·插件·oh my zsh·omz
fukai77222 天前
macOS防止休眠的菜单栏小工具
macos
web守墓人2 天前
【go语言】gotar:使用go语言复刻tar命令,并加入7z支持,可独立运行在windows、linux、macos上
linux·macos·golang
之歆3 天前
从 Mac 回到 Windows:用 PowerToys + AutoHotkey 找回熟悉手感
windows·macos
星辰即远方3 天前
字符串合法性检验
macos·ios·cocoa