xattr 命令简介
xattr – display and manipulate extended attributes
xattr 命令用于展示和修改扩展属性;
个人认为 xattr 可以看做是 extend 和 attributes 的缩写。
什么是扩展属性?
扩展属性是与文件一起存储的任意元数据,但与文件系统属性(如修改时间或文件大小)是分开的。
元数据通常是以null结尾的UTF-8字符串,但也可以是任意二进制数据。
有哪些扩展属性,都代表什么意思?
常见用法
$ xattr -h
usage: xattr [-l] [-r] [-s] [-v] [-x] file [file ...] # 列举给定文件的所有属性名
xattr -p [-l] [-r] [-s] [-v] [-x] attr_name file [file ...] # 查看属性对应的值
xattr -w [-r] [-s] [-x] attr_name attr_value file [file ...] # 设置属性键对应的值(字符串类型)
xattr -d [-r] [-s] attr_name file [file ...] # 删除一个属性
xattr -c [-r] [-s] file [file ...] # 删除所有属性
options:
-h: print this help
-l: print long format (attr_name: attr_value and hex output has offsets and ascii representation)
-r: act recursively # 以递归方式
-s: act on the symbolic link itself rather than what the link points to
-v: also print filename (automatic with -r and with multiple files)
-x: attr_value is represented as a hex string for input and output
使用示例
$ xattr ~/Desktop/001.md
com.apple.lastuseddate#PS
com.apple.metadata:kMDLabel_xw4mzzsi36dxyjl4ldltvf7xeq
右键查看文件属性(或 command + i),添加标签和注释,再次查看拓展信息
会发现多了两行属性:
$ xattr ~/Desktop/001.md
com.apple.lastuseddate#PS
com.apple.metadata:_kMDItemUserTags # 标签
com.apple.metadata:kMDItemFinderComment # 注释
com.apple.metadata:kMDLabel_xw4mzzsi36dxyjl4ldltvf7xeq
查看文件夹下所有文件的扩展属性
在 ls 命令的操作后 添加 @ 即可。
$ ls -l@
total 0
drwxr-xr-x 12 shushu staff 384 1 23 20:12 001
drwxr-xr-x@ 5 shushu staff 160 1 23 18:42 001.xcodeproj
com.apple.lastuseddate#PS 16
查看属性值
$ xattr -p com.apple.lastuseddate#PS ~/Desktop/001.md
6D FC 0B 60 00 00 00 00 1B 4B 57 2F 00 00 00 00
$ xattr -p com.apple.metadata:_kMDItemUserTags ~/Desktop/001.md
62 70 6C 69 73 74 30 30 A1 01 66 6D 4B 8B D5 00
30 00 31 00 32 00 33 08 0A 00 00 00 00 00 00 01
01 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 17
添加/修改属性值
$ xattr -w com.apple.TextEncoding utf-8 ~/Desktop/001.md
$ xattr ~/Desktop/001.md
com.apple.TextEncoding
com.apple.lastuseddate#PS
com.apple.metadata:_kMDItemUserTags
删除属性值
# 删除文件的某个属性
$ xattr -d com.apple.quarantine filename
# 递归删除文件夹下所有文件的某个属性
$ xattr -dr com.apple.quarantine diretoryName
关于 com.apple.quarantine
com.apple.quarantine—Tagging files downloaded from the Internet as possibly untrustworthy, storing the application used to download them, among other things.
当打开没有签名的 Mac 应用时,可能会报 “App can’t be opened because it is from an unidentified developer” 的错误。这种安全机制叫做 GateKeeper。
删除这个属性,就可以去除app 的隔离性,实现打开软件。
$ xattr -d com.apple.quarantine targetapp
关于权限后的 @
如果使用上图中的方法(查看文件信息)来修改部分属性,使用 ls -al 查看文件属性时,会发现 权限后面会带上 @ 符号,如下所示:
$ ls -al
total 133472
drwxr-xr-x@ 5 ss staff 160 1 23 20:03 001
-rw-r--r--@ 1 ss staff 4767 1 23 19:08 001.md
-rw------- 1 ss staff 6152665 5 10 2020代码.zip
...
删除 com.apple.metadata:kMDItemFinderComment
属性即可去除 @
$ xattr -d com.apple.metadata:kMDItemFinderComment targetfile
Manual
XATTR(1) BSD General Commands Manual XATTR(1)
NAME
xattr -- display and manipulate extended attributes
SYNOPSIS
xattr [-lrsvx] file ...
xattr -p [-lrsvx] attr_name file ...
xattr -w [-rsx] attr_name attr_value file ...
xattr -d [-rsv] attr_name file ...
xattr -c [-rsv] file ...
xattr -h | --help
DESCRIPTION
The xattr command can be used to display, modify or remove the extended attributes of one or more files, including directories and symbolic links. Extended attributes are arbitrary metadata stored with a file, but separate from the filesystem attributes (such as modification time or file size). The metadata is often a null-terminated UTF-8 string, but can also be arbitrary binary data.
One or more files may be specified on the command line. For the first two forms of the command, when there are more than one file, the file name is displayed along with the actual results. When only one file is specified, the display of the file name is usually suppressed (unless the -v option described below, is also specified).
In the first form of the command (without any other mode option specified), the names of all extended attributes are listed. Attribute names can also be displayed using ``ls -l@''.
In the second form, using the -p option (``print''), the value associated with the given attribute name is displayed. Attribute values are usually displayed as strings. However, if nils are detected in the data, the value is displayed in a hexadecimal representation.
The third form, with the -w option (``write''), causes the given attribute name to be assigned the given value.
The fourth form, with the -d option (``delete''), causes the given attribute name (and associated value), to be removed.
In the fifth form, with the -c option (``clear''), causes all attributes (including their associated values), to be removed.
Finally, the last form, with either the -h or ----hheellpp option, displays a short help message and exits immediately.
OPTIONS
-l By default, the first two command forms either displays just the attribute names or values, respectively. The --ll option causes both the attribute names and corresponding values to be displayed. For hexadecimal display of values, the output is preceeded with the hexa-decimal offset values and followed by ASCII display, enclosed by ``|''.
-r If a file argument is a directory, act as if the entire contents of the directory recursively were also specified (so that every file in the directory tree is acted upon).
-s If a file argument is a symbolic link, act on the symbolic link itself, rather than the file that the symbolic link points at.
-v Force the file name to be displayed, even for a single file.
-x Force the attribute value to be displayed in the hexadecimal repre-sentation.
The -w option normally assumes the input attribute value is a string. Specifying the -x option causes xattr to expect the input in hexadecimal (whitespace is ignored). The xxd(1) command can be used to create hexadecimal representations from exising binary data, to pass to xattr.
EXIT STATUS
The xattr command exits with zero status on success. On error, non-zero is returned, and an error message is printed to the standard error. For system call errors, both the error code and error string are printed (see getxattr(2), listxattr(2), removexattr(2) and setxattr(2) for a complete list of possible error codes).
Some attribute data may have a fixed length that is enforced by the system. For example,
% xattr -w com.apple.FinderInfo 0 foo
xattr: [Errno 34] Result too large: 'foo'
The com.apple.FinderInfo attribute must be 32 bytes in length.
EXAMPLES
This example copies the com.apple.FinderInfo attribute from the /usr directory to the MyDir directory:
% xattr -px com.apple.FinderInfo /usr
00 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
% xattr -l MyDir
% xattr -wx com.apple.FinderInfo \
"`xattr -px com.apple.FinderInfo /usr`" MyDir
% xattr -l MyDir
com.apple.FinderInfo:
00000000 00 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 |........@.......|
00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000020
SEE ALSO
ls(1), xxd(1), getxattr(2), listxattr(2), removexattr(2), setxattr(2)
BSD Nov 29, 2010 BSD
其他参考
-
File System Programming Guide
https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide -
How to View & Remove Extended Attributes from a File on Mac OS
https://osxdaily.com/2018/05/03/view-remove-extended-attributes-file-mac/ -
SO: How do I remove the “extended attributes” on a file in Mac OS X?
https://stackoverflow.com/questions/4833052/how-do-i-remove-the-extended-attributes-on-a-file-in-mac-os-x