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
相关推荐
轻松Ai享生活10 小时前
5 节课深入学习Linux Cgroups
linux
christine-rr10 小时前
linux常用命令(4)——压缩命令
linux·服务器·redis
三坛海会大神55510 小时前
LVS与Keepalived详解(二)LVS负载均衡实现实操
linux·负载均衡·lvs
東雪蓮☆10 小时前
深入理解 LVS-DR 模式与 Keepalived 高可用集群
linux·运维·服务器·lvs
乌萨奇也要立志学C++11 小时前
【Linux】进程概念(二):进程查看与 fork 初探
linux·运维·服务器
獭.獭.13 小时前
Linux -- 信号【上】
linux·运维·服务器
hashiqimiya13 小时前
centos配置环境变量jdk
linux·运维·centos
hashiqimiya13 小时前
权限更改centos中系统文件无法创建文件夹,使用命令让普通用户具备操作文件夹
linux
逆小舟17 小时前
【Linux】人事档案——用户及组管理
linux·c++
青草地溪水旁17 小时前
pthread_mutex_lock函数深度解析
linux·多线程·pthread