简体   繁体   English

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

[英]How do I remove the “extended attributes” on a file in Mac OS X?

I have an AppleScript script that runs a stress test.我有一个运行压力测试的AppleScript脚本。 Part of the test is to open, save, and close certain files.部分测试是打开、保存和关闭某些文件。 Somehow, the files have picked up some "extended attributes" that prohibit the files from being saved.不知何故,这些文件已经获得了一些禁止保存文件的“扩展属性”。 That causes the stress test to fail.这会导致压力测试失败。

How do I remove the extended attributes?如何删除扩展属性?

Use the xattr command.使用xattr命令。 You can inspect the extended attributes:您可以检查扩展属性:

$ xattr s.7z
com.apple.metadata:kMDItemWhereFroms
com.apple.quarantine

and use the -d option to delete one extended attribute:并使用-d选项删除一个扩展属性:

$ xattr -d com.apple.quarantine s.7z
$ xattr s.7z
com.apple.metadata:kMDItemWhereFroms

you can also use the -c option to remove all extended attributes:您还可以使用-c选项删除所有扩展属性:

$ xattr -c s.7z
$ xattr s.7z

xattr -h will show you the command line options, and xattr has a man page . xattr -h将显示命令行选项,并且xattr 有一个手册页

Removing a Single Attribute on a Single File删除单个文件的单个属性

See Bavarious's answer.见巴伐利亚的回答。


To Remove All Extended Attributes On a Single File删除单个文件上的所有扩展属性

Use xattr with the -c flag to "clear" the attributes:使用带有-c标志的xattr来“清除”属性:

xattr -c yourfile.txt



To Remove All Extended Attributes On Many Files删除许多文件的所有扩展属性

To recursively remove extended attributes on all files in a directory, combine the -c "clear" flag with the -r recursive flag:要递归删除目录中所有文件的扩展属性,请将-c "clear" 标志与-r递归标志结合起来:

xattr -rc /path/to/directory



A Tip for Mac OS X Users给 Mac OS X 用户的提示

Have a long path with spaces or special characters?有一个带有空格或特殊字符的长路径?

Open Terminal.app and start typing xattr -rc , include a trailing space, and then then drag the file or folder to the Terminal.app window and it will automatically add the full path with proper escaping.打开Terminal.app并开始输入xattr -rc ,包括一个尾随空格,然后将文件或文件夹拖到Terminal.app窗口,它会自动添加正确转义的完整路径。

Try using:尝试使用:

xattr -rd com.apple.quarantine directoryname

This takes care of recursively removing the pesky attribute everywhere.这负责递归地删除所有地方的讨厌的属性。


Answer (Individual Files)答案(个别文件)


1. Showcase keys to use in selection. 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. Pick a Key to delete. 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. Showcase keys again to see they have been removed. 3. 再次展示钥匙以查看它们已被移除。

xattr -l ~/Desktop/screenshot\ 2019-10-23\ at\ 010212.png
    # com.apple.FinderInfo
    # com.apple.metadata:kMDItemScreenCaptureGlobalRect
    # com.apple.metadata:kMDItemScreenCaptureType

4. Lastly, REMOVE ALL keys for a particular file 4. 最后,删除特定文件的所有键

xattr -c ~/Desktop/screenshot\ 2019-10-23\ at\ 010212.png

Answer (All Files In A Directory)答案(目录中的所有文件)


1. Showcase keys to use in selection. 1. 展示选择中使用的键。

xattr -r ~/Desktop

2. Remove a Specific Key for EVERY FILE in a directory 2.删除目录中每个文件的特定键

xattr -rd com.apple.FinderInfo ~/Desktop

3. Remove ALL keys on EVERY FILE in a directory 3.删除目录中每个文件的所有键

xattr -rc ~/Desktop

WARN: Once you delete these you DON'T get them back!警告:一旦你删除了这些,你就不会再找回来了!
FAULT ERROR: There is NO UNDO.故障错误:没有撤消。


Errors错误


I wanted to address the error's people are getting.我想解决人们遇到的错误。 Because the errors drove me nuts too... On a mac if you install xattr in python, then your environment may have an issue.因为这些错误也让我发疯了......在 Mac 上,如果你在 python 中安装xattr ,那么你的环境可能有问题。

There are two different paths on my mac for xattr我的 mac 上有两条不同的xattr路径

type -a xattr

    # xattr is /usr/local/bin/xattr    # PYTHON Installed Version
    # xattr is /usr/bin/xattr          # Mac OSX Installed Version

So in one of the example's where -c will not work in xargs is because in bash you default to the non-python version.因此,在示例之一中, -c在 xargs 中不起作用是因为在 bash 中您默认使用非 Python 版本。

Works with -c-c

/usr/bin/xattr -c

Does NOT Work with -c不适用于-c

/usr/local/bin/xattr -c
    # option -c not recognized

My Shell/Terminal defaults to /usr/local/bin/xattr because my $PATH /usr/local/bin: is before /usr/bin: which I believe is the default.我的外壳/终端默认为 /usr/local/bin/xattr 因为我的$PATH /usr/local/bin:/usr/bin: ,我认为这是默认值。

I can prove this because, if you try to uninstall the python xattr you will see:我可以证明这一点,因为如果您尝试卸载 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)?

Workarounds解决方法


To Fix option -c not recognized Errors.修复option -c not recognized错误。

  1. Uninstall any Python xattr you may have: pip3 uninstall xattr卸载您可能拥有的任何 Python xattrpip3 uninstall xattr
  2. Close all Terminal windows & quit Terminal关闭所有Terminal窗口并退出Terminal
  3. Reopen a new Terminal window.重新打开一个新的Terminal窗口。
  4. ReRun xattr command and it should now work.重新运行xattr命令,它现在应该可以工作了。

OR

If you want to keep the Python xattr then use如果你想保留 Python xattr然后使用

/usr/bin/xattr

for any Shell commands in Terminal对于Terminal任何Shell命令


Example:示例:


Python's version of xattr doesn't handle images at all: 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 Pages手册页

MAN PAGE for OSX xattr OSX xattr 手册页

MAN PAGE for Python xattr VERSION 0.6.4 Python xattr 版本 0.6.4 的手册页

NOTE: I could not find the python help page for current VERSION 0.9.6注:我找不到蟒蛇帮助页面当前版本0.9.6

Thanks for Reading!感谢阅读!

Another recursive approach:另一种递归方法:

# 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Mac OS X 中的文件上添加“扩展属性”? - How do I add the “extended attributes” on a file in Mac OS X? 如何使用Java在OS X上的扩展文件属性中存储哈希? - How to store a hash in extended file attributes on OS X with Java? 在Mac OS X上,如何将POSIX fd转换为文件*? - On Mac OS X, how do I turn a POSIX fd into a FILE*? 如何在Mac OS X中的.app中选择文件? - How do I select a file within a .app in Mac OS X? 如何更改Mac上文件的扩展文件属性? - how to change extended file attributes of files on mac? Mac OS X Mavericks:如何从Sparsebundle挂载中删除隔离标志? - Mac OS X Mavericks: How do I remove the quarantine flag from my Sparsebundle mount? 如何从 Mac OS X 中完全删除(所有版本的)pdftk 服务器? - How do I completely remove (all versions of) pdftk server from Mac OS X? 在 Java 中创建文件时,如何在 Mac OS X 中提供文件路径? - How do I provide a file path in Mac OS X while creating a file in Java? 如何在Mac OS X 10.6上编译Mac OS X 10.5的python扩展? - How do I compile python extensions for Mac OS X 10.5, on Mac OS X 10.6? 如何在 Mac OS X 10.5 中处理来自 Finder 的多个文件拖放? - How do I handle multiple file drag/drop from Finder in Mac OS X 10.5?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM