Search code examples
bashawkfindxattr

Passing awk results to command after pipe


I'm trying to pass what would be the awk outputs of print $1 and print $2 to setfattr after a pipe. The value of the extended attribute is an MD5 hash which is calculated from input files from the output of a find command. This is what I have so far:

find /path/to/dir -type f \
  -regextype posix-extended \
  -not -iregex '.*\.(jpg|docx|psd|jpeg|png|html|bmp|gif|txt|pdf|mp3|bts|srt)' \
| parallel -j 64 md5sum | awk '{system("setfattr -n user.digest.md5 -v " $1 $2)}'

Having awk '{print $1}' and $2 after the last pipe returns the hash and file path respectively just fine, I'm just not sure how to get those values into setfattr. setfattr just throws a generic usage error when that command is run. Is this just a syntax issue or am I going about this totally wrong?


Solution

  • Try piping the output of the parallel command into a while loop:

    find /path/to/dir -type f \
        -regextype posix-extended \
        -not -iregex '.*\.(jpg|docx|psd|jpeg|png|html|bmp|gif|txt|pdf|mp3|bts|srt)' |
      parallel -j 64 md5sum |
      while read hash file; do
        setfattr -n user.digest.md5 -v ${hash} ${file}
      done