我有一个运行压力测试的 AppleScript 脚本。部分测试是打开、保存和关闭某些文件。不知何故,这些文件获得了一些禁止保存文件的“扩展属性”。这会导致压力测试失败。
如何删除扩展属性?
使用 xattr
命令。您可以检查扩展属性:
$ xattr s.7z
com.apple.metadata:kMDItemWhereFroms
com.apple.quarantine
并使用 -d
选项删除一个扩展属性:
$ xattr -d com.apple.quarantine s.7z
$ xattr s.7z
com.apple.metadata:kMDItemWhereFroms
您还可以使用 -c
选项删除所有扩展属性:
$ xattr -c s.7z
$ xattr s.7z
xattr -h
将显示命令行选项,xattr has a man page。
删除单个文件的单个属性
请参阅 Bavarious 的回答。
删除单个文件上的所有扩展属性
使用带有 -c
标志的 xattr
来“清除”属性:
xattr -c yourfile.txt
删除许多文件上的所有扩展属性
要递归删除目录中所有文件的扩展属性,请将 -c
“清除”标志与 -r
递归标志结合起来:
xattr -rc /path/to/directory
给 Mac OS X 用户的提示
有空格或特殊字符的长路径?
打开 Terminal.app
并开始输入 xattr -rc
,包含一个尾随空格,然后将文件或文件夹拖到 Terminal.app
窗口,它会自动添加完整路径并正确转义。
-c
在 OS X 10.5 上失败:option -c not recognized
。我们使用其中一个在 Apple 的 PowerPC 上进行测试。看起来 OS X 的低级版本支持 -l
、-p
、< i>-w
和 -d
。
. textClipping
尝试使用:
xattr -rd com.apple.quarantine directoryname
这负责递归地删除所有地方的讨厌属性。
答案(个人档案)
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
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
3. 再次展示键以查看它们已被移除。
xattr -l ~/Desktop/screenshot\ 2019-10-23\ at\ 010212.png
# com.apple.FinderInfo
# com.apple.metadata:kMDItemScreenCaptureGlobalRect
# com.apple.metadata:kMDItemScreenCaptureType
4. 最后,删除特定文件的所有键
xattr -c ~/Desktop/screenshot\ 2019-10-23\ at\ 010212.png
答案(目录中的所有文件)
1. 在选择中使用的展示键。
xattr -r ~/Desktop
2.删除目录中每个文件的特定键
xattr -rd com.apple.FinderInfo ~/Desktop
3.删除目录中每个文件上的所有键
xattr -rc ~/Desktop
警告:一旦你删除了这些,你就不能把它们找回来!故障错误:没有撤消。
错误
我想解决人们遇到的错误。 因为这些错误也让我发疯了... 如果您在 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
因此,在示例之一中 -c
在 xargs 中不起作用是因为在 bash 中您默认为非 python 版本。
与 -c 一起使用
/usr/bin/xattr -c
不适用于 -c
/usr/local/bin/xattr -c
# option -c not recognized
我的 Shell/Terminal 默认为 /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)?
解决方法
修复 option -c not recognized
错误。
卸载您可能拥有的任何 Python xattr: pip3 uninstall xattr 关闭所有终端窗口并退出终端重新打开一个新的终端窗口。重新运行 xattr 命令,它现在应该可以工作了。
或者
如果你想保留 Python xattr 然后使用
/usr/bin/xattr
对于 Terminal
中的任何 Shell
命令
例子:
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
手册页
MAN PAGE for Python xattr VERSION 0.6.4
注意:我找不到当前版本 0.9.6 的 python 帮助页面
谢谢阅读!
另一种递归方法:
# 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
-c
在 OS X 10.5 上失败:option -c not recognized
。我们使用其中一个在 Apple 的 PowerPC 上进行测试。看起来 OS X 的低级版本支持 -l
、-p
、< i>-w
和 -d
。
-c
不适合我 (OS X 10.6.8)。-d
虽然没有抱怨,但也没有任何效果。我必须像这样显式地命名每个扩展属性:xattr -rd <attrname> <folder/file>
(递归删除)。要找出存在的属性(可能是整个目录树中的相同属性),请使用xattr <dir/file>
,如上面的答案。xattr -c
对我来说很好(并成功剥离了所有元数据)。