在Linux中解压macOS制作的tar文件时,会产生“xattr”错误。
Tar Files Created on macOS Display Errors When Extracting on Linux (2024)

原始链接: https://aruljohn.com/blog/macos-created-tar-files-linux-errors/

在macOS上创建`.tar.gz`文件,用于部署到Linux服务器时,用户在解压时经常会遇到警告或错误,这是由于默认的`bsdtar`工具添加了macOS特定的扩展属性造成的。这些属性会创建重复的文件(前缀为`._`),并触发“忽略未知的扩展头关键字”警告。 有三种主要的解决方案: 1. **`--no-xattrs`:** 在创建tar包时包含`--no-xattrs`(例如,`tar -cvzf --no-xattrs pix.tar.gz pix`),以防止添加这些属性。 2. **`--disable-copyfile`:** 类似于`--no-xattrs`,在创建tar包时使用`--disable-copyfile`(例如,`tar -cvzf --disable-copyfile pix.tar.gz pix`)。 3. **安装`gnu-tar`:** 使用Homebrew(`brew install gnu-tar`)替换默认的`bsdtar`为`gnu-tar`。然后,调整你的`~/.bash_profile`,使`gnu-tar`在系统的PATH中具有更高的优先级。 切换到`gnu-tar`提供了一个永久的解决方案,无需每次创建tar包时都记住额外的标志。这些解决方案可确保创建干净的tar包,在Linux系统上可以无错误地解压。

## macOS Tar 文件与 Linux 兼容性 一篇 Hacker News 讨论强调了在 Linux 环境中展开在 macOS 上创建的 `.tar` 文件时出现的问题,特别是“xattr”错误。这些错误源于 macOS 对 `tar` 的处理方式,它优先忠实地归档文件系统对象——包括 Finder 和 Gatekeeper 元数据——而不是严格遵守可移植的 Unix 标准。 用户建议的解决方法包括将标准错误 (`stderr`) 重定向到 `/dev/null`,或在创建 tar 文件时使用 `--no-xattrs` 和 `--no-mac-metadata` 标志。一位前 Apple 工程师解释说,这种行为是故意的,旨在防止在 Mac 之间归档时数据丢失,并模拟 `copyfile(3)` 函数。 虽然有些人觉得这种行为令人沮丧(特别是包含 `.DS_Store` 文件),但另一些人认为这符合 Apple 的“最小惊讶原则”——`tar` 归档文件系统*按原样*,并在需要标准 Unix 归档时提供排除元数据的选项。
相关文章

原文

I use my MacBook as my development machine and Debian / Ubuntu / Red Hat Linux at work and on this website. I occasionally create tarballs, or tar.gz and tgz files on Mac, which are then deployed on Linux servers. When I untar the tar.gz file on the Linux server, it may throw errors or warnings.

How to exclude ._ files in tar.gz created on  macOS

Creating tar files

I create tar.gz files the regular way. For example, if I want to create a tar.gz file of a directory pix containing pictures.

tar -cvzf pix.tar.gz pix

That command will create a tar gzipped file pix.tar.gz.

SCP to Linux server

I will scp this tar.gz to myserver.tld as user and under the /tmp directory

scp pix.tar.gz [email protected]:/tmp/

Now, I will extract this gzipped tar file on the Linux server.

cd /tmp
tar -xzvf pix.tar.gz 

Output (snippet):

...
pix/TODO/._IMG_3110.jpeg
tar: Ignoring unknown extended header keyword 'LIBARCHIVE.xattr.com.apple.quarantine'
tar: Ignoring unknown extended header keyword 'LIBARCHIVE.xattr.com.apple.lastuseddate#PS'
pix/TODO/IMG_3110.jpeg
pix/TODO/._IMG_3113.jpeg
tar: Ignoring unknown extended header keyword 'LIBARCHIVE.xattr.com.apple.quarantine'
tar: Ignoring unknown extended header keyword 'LIBARCHIVE.xattr.com.apple.lastuseddate#PS'
pix/TODO/IMG_3113.jpeg
pix/TODO/._IMG_3115.jpeg
tar: Ignoring unknown extended header keyword 'LIBARCHIVE.xattr.com.apple.quarantine'
tar: Ignoring unknown extended header keyword 'LIBARCHIVE.xattr.com.apple.lastuseddate#PS'
pix/TODO/IMG_3115.jpeg
pix/TODO/._IMG_3114.jpeg
tar: Ignoring unknown extended header keyword 'LIBARCHIVE.xattr.com.apple.quarantine'
tar: Ignoring unknown extended header keyword 'LIBARCHIVE.xattr.com.apple.lastuseddate#PS'
pix/TODO/IMG_3114.jpeg
...

Warnings and errors when extracting tar.gz or tgz files

As we can see, the tar file contained several new files starting with ._. These appear to be duplicates of existing files.

You may or may not see errors and warnings when extracting files that were tarred on macOS.

On one occasion, I saw warnings like this:

tar: Ignoring unknown extended header keyword 'LIBARCHIVE.xattr.com.apple.metadata:kMDItemTextContentLanguage'
tar: Ignoring unknown extended header keyword 'LIBARCHIVE.xattr.com.apple.metadata:kMDItemKeyphraseVersion'
tar: Ignoring unknown extended header keyword 'LIBARCHIVE.xattr.com.apple.metadata:kMDItemKeyphraseLabels'
tar: Ignoring unknown extended header keyword 'LIBARCHIVE.xattr.com.apple.metadata:kMDItemKeyphraseConfidences'
tar: Ignoring unknown extended header keyword 'LIBARCHIVE.xattr.com.apple.lastuseddate#PS'

Another time, I got these warnings:

tar: Ignoring unknown extended header keyword 'LIBARCHIVE.xattr.com.apple.quarantine'
tar: Ignoring unknown extended header keyword 'LIBARCHIVE.xattr.com.apple.lastuseddate#PS'

And then, this:

tar: Ignoring unknown extended header keyword 'LIBARCHIVE.xattr.com.apple.metadata:kMDItemTextContentLanguage'
tar: Ignoring unknown extended header keyword 'LIBARCHIVE.xattr.com.apple.metadata:kMDItemKeyphraseVersion'
tar: Ignoring unknown extended header keyword 'LIBARCHIVE.xattr.com.apple.metadata:kMDItemKeyphraseLabels'
tar: Ignoring unknown extended header keyword 'LIBARCHIVE.xattr.com.apple.metadata:kMDItemKeyphraseConfidences'

For some reason, the default BSD tar program on macOS seems to create and add these new files. There are two ways to get create a clean tar file.

Solution 1: Use --no-xattrs while creating the tar

While creating the tar file, you can add the attribute --no-xattrs. Doing this will prevent the extra files from getting added.

Previously, we did this:

tar -cvzf pix.tar.gz pix

Now, we will do this:

tar -cvzf --no-xattrs pix.tar.gz pix

This is the simplest way.

Now, SCP it to your Linux server and untar / gunzip it. You should not find the extra files or any errors or warnings.

tar -xzvf pix.tar.gz

Solution 2: Use --disable-copyfile while creating the tar

Similar to the previous solution, in this case, we will use the --disable-copyfile option.

tar -cvzf --disable-copyfile pix.tar.gz pix

Now, SCP the pix.tar.gz file to your Linux server and extract it. You should not find the extra files or any errors or warnings.

tar -xzvf pix.tar.gz

Solution 3: Install and use gnu-tar instead of bsdtar

This is more of a permanent solution, in that you don't have to remember to type the extra optional arguments.

On your macOS, run this command to verify that you are using bsdtar. You should get something like this.

$ tar --version
bsdtar 3.5.3 - libarchive 3.5.3 zlib/1.2.12 liblzma/5.4.3 bz2lib/1.0.8 

Check in which directory bsdtar is installed:

$ which tar
/usr/bin/tar

Install gnu-tar

Now, install gnu-tar using Homebrew.

$ brew install gnu-tar

After a few minute, gnu-tar will be installed in your macOS. It will get installed in this location:

/usr/local/opt/gnu-tar/libexec/gnubin/tar

Verify gnu-tar installation

You can verify gnu-tar by running this:

$ /usr/local/opt/gnu-tar/libexec/gnubin/tar --version
tar (GNU tar) 1.35
Copyright (C) 2023 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://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.

Make gnu-tar your default tar

Now, you can make that your default by setting the path to gnu-tar earlier than bsdtar in your ~/.bash_profile file.

Edit your ~/.bash_profile file.

If you are using a Mac with an Intel processor, add this line:

export PATH="/usr/local/opt/gnu-tar/libexec/gnubin:$PATH"

If you are using a Mac with an Apple Silicon M1, M2, M3 or M4 processor, add this line:

export PATH="/opt/homebrew/opt/gnu-tar/libexec/gnubin:$PATH"

Save and quit your editor and restart your Terminal session.

Check that gnu-tar is default tar, and tar again

Test and see if tar --version returns GNU tar instead of BSD tar.

$ tar --version
tar (GNU tar) 1.35
Copyright (C) 2023 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://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.

If you get a similar response, then recreate the tar file the regular way.

tar -cvzf pix.tar.gz pix

SCP it to your Linux server and untar / gunzip it. You should not find the extra files or any errors or warnings.

Conclusion

I hope the steps in this blog post worked for you. This post will be updated whenever there is anything relevant. Feel free to contact me if you have any questions or suggestions.

Related Posts

If you have any questions, please contact me at [email protected]. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Last Updated: December 06, 2024.     This post was originally written on December 05, 2024.

联系我们 contact @ memedata.com