3

I have a number of files in a directory, and I add extended file attributes to them

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

I was wondering, is it possible to iterate over the files in the directory with a FOR loop but sorting them according to the extended file attribute "processorder"?

miduarte
  • 83
  • 1
  • 8
  • Unfortunately, I don't think `for` and the shell has a shortcut for ordering by an arbitrary defined extended attribute. You'll probably need to write a script that calls `getfattr` to do that. – lurker Feb 05 '18 at 17:22

2 Answers2

1

Here's how to do that on an XFS filesystem

Adding attributes

attr -s some.attr -V 1 file3 attr -s some.attr -V 2 file1 attr -s some.attr -V 3 file2

Reading and sorting on them

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

LMC
  • 10,453
  • 2
  • 27
  • 52
  • By doing a little edit on your code i managed to print the filenames sorted by the extended attribute, like this: ls -1 | xargs -I '{}' bash -c "attr -g some.ordemprocessamento '{}' | tr '\n' ' ' ; echo" | sort -n -t ':' -k1,1 | awk '{print $9;}' | while read x; do echo "${x//:}"; done – miduarte Feb 06 '18 at 17:14
0

You don't specify your OS, but it might go like this on 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
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432