202

In the clean section of my Makefile I am trying to check if the file exists before deleting permanently. I use this code but I receive errors.

What's wrong with it?

 if [ -a myApp ]
 then
     rm myApp
 fi

I get this error message

 if [ -a myApp ]
 /bin/sh: Syntax error: end of file unexpected (expecting "then")
 make: *** [clean] Error 2
5
  • Is myApp a variable or an actual filename?
    – BugFinder
    Commented Apr 5, 2011 at 14:21
  • myApp is for myApplication i.e. the filename by the build process. Commented Apr 5, 2011 at 15:31
  • 7
    If you just want to avoid make stopping if the file does not exist, rm -rf myApp could be an alternative. Or preceding the command with a dash (-rm myApp) to make make ignore the error from rm (it will however print an ugly message).
    – thomasa88
    Commented Jan 6, 2014 at 9:10
  • 4
    Your problem was that make treats each line in a rule as a separate command and sends them individually to the shell. It's like running just `if [ -a myApp ]' on its own. If you get this error you either need a solution which joins the lines into one (using ) or which ends up with each line independent of the other. There are now several of these below.
    – Michael
    Commented Jan 26, 2019 at 11:45
  • Where can I learn about the options like "-a". I couldn't find it in gnu.org/software/make/manual/make.html ?
    – zk9099
    Commented Jan 7, 2023 at 15:28

16 Answers 16

262

It's strange to see so many people using shell scripting for this. I was looking for a way to use native makefile syntax, because I'm writing this outside of any target. You can use the wildcard function to check if file exists:

 ifeq ($(UNAME),Darwin)
     SHELL := /opt/local/bin/bash
     OS_X  := true
 else ifneq (,$(wildcard /etc/redhat-release))
     OS_RHEL := true
 else
     OS_DEB  := true
     SHELL := /bin/bash
 endif 

See also:

Update:

I found a way which I really like:

ifneq ("$(wildcard $(PATH_TO_FILE))","")
    FILE_EXISTS = 1
else
    FILE_EXISTS = 0
endif
12
  • 4
    tried this, but I just keep getting Makefile:133: *** unterminated call to function `wildcard': missing `)'. Stop.
    – Ant6n
    Commented Dec 8, 2014 at 5:29
  • 1
    Great use of wildcard, so it can be done with makefile itself. Neat :)
    – anoop
    Commented Aug 3, 2017 at 21:48
  • 4
    Helps to also understand what $(wildcard pattern) actually does. See link.
    – Dr. Dan
    Commented Nov 5, 2018 at 22:43
  • 2
    More concise: FILE_EXISTS := $(or $(and $(wildcard $(PATH_TO_FILE)),1),0) Commented Nov 16, 2018 at 14:57
  • 5
    It's worth noting if you're running on Windows under cygwin, using wildcard in this manner does a case sensitive match on the filename, so if the file exists but with different case than the path it won't be found. There doesn't seem to be any way to override this behaviour within the makefile. Commented Nov 27, 2018 at 10:26
117

The second top answer mentions ifeq, however, it fails to mention that this ifeq must be at the same indentation level in the makefile as the name of the target, e.g., to download a file only if it doesn't currently exist, the following code could be used:

download:
ifeq (,$(wildcard ./glob.c))
    curl … -o glob.c
endif

# THIS DOES NOT WORK!
download:
    ifeq (,$(wildcard ./glob.c))
        curl … -o glob.c
    endif
3
  • did not work until I added backslash `` after if fi
    – taras
    Commented Dec 29, 2018 at 10:01
  • 8
    This answer will be a bit weird; the file check happens when the makefile is processed but the action will happen later when the target is built by make. If you delete the file in the meantime then the file won't be created. I have put in an edit to make it clearer.
    – Michael
    Commented Jan 26, 2019 at 10:58
  • Thanks much! This point was not clear from reading the manual. Commented May 9, 2019 at 5:22
84

The problem is when you split your command over multiple lines. So, you can either use the \ at the end of lines for continuation as above or you can get everything on one line with the && operator in bash.

Then you can use a test command to test if the file does exist, e.g.:

test -f myApp && echo File does exist

-f file True if file exists and is a regular file.

-s file True if file exists and has a size greater than zero.

or does not:

test -f myApp || echo File does not exist
test ! -f myApp && echo File does not exist

The test is equivalent to [ command.

[ -f myApp ] && rm myApp   # remove myApp if it exists

and it would work as in your original example.

See: help [ or help test for further syntax.

10
  • 5
    I would have upvoted, but you did not warn that -s is a special case for exists and has a size greater than zero. The question as written is size-agnostic, so existence should be checked using test -e for a file or -d for a directory. Empty files can be especially useful as (for want of a better term) indicators/sentinels, which might be quite relevant for make. Commented Sep 14, 2015 at 22:07
  • Thanks for the suggestion. Changed -f by default, as it's more common to use.
    – kenorb
    Commented Sep 14, 2015 at 22:59
  • 1
    How can I get test on Windows?
    – thowa
    Commented Feb 4, 2016 at 8:49
  • 4
    In order for this to work you need to add || true at the end so the command return true when file don't exists.
    – jcubic
    Commented Jan 21, 2018 at 14:17
  • 2
    @AndrewMackenzie test -f myApp || CMD, notice the ||, so if -f will fail - does not exist (||), then run the command. Does it make sense?
    – kenorb
    Commented Jan 27, 2019 at 18:58
55

It may need a backslash on the end of the line for continuation (although perhaps that depends on the version of make):

if [ -a myApp ] ; \
then \
     rm myApp ; \
fi;
       
6
  • 3
    seems to be not a Makefile syntax? sunsite.ualberta.ca/Documentation/Gnu/make-3.79/html_chapter/…
    – holms
    Commented Dec 13, 2013 at 12:39
  • @holms its a bash syntax. By escaping the new lines it allows it to be handled as a single bash command. By default in make a new line would be a new bash command. The major caveat of this, other than the annoyance of having lots of \ at the end of lines is that every command must be terminated with the ; which would otherwise be implicit in bash.
    – flungo
    Commented Dec 10, 2014 at 8:51
  • 1
    The right answer is by @holms. Neither '\' nor wildcard is exactly intended for this purpose. The reason why you would use wildcard is for code clarity. Not only is this method less readable, it's more prone to syntax errors.
    – Maitreya
    Commented Apr 25, 2015 at 0:15
  • 1
    the link @holms provided does not work anymore, use gnu.org/software/make/manual/make.html#Conditionals instead
    – fero
    Commented May 5, 2016 at 9:19
  • this is a great answer because it matches what is wrong with what the original questioner did and it will work depending on whether the file exists at the time the target is build rather than at the time the Makefile is started which is what most people would expect and want most of the time. In a few weird cases the answer from @cnst would be better.
    – Michael
    Commented Jan 26, 2019 at 11:05
38

Or just put it on one line, as make likes it:

if [ -a myApp ]; then rm myApp; fi;
3
  • that's a great answer in this case (and should get upvotes) but won't work so well if the actions were more complex, in which case just switch to using \
    – Michael
    Commented Jan 26, 2019 at 11:15
  • [ -a myApp ] && rm myApp
    – Robin Hsu
    Commented Mar 12, 2019 at 2:32
  • 4
    ERROR /bin/sh: 1: [: -a: unexpected operator Commented Mar 21, 2021 at 22:07
27

One line solution:

   [ -f ./myfile ] && echo exists

One line solution with error action:

   [ -f ./myfile ] && echo exists || echo not exists

Example used in my make clean directives:

clean:
    @[ -f ./myfile ] && rm myfile || true

And make clean works without error messages!

4
  • 3
    just do @rm -f myfile. Because of the "-f" flag, "rm" will exit with 0 regardless of whether the file exists or not. Commented Nov 15, 2016 at 14:21
  • Or, -rm myfile the lead dash telling make to ignore any error. Commented Apr 4, 2019 at 19:28
  • 1
    In my case, leaving off the || <error action> caused problems. Your final example, where you returned true if the file did not exist, addressed this nicely. Thank you!
    – Flic
    Commented Apr 10, 2019 at 1:34
  • For me path to myfile needs to be with apostrophe('): @[ -f 'myfile' ] && rm myfile
    – Sten
    Commented Oct 14, 2019 at 16:06
18
FILE1 = /usr/bin/perl
FILE2 = /nofile

ifeq ($(shell test -e $(FILE1) && echo -n yes),yes)
    RESULT1=$(FILE1) exists.
else
    RESULT1=$(FILE1) does not exist.
endif

ifeq ($(shell test -e $(FILE2) && echo -n yes),yes)
    RESULT2=$(FILE2) exists.
else
    RESULT2=$(FILE2) does not exist.
endif

all:
    @echo $(RESULT1)
    @echo $(RESULT2)

execution results:

bash> make
/usr/bin/perl exists.
/nofile does not exist.
5
  • This helped me, I think some missed that the OP was asking about makefile. I didn't understand why "&& echo -n yes" is necessary. Explanation: if test -e returns 1 (not found) then shell won't execute the echo command, and therefore won't match the yes in ifeq
    – Brad Dre
    Commented Mar 11, 2019 at 23:29
  • I think you understand it correctly in your explanation statement.
    – Robin Hsu
    Commented Mar 12, 2019 at 2:30
  • @BradDre -- The echo -n yes changes the success of test into the string yes without NL. The ifeq can then compare it with the hard coded yes. All because ifeq wants a string to compare to, not a success status from a shell command. Commented Apr 4, 2019 at 19:27
  • I had to remove the -n part to get it to work...
    – Rick
    Commented Sep 14, 2022 at 4:37
  • This should be the accepted answer since it actually is Makefile syntax with shell test -e
    – tantrix
    Commented Apr 19 at 4:32
14

Missing a semicolon

if [ -a myApp ];
then
  rm myApp
fi

However, I assume you are checking for existence before deletion to prevent an error message. If so, you can just use rm -f myApp which "forces" delete, i.e. doesn't error out if the file didn't exist.

2
  • 1
    ThHat's exactly what I wanted to do. Thanks a lot. Commented Apr 5, 2011 at 15:31
  • 1
    this won't work in a Makefile because the if is still spread over multiple lines - you need to either put this on one line or use \es. and even if you added the \ es you are still missing some semi-colons.
    – Michael
    Commented Jan 26, 2019 at 11:16
5
ifneq ("$(wildcard $(PATH_TO_FILE))","")
    FILE_EXISTS = 1
else
    FILE_EXISTS = 0
endif

This solution posted above works best. But make sure that you do not stringify the PATH_TO_FILE assignment E.g.,

PATH_TO_FILE = "/usr/local/lib/libhl++.a" # WILL NOT WORK

It must be

PATH_TO_FILE = /usr/local/lib/libhl++.a
2

Use test command to check if the file exists or not and then use rm to delete it.\

Syntax for the file command is -

test -f FILENAME && echo exists || echo not exists

Syntax for deleting the file is -

rm -rf FILENAME

So now we need a command to delete the file only if it exists so we will only use OR || with the test command

test -f FILENAME || rm -rf FILENAME

use can use multiple commands by using and && within the parenthesis ()

test -f FILENAME || (rm -rf FILENAME && echo "file deleted as it exists")
1
  • This does not work in a makefile. It ill always run the rm -rf FILENAME regardless of the first test result Commented Dec 7, 2021 at 15:36
1

The answers like the one from @mark-wilkins using \ to continue lines and ; to terminate them in the shell or like the ones from @kenorb changing this to one line are good and will fix this problem.

there's a simpler answer to the original problem (as @alexey-polonsky pointed out). Use the -f flag to rm so that it won't trigger an error

rm -f myApp

this is simpler, faster and more reliable. Just be careful not to end up with a slash and an empty variable

rm -f /$(myAppPath) #NEVER DO THIS

you might end up deleting your system.

3
  • 1
    This is a good answer for deleting the file that the OP had, but I'm pretty sure most people who find this question aren't actually looking for deleting any files; this is actually evidenced by the fact that most answers don't even mention rm at all; BTW, I'm pretty sure that rm -f /$(myAppPath) won't do any damage, either, because / is a directory, and the -r is missing.
    – cnst
    Commented Jan 26, 2019 at 18:13
  • This is not a simpler solution. Like @cnst said, there may be reasons why the original poster does not simply want to do an rm -f, e.g. they may want to rm a variable.
    – dancow
    Commented Nov 3, 2019 at 1:42
  • 1
    I like this simple answer. if the file exists delete, if it doesn't exist, don't complain!
    – Chan Kim
    Commented Jul 14, 2021 at 8:38
1
test ! -f README.md || echo 'Support OpenSource!' >> README.md

"If README.md does not exist, do nothing (and exit successfully). Otherwise, append text to the end."

If you use && instead of || then you generate an error when the file doesn't exist:

Makefile:42: recipe for target 'dostuff' failed
make: *** [dostuff] Error 1
0

I was trying:

[ -f $(PROGRAM) ] && cp -f $(PROGRAM) $(INSTALLDIR)

And the positive case worked but my ubuntu bash shell calls this TRUE and breaks on the copy:

[ -f  ] && cp -f  /home/user/proto/../bin/
cp: missing destination file operand after '/home/user/proto/../bin/'

After getting this error, I google how to check if a file exists in make, and this is the answer...

0

Slightly different from the question, but in case you have a variable containing a list of files which you want to delete you can do

targets: filename1 filename2

clean_targets:
    @$(foreach file, $(targets), test -f $(file) && rm -v $(file) || echo No $(file);)

The basically you loop over the filenames defined by the targets variable and check with 'test' if the target exists. If yes, delete the file, if not, report it is not there. The last check (reporting it is not there) is necessary because otherwise an error is raised in case there is no target at all

0

I wanted to command above, but reputation :)

You can have multi-line commands in gnu make targets by adding the .ONESHELL: directive:

all-in-one-shell:
    if [ -a MyApp ] ; then
        echo "here"
    fi

.ONESHELL: all-in-one-shell

This eliminates trying to come up with creative one-liners or backslash everything.

1
  • 1
    Beware! You can't limit the effects of .ONESHELL: to a single rule like this answer suggests. From the docs: "If the .ONESHELL special target appears anywhere in the makefile then all recipe lines for each target will be provided to a single invocation of the shell" (emphasis mine).
    – mforbes
    Commented Nov 16, 2023 at 18:04
0

We can also take advantages from GNU Coreutils's test builtin (manual), which means we can check file existence like this:

check_exist:
    test -f file && echo yes || echo no 

or more neatly, like this:

check_exist:
    [ -f file ] && echo yes || echo no 

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.