Goal: I'm trying to create a PHONY target inside my Makefile so that when I run the command "make backup",
It will move all the files that end in "~" into the specified backup folder.

Here is my code currently, and I'll explain the problem after:

.PHONY: backup
backup:
    @mkdir -p ./backup/include    #make folder (don't complain if it already exists)
    @mkdir -p ./backup/src        #make folder (don't complain if it already exists)
    @mv -fu *.h~ ./backup/include #move header file backups into desired folder
    @mv -fu *.cpp~ ./backup/src   #move source file backups into desired folder

Problem: This works great, BUT ONLY if a file "*.h~" and "*.cpp~" file already exist in Makefile's current directory. I would like this target to move the files if it can, and just be quiet if there are no files to move..

Error Message: mv: cannot stat `*.h~': No such file or directory

*I'm on linux(ubuntu) and so my shell is "bash"

Appreciate the help, I’ve been stuck on this all night trying all kinds of commands from if statements to the "find" command. Just thought I’d finally ask for some assistance :icon_wink:

Instead of bothering yourself with doing copies of backup files with the tilde mark. I would suggest you just use a subversion control system. I recommend Git. At any time, you can retrieve the entire history of the modifications to any of your source files ("diff" style). That's very easy to use, and git is not centralized, so it does not require a server running it or anything, that's what I do for all my work files, including files that aren't source files, I can keep a triple backup of all my files (and their entire modification history), on three separate hard-drives.

As for build system, I recommend cmake, it is much nicer than those all-too-limited makefiles.

To solve your problem with the makefile, I can't really help you. You should probably not implement this mechanism in a makefile, use a shell script instead, which you can call from your makefile "backup" target.

.PHONY: backup
backup:
    @mkdir -p ./backup/include    #make folder (don't complain if it already exists)
    @mkdir -p ./backup/src        #make folder (don't complain if it already exists)
    @mv -fu *.h~ ./backup/include #move header file backups into desired folder
    @mv -fu *.cpp~ ./backup/src   #move source file backups into desired folder

Problem: This works great, BUT ONLY if a file "*.h~" and "*.cpp~" file already exist in Makefile's current directory. I would like this target to move the files if it can, and just be quiet if there are no files to move..

Error Message: mv: cannot stat `*.h~': No such file or directory

Few solutions:
1. (dirty) Redirect error message (and precede the commands with a dash (which makes make to ignore an error)):

-@mv -fu *.h~ ./backup/include 2>/dev/null

2. (clean) Be explicit:

backup/include/%.h: %.h~
    @mv $^ $@
backup/src/%.cpp: %.cpp~
    @mv $^ $@

PS: I am not sure why do you keep .h and .cpp together in the current directory, yet want them in separate backup directories.
3. (best) Use source control as Mike suggested.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.