This question already has an answer here:

I want to run git stripspace on all my source code files, on each build (or precommit or just manually), to remove extraneous whitespace, and later maybe to format the code in a standard way,

So I want to do something like

git stripspace < project/code.m > project/code.m

so I thought I should be able to use find -exec or xargs but by the looks of find does not like 'git stripspace' (maybe because of the space in between or the < or >) and I don't know how to pass the same file name, same argument, twice in a row with the proper redirections (< >) .

marked as duplicate by Gilles, Eric Renouf, polemon, mdpc, G-Man Aug 29 '16 at 23:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 4
    DO NOT DO THIS. Because of the way the shell interprets the command sequences, it'll empty project/code.m first to prepare the output pipe from the > before it tries to read it. – Shadur Jul 26 '13 at 4:48
  • I see the concept that would be applicable from the duplicate suggested but it doesn't directly address the question here though. I think the chosen answer is very straight-forward and can be a good reference. – Julie Pelletier Aug 29 '16 at 18:15

You're clobbering the file before you read from it. You need to use a temporary file or a utility like sponge from moreutils.

You can't use find -exec or xargs in this case. You would need to pass a shell a single argument containing both the commands and the file name. xargs and find expect the file name marker (which they replace with the file name) to be a standalone argument. You can use a loop instead.

With a temporary file:

find | while read file ; do
    git stripspace < "$file" > tempfile
    mv -f tempfile "$file"

With sponge:

find | while read file ; do
    git stripspace < "$file" | sponge "$file"

You can use Vim in Ex mode:

ex -sc '%!git stripspace' -cx project/code.m
  1. % select all lines

  2. ! run command

  3. x save and close

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