My aim is to get a list of files that have been modified in git, then run rspec command passing each file in as an argument.

Currently I have:

$ git status -s | awk '{if ($1 == "M") print $2}' | tr "\\n" "\\0" | \
    xargs -0 -I % rspec -f documentation %

This technically works, but it runs rspec for each modified file, I want it to run:

$ rspec path/to/file/1 path/to/file/2 ...

Anyone know how I can achieve this?

  • A shorter variant for the second command: awk '/^ M / {print $2}' (see examples) – ruvim Jun 7 '18 at 13:11
... | xargs -0 rspec -f documentation 

Note that this can call rspec multiple times if the command line would be too long. This isn't an issue with rspec since the reason to call it once is performance, but don't use it for something like xargs -0 tar cf archive.tar where any second, third, … run would create an archive overwriting the output of the previous runs.

  • Damn, knew I was doing something wrong.. Thanks – Michael Baldry Nov 18 '14 at 11:54
  • in your tar example you could use -x to force xargs to abort if the line is too long. – Jeff Mar 18 at 6:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.