Linux xattr 命令详解

简介

Linux 中的 xattr 命令用于管理文件的扩展属性。扩展属性存储标准属性中未包含的附加元数据(如权限、所有者和时间戳)。它们通常由特定的应用程序或文件系统(例如,aclSELinux 标签)使用。

关键概念

  • 扩展属性:与文件相关的元数据,以键值对的形式存储

  • 属性的通用命名空间:

    • user:通用,普通用户可访问

    • security:用于安全框架,如 SELinux

    • system:用于系统级别的元数据

    • trusted:需要 root 访问权限的元数据

用法示例

列出文件的所有扩展属性

shell 复制代码
xattr example.txt

# 输出如:user.comment

查看扩展属性的值

shell 复制代码
xattr -p [attribute_name] [file]

xattr -p user.comment example.txt

# 输出如:This is a sample comment.

设置或更新扩展属性

shell 复制代码
xattr -w [attribute_name] [value] [file]

xattr -w user.comment "This is a test comment" example.txt

移除指定的扩展属性

shell 复制代码
xattr -d [attribute_name] [file]

xattr -d user.comment example.txt

列出文件的所有扩展属性的键和值

shell 复制代码
xattr -l [file]

xattr -l example.txt

复制一个文件的扩展属性到另一个文件

shell 复制代码
xattr --copy-source=[source_file] [destination_file]

xattr --copy-source=example.txt copy.txt

递归列出目录所有文件的扩展属性

shell 复制代码
xattr -r [directory]

递归删除目录所有文件的扩展属性

shell 复制代码
xattr -cr [directory]

常见问题

  • ext4XFSBtrfs 文件系统支持扩展属性,FAT32 文件系统不支持。

  • 如果扩展属性不工作,使用下列命令启用:

shell 复制代码
sudo mount -o remount,user_xattr /mount/point
相关推荐
Sheffield10 小时前
Docker的跨主机服务与其对应的优缺点
linux·网络协议·docker
Sheffield18 小时前
Alpine是什么,为什么是Docker首选?
linux·docker·容器
Johny_Zhao2 天前
centos7安装部署openclaw
linux·人工智能·信息安全·云计算·yum源·系统运维·openclaw
haibindev2 天前
在 Windows+WSL2 上部署 OpenClaw AI员工的实践与踩坑
linux·wsl2·openclaw
0xDevNull3 天前
Linux切换JDK版本详细教程
linux
进击的丸子3 天前
虹软人脸服务器版SDK(Linux/ARM Pro)多线程调用及性能优化
linux·数据库·后端
Johny_Zhao4 天前
OpenClaw安装部署教程
linux·人工智能·ai·云计算·系统运维·openclaw
chlk1236 天前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑6 天前
Ubuntu系统安装CodeX出现问题
linux·后端