linux - 扩展文件属性和 bash

标签 linux bash ubuntu attr

我在 Ubuntu bash 中遇到了一个有趣的行为,但我无法完全理解。 如果我将扩展文件属性添加到文件然后更改它 - 属性将从文件中删除。从我的角度来看这是可以的。

user@user:~/tmp$ echo "aaa" > testattr
user@user:~/tmp$ setfattr --name "user.test" --value "Tested" testattr 
user@user:~/tmp$ getfattr --name "user.test" testattr
# file: testattr
user.test="Tested"
user@user:~/tmp$ vi testattr 
< change something in file and save it > 
user@user:~/tmp$ getfattr --name "user.test" testattr
testattr: user.test: No such attribute

但是如果我使用 bash 在文件中写入内容 - 文件属性将保留在原来的位置。 有人可以解释这种行为吗?

user@user:~/tmp$ echo "aaa" > testattr
user@user:~/tmp$ setfattr --name "user.test" --value "Tested" testattr 
user@user:~/tmp$ getfattr --name "user.test" testattr
# file: testattr
user.test="Tested"
user@user:~/tmp$ echo "bbb" > testattr
user@user:~/tmp$ getfattr --name "user.test" testattr
# file: testattr
user.test="Tested"

最佳答案

vi 正在删除已编辑的文件并将其替换为新文件。这就是不保留属性的原因。

这是文件操作的日志。

$ mkdir test
$ touch test/file
$ inotifywait -m -r test
Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.
# vi is now running in another shell
test/ OPEN file
test/ CREATE .file.swp
test/ OPEN .file.swp
test/ CREATE .file.swpx
test/ OPEN .file.swpx
test/ CLOSE_WRITE,CLOSE .file.swpx
test/ DELETE .file.swpx
test/ CLOSE_WRITE,CLOSE .file.swp
test/ DELETE .file.swp
test/ CREATE .file.swp
test/ OPEN .file.swp
test/ MODIFY .file.swp
test/ ATTRIB .file.swp
test/ CLOSE_NOWRITE,CLOSE file
test/ OPEN file
test/ CLOSE_NOWRITE,CLOSE file
test/ MODIFY .file.swp
test/ CREATE 4913
test/ OPEN 4913
test/ ATTRIB 4913
test/ CLOSE_WRITE,CLOSE 4913
test/ DELETE 4913
test/ MOVED_FROM file     # old file moved
test/ MOVED_TO file~
test/ CREATE file         # new file created
test/ OPEN file
test/ MODIFY file
test/ CLOSE_WRITE,CLOSE file
test/ ATTRIB file
test/ ATTRIB file
test/ MODIFY .file.swp
test/ DELETE file~
test/ CLOSE_WRITE,CLOSE .file.swp
test/ DELETE .file.swp

参见this answer禁用该行为。

关于linux - 扩展文件属性和 bash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20609221/

相关文章:

java - BIRT 预览版在 Linux 上的 Eclipse Designer 中崩溃

c++ - 寻找API来衡量流程的电池使用情况

linux - 每 105 分钟运行一次 cron

linux + -/var/spool/clientmqeueue 下的文件

bash - 如何使用 Putty 解压当前目录中的所有 tar 文件

linux - 如何删除一堆文件的第N行

bash - 带正负过滤的多行 grep

linux - sed 因错误而失败

c - 如何在 Mac 中使用 bash 终端使用 c 程序获取修改后的环境变量

linux - svn over HTTP 代理