如何在Mac OS X中删除文件中的"扩展属性"?

tam*_*mes 250 macos attributes

我有一个运行压力测试的AppleScript脚本.部分测试是打开,保存和关闭某些文件.不知何故,文件已经选择了一些禁止文件保存的"扩展属性".这导致压力测试失败.

如何删除扩展属性?

小智 377

使用该xattr命令.您可以检查扩展属性:

$ xattr s.7z
com.apple.metadata:kMDItemWhereFroms
com.apple.quarantine
Run Code Online (Sandbox Code Playgroud)

并使用该-d选项删除一个扩展属性:

$ xattr -d com.apple.quarantine s.7z
$ xattr s.7z
com.apple.metadata:kMDItemWhereFroms
Run Code Online (Sandbox Code Playgroud)

您还可以使用该-c选项删除所有扩展属性:

$ xattr -c s.7z
$ xattr s.7z
Run Code Online (Sandbox Code Playgroud)

xattr -h将显示命令行选项,xattr有一个手册页.

  • 在我的10.8.5系统上安装了Xcode,`xattr -c`对我来说很好(并且成功地剥离了所有元数据). (5认同)
  • @helioz -c肯定在OS X 10.8.3上为我工作 (3认同)
  • Apple在线提供了一个手册页:http://developer.apple.com/documentation/Darwin/Reference/ManPages/man1/xattr.1.html (2认同)

cwd*_*cwd 99

删除单个文件上的单个属性

见Bavarious的回答.


删除单个文件上的所有扩展属性

使用xattr-c标志"明确"的属性:

xattr -c yourfile.txt
Run Code Online (Sandbox Code Playgroud)



删除许多文件上的所有扩展属性

要以递归方式删除目录中所有文件的扩展属性,请将-c"clear"标志与-r递归标志结合使用:

xattr -rc /path/to/directory
Run Code Online (Sandbox Code Playgroud)



适用于Mac OS X用户的提示

有空格或特殊字符的长路径?

打开Terminal.app并开始输入xattr -rc,包括尾随空格,然后将文件或文件夹拖到Terminal.app窗口,它将自动添加正确转义的完整路径.

  • 使用Terminal.app进行拖放操作的+1 (6认同)

小智 23

尝试使用:

xattr -rd com.apple.quarantine directoryname
Run Code Online (Sandbox Code Playgroud)

这需要在任何地方递归地删除讨厌的属性.


Jay*_*zzo 16


答案(个别文件)


1. 展示在选择中使用的键。

xattr ~/Desktop/screenshot\ 2019-10-23\ at\ 010212.png
    # com.apple.FinderInfo
    # com.apple.lastuseddate#PS
    # com.apple.metadata:kMDItemIsScreenCapture
    # com.apple.metadata:kMDItemScreenCaptureGlobalRect
    # com.apple.metadata:kMDItemScreenCaptureType
Run Code Online (Sandbox Code Playgroud)

2. 选择要删除的键。

xattr -d com.apple.lastuseddate#PS ~/Desktop/screenshot\ 2019-10-23\ at\ 010212.png
xattr -d kMDItemIsScreenCapture ~/Desktop/screenshot\ 2019-10-23\ at\ 010212.png
Run Code Online (Sandbox Code Playgroud)

3. 再次展示钥匙以查看它们已被移除。

xattr -l ~/Desktop/screenshot\ 2019-10-23\ at\ 010212.png
    # com.apple.FinderInfo
    # com.apple.metadata:kMDItemScreenCaptureGlobalRect
    # com.apple.metadata:kMDItemScreenCaptureType
Run Code Online (Sandbox Code Playgroud)

4. 最后,删除特定文件的所有键

xattr -c ~/Desktop/screenshot\ 2019-10-23\ at\ 010212.png
Run Code Online (Sandbox Code Playgroud)

答案(目录中的所有文件)


1. 展示在选择中使用的键。

xattr -r ~/Desktop
Run Code Online (Sandbox Code Playgroud)

2.删除目录中每个文件的特定键

xattr -rd com.apple.FinderInfo ~/Desktop
Run Code Online (Sandbox Code Playgroud)

3.删除目录中每个文件的所有键

xattr -rc ~/Desktop
Run Code Online (Sandbox Code Playgroud)

警告:一旦你删除了这些,你就不会再找回来了!
故障错误:没有撤消。


错误


我想解决人们遇到的错误。 因为这些错误也让我发疯了...... 在 Mac 上,如果你安装xattr在 python 中,那么你的环境可能有问题。

我的 mac 上有两条不同的路径 xattr

type -a xattr

    # xattr is /usr/local/bin/xattr    # PYTHON Installed Version
    # xattr is /usr/bin/xattr          # Mac OSX Installed Version
Run Code Online (Sandbox Code Playgroud)

因此,在示例之一-c中 xargs 不起作用的地方是因为在 bash 中您默认使用非 Python 版本。

-c

/usr/bin/xattr -c
Run Code Online (Sandbox Code Playgroud)

不适用于 -c

/usr/local/bin/xattr -c
    # option -c not recognized
Run Code Online (Sandbox Code Playgroud)

我的shell /终端默认为/ usr / local / bin目录/ XATTR,因为我$PATH /usr/local/bin:是之前/usr/bin:我认为是默认它。

我可以证明这一点,因为如果您尝试卸载 python,xattr您将看到:

pip3 uninstall xattr
Uninstalling xattr-0.9.6:
  Would remove:
    /usr/local/bin/xattr
    /usr/local/lib/python3.7/site-packages/xattr-0.9.6.dist-info/*
    /usr/local/lib/python3.7/site-packages/xattr/*
Proceed (y/n)?
Run Code Online (Sandbox Code Playgroud)

解决方法


修复option -c not recognized错误。

  1. 卸载xattr您可能拥有的任何 Python :pip3 uninstall xattr
  2. 关闭所有Terminal窗口并退出Terminal
  3. 重新打开一个新Terminal窗口。
  4. 重新运行xattr命令,它现在应该可以工作了。

或者

如果你想保留 Pythonxattr然后使用

/usr/bin/xattr
Run Code Online (Sandbox Code Playgroud)

对于任何Shell命令Terminal


例子:


Python 的版本xattr根本不处理图像:

Good-Mac:~ JayRizzo$ xattr ~/Desktop/screenshot\ 2019-10-23\ at\ 010212.png
    # com.apple.FinderInfo
    # Traceback (most recent call last):
    #   File "/usr/local/bin/xattr", line 8, in <module>
    #     sys.exit(main())
    #   File "/usr/local/lib/python3.7/site-packages/xattr/tool.py", line 196, in main
    #     attr_value = attr_value.decode('utf-8')
    # UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb0 in position 2: invalid start byte

Good-Mac:~ JayRizzo$ /usr/bin/xattr ~/Desktop/screenshot\ 2019-10-23\ at\ 010212.png
    # com.apple.FinderInfo
    # com.apple.lastuseddate#PS
    # com.apple.metadata:kMDItemIsScreenCapture
    # com.apple.metadata:kMDItemScreenCaptureGlobalRect
    # com.apple.metadata:kMDItemScreenCaptureType
Run Code Online (Sandbox Code Playgroud)

手册页

OSX xattr 的手册页

Python xattr 版本 0.6.4 的手册页

注:我无法找到Python的帮助页面当前版本0.9.6

谢谢阅读!


bob*_*bob 7

另一种递归方法:

# change directory to target folder:
cd /Volumes/path/to/folder

# find all things of type "f" (file), 
# then pipe "|" each result as an argument (xargs -0) 
# to the "xattr -c" command:
find . -type f -print0 | xargs -0 xattr -c

# Sometimes you may have to use a star * instead of the dot.
# The dot just means "here" (whereever your cd'd to
find * -type f -print0 | xargs -0 xattr -c
Run Code Online (Sandbox Code Playgroud)