您的位置:首页 > 其它

文件系统的扩展属性:xattr命令

2015-01-22 22:25 3279 查看
在 Mac OS X 下的 hfs+ 文件系统上,文件经常会被附加上 Mac 专有的扩展属性。比如
$ ls -l
total 0
-rw-r--r--@ 1 daniel  staff  0 16 jul 19:28 1.txt
-rw-r--r--  1 daniel  staff  0 16 jul 19:28 2.txt


会有 @ 标志。这个属性是用户在 Finder 里对文件进行任意操作时就会附加上的。比如右键查看 Get Info,然后在 spotlight commands 里写上几个字再删掉,此时,该文件就会附带有 @ 属性。如果想手动去除掉文件的 @ 属性,可以使用命令 xattr。例如
$ xattr -l 1.txt
com.apple.metadata:kmditemfindercomment
$ xattr -d com.apple.metadata:kmditemfindercomment 1.txt
$ xattr -l 1.txt
$ ls -al 1.txt


此时就没有特殊属性了。

网上资料:命令xattr详解

usage: xattr [-l] [-r] [-v] [-x] file [file ...]

xattr -p [-l] [-r] [-v] [-x] attr_name file [file ...]

xattr -w [-r] [-v] [-x] attr_name attr_value file [file ...]

xattr -d [-r] [-v] attr_name file [file ...]

The first form lists the names of all xattrs on the given file(s).

The second form (-p) prints the value of the xattr attr_name.

The third form (-w) sets the value of the xattr attr_name to the string attr_value.

The fourth form (-d) deletes the xattr attr_name.

options:

-h: print this help

-r: act recursively

-l: print long format (attr_name: attr_value and hex output has offsets and

ascii representation)

-v: also print filename (automatic with -r and with multiple files)

-x: attr_value is represented as a hex string for input and output

举例:1:给文件加上@的扩展属性

首先查询到需要添加的属性以及属性的值,它们是成对出现的,比如文字编码com.apple.TextEncoding: utf-8;

可以使用xattr -l newfile2来查看。然后就是给文件加上扩展属性,使用命令:xattr -w 属性名 属性值 文件名 。比如给文件加上字符utf-8编码 :xattr -w com.apple.TextEncoding utf-8 newfile3

再查看一下文件,已经改变了。

2:去掉文件的扩展属性

首先要查询到文件已经有的扩展属性,使用xattr -l 文件名 来查看,然后使用命令 xattr -d 属性名 文件名,就可以去掉了,比如去掉文件的文字编码:xattr -d com.apple.TextEncoding newfile3

这个命令的内容很少,可以使用xattr -h命令来查看完整的内容。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: