0

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?

Tony
  • 8,681
  • 7
  • 36
  • 55

3 Answers3

3

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.

Bali C
  • 30,582
  • 35
  • 123
  • 152
2

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.

Community
  • 1
  • 1
dcp
  • 54,410
  • 22
  • 144
  • 164
  • I don't think the standard windows dos prompt supports UNC paths? – Tony Jun 19 '12 at 17:40
  • @Tony - Yes, it does. You preceed the network location with \\. For example, \\somenetworklocation\somefolder\someotherfolder. Refer to this link for more details: http://en.wikipedia.org/wiki/Path_%28computing%29#Uniform_Naming_Convention – dcp Jun 19 '12 at 17:44
  • can't seem to get it to work it either throws The system cannot find the file specified or if I just try to CD to my remote box I get \\\folder\ cmd does not support UNC paths as current directories. – Tony Jun 19 '12 at 17:56
  • 1
    @Tony - To be able to CD to the folder, you'd have to mount it as a drive. But from your original question, I'm not sure why you would need to CD to the folder. You can just execute the copy command and specify the UNC path as the destination folder. There's no need to CD to the destination. – dcp Jun 19 '12 at 18:25
  • Thanks I realized that as I was troublshooting the issue. – Tony Jun 19 '12 at 22:04
0

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
user2023370
  • 10,488
  • 6
  • 50
  • 83