Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 使用扩展文件属性对目录中的文件进行排序_Bash_Unix_File Attributes - Fatal编程技术网

Bash 使用扩展文件属性对目录中的文件进行排序

Bash 使用扩展文件属性对目录中的文件进行排序,bash,unix,file-attributes,Bash,Unix,File Attributes,我在一个目录中有许多文件,我向它们添加扩展文件属性 setfattr -n user.processorder -v 2 myfile.txt 我想知道,是否可以使用FOR循环对目录中的文件进行迭代,但根据扩展文件属性“processorder”对其进行排序?下面介绍如何在XFS文件系统上执行此操作 添加属性 attr-s some.attr-v1文件3 attr-s some.attr-v2文件1 attr-s some.attr-V 3文件2 阅读和整理它们 ls-1文件*| xargs-

我在一个目录中有许多文件,我向它们添加扩展文件属性

setfattr -n user.processorder -v 2 myfile.txt

我想知道,是否可以使用FOR循环对目录中的文件进行迭代,但根据扩展文件属性“processorder”对其进行排序?

下面介绍如何在XFS文件系统上执行此操作

添加属性

attr-s some.attr-v1文件3
attr-s some.attr-v2文件1
attr-s some.attr-V 3文件2

阅读和整理它们

ls-1文件*| xargs-I'{}'bash-c“attr-g some.attr'{}'|tr'\n'';echo“| sort-n-t':'-k2,2
属性“some.attr”的文件3:1的值为1字节
属性“some.attr”的file1:2的值为1字节

属性“some.attr”的文件2:3有一个1字节的值。您不需要指定操作系统,但在macOS上可能会这样

# write 2 as attribute "v" on file "a"
xattr -w v 2 a
xattr -w v 3 b
xattr -w v 1 c

# read attribute "v" of file "a"
xattr -p v a
2

# sort files by attribute "v"
xattr -p v a b c | sort -t: -k2 -n
c: 1
a: 2
b: 3

不幸的是,我认为
不适用于
,并且shell有一个通过任意定义的扩展属性进行排序的快捷方式。您可能需要编写一个调用
getfattr
的脚本来实现这一点。通过对代码进行一些编辑,我成功地打印了按扩展属性排序的文件名,如下所示:ls-1 | xargs-i'{}bash-c“attr-g some.ordemprocesso'{}tr'\n'';echo“| sort-n-t':'-k1,1 | awk'{print$9;}|读x时;执行回显“${x/:}”;完成