Tony
Tony

Reputation: 9581

Cygwin copy only if file already exists

I want to copy a xml file from one remote box to a bunch of other remote boxes, but I only want it to copy the file it there is currently an existing file already in place. How can I do that?

One more question, is there a way to export out the list of only if the file exists?

Upvotes: 0

Views: 1907

Answers (3)

user2023370
user2023370

Reputation: 11046

This will work from inside in a bash file:

if [ -f /path/to/file.xml ]; then
  cp /path/to/file.xml /path/to/other/file.xml
fi

A one-liner for the command line might be:

[ -f /path/to/file.xml ] && cp /path/to/file.xml /path/to/other/file.xml

Upvotes: 0

Bali C
Bali C

Reputation: 31241

I'm not sure about using cygwin but as it's windows you can just use xcopy.

xcopy \\remotebox1\file.xml \\remotebox2\file.xml /U /Y

That will copy the file only if it exists in the destination already, and will overwrite without prompting.

Upvotes: 3

dcp
dcp

Reputation: 55449

You can just do it using regular DOS commands, there's no need to resort to cygwin:

IF EXIST filename_on_remote_server COPY /Y filename_on_local_server filename_on_remote_server

Or, if you are writing a BASH script for cygwin, then you can refer to this answer.

Upvotes: 2

Related Questions