Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In the makefile for my project, I want it to copy a config file only if the file does not already exist in the destination folder. At the moment I am using:

cp -n

However, recently someone has told me that they are getting an "invalid option" error.

My question is: for a makefile, is there a more compatible method to achieve this than cp -n?

(Note: cp -u is not what I want, if the file already exists, it should never be replaced even if it is older than the source file.)

share|improve this question
    
To whomever voted to close: This isn't off topic, it is perfectly valid here. –  Wuffers Jul 11 '11 at 16:06
add comment

2 Answers

up vote 2 down vote accepted

How about:

#!/bin/bash
if ! [ -e /path/to/foo ]
then
    cp foo /path/to/
fi

Even better, if this is e.g. a configuration file that might have new, useful options you could:

#!/bin/bash
if ! [ -e /path/to/foo ]
then
    cp foo /path/to/
else
    cp -f foo /path/to/foo.new
fi

so that they still have a copy they can refer to.

share|improve this answer
    
That's a great idea about the .new file, I'll use that - thanks! –  SlappyTheFish Jul 11 '11 at 22:08
add comment

I think your commands from your Makefile are just going to be bash? If so, you can try using the bash if conditional that depends on whether the file exists:

if [ -f $FILE ];
then
   #echo "File $FILE exists"
else
   #echo "File $FILE does not exists"
   cp $SRC_FILE $FILE
fi

Bash code from here.

In the event that you discover the Makefile being run in environments where their contents are not evaluated against bash, then you could may be able to use rsync (should be installed on most unixy machines) for the copy operation. Though I just looked through the man page and didn't see a flag for copying only if dest doesn't exist - but I still sort of think that must be accommodated by rsync, just have to find the intended flag to use it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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