AndrewE
AndrewE

Reputation: 353

Passing filepaths containing spaces with xargs

I'm trying to use xargs to pass the contents of a variable containing zero or more filepaths separated by newlines to another command and have been having inconsistent success.

My input is the output of this:

newHTK=`grep -Fxv -f $TMPFILE /Users/foo/.htk`

Which generates the aforementioned list of filenames. Here's where things go wrong (or sometimes inexplicably right):

echo "$newHTK" | xargs -L 1 xattr -w com.apple.metadata:kMDItemFinderComment htk

The intention is for is to use each line in $newHTK as a filename argument for xattr. What usually happens is xattr splits the input at the spaces. I think I might need to escape the filenames coming out of the echo command or somehow enclose them in double quotation marks (Any advice on an easy way to do this would be appreciated). But if that's the case why did it work for some of the files?

Upvotes: 1

Views: 240

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 81052

You can use the xargs -I flag (if you have it I don't know what its portability is) to do this.

grep -Fxv -f $TMPFILE /Users/foo/.htk | xargs -I % xattr -w com.apple.metadata:kMDItemFinderComment htk %

Upvotes: 3

Related Questions