使用扩展文件属性对目录中的文件进行排序
Sort files in directory using extended file attributes
我在一个目录下有很多文件,我给它们添加了扩展文件属性
setfattr -n user.processorder -v 2 myfile.txt
我想知道,是否可以使用 FOR 循环遍历目录中的文件,但根据扩展文件属性对它们进行排序"processorder"?
这是在 XFS 文件系统上执行此操作的方法
添加属性
attr -s some.attr -V 1 file3
attr -s some.attr -V 2 file1
attr -s some.attr -V 3 file2
阅读并整理它们
ls -1 file* | xargs -I '{}' bash -c "attr -g some.attr '{}' | tr '\n' ' ' ; echo" | sort -n -t ':' -k2,2
Attribute "some.attr" had a 1 byte value for file3: 1
Attribute "some.attr" had a 1 byte value for file1: 2
Attribute "some.attr" had a 1 byte value for file2: 3
您没有指定 OS,但在 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
我在一个目录下有很多文件,我给它们添加了扩展文件属性
setfattr -n user.processorder -v 2 myfile.txt
我想知道,是否可以使用 FOR 循环遍历目录中的文件,但根据扩展文件属性对它们进行排序"processorder"?
这是在 XFS 文件系统上执行此操作的方法
添加属性
attr -s some.attr -V 1 file3
attr -s some.attr -V 2 file1
attr -s some.attr -V 3 file2
阅读并整理它们
ls -1 file* | xargs -I '{}' bash -c "attr -g some.attr '{}' | tr '\n' ' ' ; echo" | sort -n -t ':' -k2,2
Attribute "some.attr" had a 1 byte value for file3: 1
Attribute "some.attr" had a 1 byte value for file1: 2
Attribute "some.attr" had a 1 byte value for file2: 3
您没有指定 OS,但在 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