42

I'm making a Makefile that moves an output file (foo.o) to a different directory (baz).

The output file moves as desired to the directory. However since make won't recompile the output file if I type make again, mv gets an error when it tries to move the non-existent empty file to the directory baz.

So this is what I have defined in my rule make all after all compilation:

-test -e "foo.o" || mv -f foo.o ../baz

Unfortunately, I'm still getting errors.

1
  • 1
    Look at what altendky did: -mv foo.o ../baz
    – Malcolm
    Apr 2, 2013 at 18:21

6 Answers 6

107

Errors in Recipes (from TFM)

To ignore errors in a recipe line, write a - at the beginning of the line's text (after the initial tab).

So the target would be something like:

moveit:
    -mv foo.o ../baz
0
62

I notice nobody has actually answered the original question itself yet, specifically how to ignore errors (all the answers are currently concerned with only calling the command if it won't cause an error).

To actually ignore errors, you can simply do:

mv -f foo.o ../baz 2>/dev/null; true

This will redirect stderr output to null, and follow the command with true (which always returns 0, causing make to believe the command succeeded regardless of what actually happened), allowing program flow to continue.

4
  • 3
    This is a good one. Of course someone could also drop the "true" part and use "-" if they wanted to see some output about the missing file. Feb 27, 2014 at 19:00
  • 1
    I think the semicolon can also be replaced with || (or). May 5, 2020 at 9:42
  • yes, if you want to increase your line length slightly :)
    – Riot
    May 6, 2020 at 10:20
  • 1
    @Riot It also reduces the runtime by one execution in the case that mv succeeds. May 14, 2021 at 17:47
10
+@[ -d $(dir $@) ] || mkdir -p $(dir $@)

is what I use to silently create a folder if it does not exist. For your problem something like this should work

-@[ -e "foo.o" ] && mv -f foo.o ../baz
3
  • i still get an ignored error when using this but was the best solution so far. thanks
    – Sam
    Jul 1, 2010 at 17:42
  • yes you will still get the silent ignore error from make, which I dont quite agree with as @ should make it completely silent.
    – Charles
    Jul 1, 2010 at 20:33
  • @ does not affect the output from make of from the command, it only tells make not to print the invokation itself.
    – JesperE
    Jul 2, 2010 at 8:55
2
   -test -e "foo.o" || if [ -f foo.o ]; then mv -f foo.o ../baz; fi;

That should work

1

Something like

test -e "foo.o" && mv -f foo.o ../baz

should work: the operator should be && instead of ||.

You can experiment with this by trying these commands:

test -e testfile && echo "going to move the file"
test -e testfile || echo "going to move the file"
0

I faced the same problem and as I am generating files, they always have different time. Workaround is set the same time to the files: touch -d '1 June 2018 11:02' file. In that case, gzip generates the same output and same md5sum. In my scenario, I don't need the time for the files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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