macOS Command - xattr

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


其他参考

### 回答1: macOS是苹果公司开发的一款操作系统,而Arm-Linux是一款基于Arm架构的Linux操作系统。MacOS Arm-Linux指的是在Mac电脑上运行基于Arm架构的Linux系统。 苹果公司宣布将在不久的将来改变Mac电脑的芯片架构,从之前使用的Intel处理器转变为自家研发的Arm架构处理器。这意味着未来的Mac电脑将可以运行使用Arm架构编译的软件。 在这种背景下,Mac电脑将具备运行Arm-Linux操作系统的能力。Arm-Linux是Linux操作系统的一个分支,被广泛应用于嵌入式设备和移动设备等领域。将Arm-Linux移植到Mac电脑上,用户可以通过在Mac上运行Linux,获得更多的软件和应用选择。 MacOS Arm-Linux的引入将为用户提供更多的自由和灵活性。Arm-Linux操作系统具有良好的兼容性和可定制性,用户可以根据个人需求进行自定义设置,进行系统优化和调试等操作。同时,对于开发者来说,Arm-Linux也提供了更多的开发工具和环境,使他们能够更方便地进行软件开发和调试。 总之,MacOS Arm-Linux的结合将为用户带来更广阔的选择和更多的可能性,使Mac电脑成为一款更加强大和多功能的设备。用户可以享受到更多的软件和应用,同时也能更灵活地进行系统设置和软件开发等操作。这对于个人和开发者来说都是一种积极的变化,有助于提升用户体验和系统的可定制性。 ### 回答2: macOS是苹果公司自主开发的操作系统,主要运行于苹果的个人电脑和服务器上。而arm-linux是一种基于ARM架构的Linux操作系统。 macOS使用的是x86架构,而arm-linux则是基于ARM架构的操作系统。这两种架构在硬件和指令集上存在差异,因此软件在不同架构上运行时需要进行编译和适配。 最近,苹果公司宣布将在将来的Mac电脑上使用自家研发的ARM架构芯片。这一决定的背后有很多原因,其中包括更好的性能、能效和统一化等。换句话说,未来的Mac电脑将会运行基于ARM架构的芯片,并适配macOS操作系统。 对于软件开发者和用户来说,这个转变意味着一些软件可能需要重新编写或适配以在新的硬件和操作系统上运行。macOS上原本运行良好的x86架构软件需要重新编译为ARM架构版本,以确保兼容性和性能。 然而,这个转变也给开发者带来了新的机会。ARM架构在移动设备上已经得到广泛应用,移植到Mac电脑上有望带来更好的性能和能效。此外,将来的Mac电脑和iOS设备将共享更多的软件和生态系统,有助于提升跨设备的一体化体验。 总之,macOS将转向ARM架构的决定对于苹果公司和用户来说都是一个重要的里程碑。它将带来更好的性能、能效和统一化,并为开发者带来新的机遇。也许,未来的Mac电脑将更加强大和多样化。 ### 回答3: macOS是苹果公司的操作系统,它最初是基于PowerPC架构的,后来转向了x86架构。而ARM架构是一种使用更低功耗的处理器架构,被广泛应用于手机、平板电脑和嵌入式系统等领域。 近年来,ARM架构的处理器性能不断提升,开始逐渐应用于个人电脑领域。所以有人开始猜测苹果是否会将ARM架构引入其Mac电脑产品线。 然而,直到2020年底,苹果才正式发布了基于ARM架构的Mac电脑。这代表着苹果公司将逐渐适应ARM架构,并且未来的Mac电脑将会更加高效、节能。 对于开发者和用户来说,macOS转向ARM架构意味着一些程序和应用可能需要重新编译和优化,以适应新架构的要求。同时,用户也将享受到更长续航时间、更流畅的系统操作和更低的功耗。 对于整个行业来说,macOS转向ARM架构也体现了ARM架构的不断演进和扩大应用领域。它有望推动ARM架构在电脑领域的发展,进一步加强ARM与x86架构的竞争。特别是在移动办公、云计算和物联网等领域,ARM架构有望发挥更大的优势。 总之,macOS转向ARM架构是苹果公司顺应时代发展趋势的举措。它将为用户带来更好的系统体验,也为整个行业的发展带来新的机遇和挑战。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI工程仔

请我喝杯伯爵奶茶~!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值