0

When a directory contains some files with non-empty extended attributes, all the attribute values can be shown as follows:

$ getfattr -d *
# file: file1
user.comment="comment1"

# file: file2
user.comment="comment2"

This should work for all the Linux distributions.

Is it possible to list only the files whose user.comment extended attribute has a specific value, for example comment1? If yes, how?

BowPark
  • 103
  • 1
  • 4

2 Answers2

0

One way is to dump the extended attributes to a file and then use awk

getfattr --dump * > xfatrrts.txt

or with a bash function

 ea_query() {
    a=$1; v=$2
    shift 3
    getfattr -n $a $* |
    awk "BEGIN  {
      RS="# file: "
      FS="n"
   }/="$v"/ { print $1 }"
}

and then

cp `ea_query user.important yes *.txt` important_texts/

In zsh

fattr() {
  local val=$(getfattr -e text -n user.$1 --only-values $REPLY 2>/dev/null)
  [[ -n $val && ( -z $2 || $val = $2 ) ]]
}

And then

ls *(e:fattr year 2020:) 

Here you will find a few other solutions https://linux-user.gr/t/ektetamenes-idiothtes-archeiwn/2379 (in greek).

jtsagata
  • 101
  • 1
0

Try issuing getfattr -n user.comment --only-values * | grep -B 1 "comment1"

This will display the user.comment values of all files and filter the output with grep. You can of course use more sophisticated approaches, for example with find -exec

shodanshok
  • 47,711
  • 7
  • 111
  • 180