How do I tell rsync to run only if the destination directory exists?

696

Solution 1

These two flags look like what you're looking for:

--existing, --ignore-non-existing

From the man page:

--existing, --ignore-non-existing
This tells rsync to skip creating files (including directories) that do not exist yet on the destination. If this option is combined with the --ignore-existing option, no files will be updated (which can be useful if all you want to do is delete extraneous files).

Solution 2

AFAIK no, but you can simulate the behavour with a trailing slash:

rsync -av dir_to_backup /Volumes/External/;

It will exit with an error if the directory does not exist (which may or may not be desired).

Also, you can always optimize away the if:

test -e $DIR && rsync -av ...

Solution 3

No, there does not seem to be any such option, as far as I can see from the manpage.

Share:
696
Gordana Jekic
Author by

Gordana Jekic

Updated on June 14, 2022

Comments

  • Gordana Jekic
    Gordana Jekic almost 2 years

    I'm trying to pass string value as a function parameter, but I need string value inside function

    Here is the code:

    EDIT:

    var array = [{someValue: 5}, {someOtherValue: 10}];
    var newArray = [];
    var obj = {value: "someValue"};
    var setValue = function(choosenValue) {
    
    for (let i = 0; i < array.length; i++) {
       newArray.push({
       choosenValue: array[i].choosenValue
      });
    }
    
    }
    
    setValue(obj.value);
    

    EDIT: What I want to get is newArray=[someValue : 5]; I want to create new array from array with key that is passed as parameter and chhosenValue is also value inside array, and I want it's value. It should be array of objects with same keys but different values.

    Value from object will change and depends what is chosen I need to loop through array. Since choosenValue is string I can loop properly through array.

    I tried with choosenValue.valueOf(), but it doesn't work. Any other idea?

    I'm getting inside push() that choosenValue is undefined.

    • Geeky
      Geeky over 7 years
      what is array in the code?are you missing anything here
    • SLaks
      SLaks over 7 years
      What do you think array[i].choosenValue means? If you want to use your parameter, just use your parameter.
    • Dan O
      Dan O over 7 years
      please provide more of your code. where is array defined?
    • rogeriolino
      rogeriolino over 7 years
      Pass choosenValue as object index: array[i][choosenValue]
    • Eduardo M - bbaaxx
      Eduardo M - bbaaxx over 7 years
      array[i].chosenvalue is not defined nor can be defined because what you have in array[i] is not yet an object, it is undefined. You cannot assign properties (choosenValue) to undefined.
    • Redu
      Redu over 7 years
      If you had already defined newArray and array somewhere in the code what is the problem?
    • Gordana Jekic
      Gordana Jekic over 7 years
      Take a look at my edits.
    • Moob
      Moob over 7 years
      It would be a lot easier to understand what you want if you gave an example of the expected output. Most of the time spent helping you has gone on simply trying to understand your requirements. Voting to close.
  • Eduardo M - bbaaxx
    Eduardo M - bbaaxx over 7 years
    Also you have to initialize an object in array[i] such as if (!array[i]) {array[i] = {};)
  • Gordana Jekic
    Gordana Jekic over 7 years
    Where should I do this? Before push()?
  • Gordana Jekic
    Gordana Jekic over 7 years
    I'm getting empty array. :(
  • Andrea Limoli
    Andrea Limoli over 7 years
    See my full example in the answer and tell me what don't you uderstand.
  • Gordana Jekic
    Gordana Jekic over 7 years
    I understand, but this is not what I need. You are returning object, and I'm getting only one object, and I need array of objects. I have a lot of them.
  • Andrea Limoli
    Andrea Limoli over 7 years
    I don't understand. Give me the final result (object or array) of what do you want to retrieve.
  • Gordana Jekic
    Gordana Jekic over 7 years
    I want to return array. And with your example it returns new object with value of last member of array. I'm getting only one object and I need to create new array. Result should be array.
  • Andrea Limoli
    Andrea Limoli over 7 years
    The last example returns an array.
  • Gordana Jekic
    Gordana Jekic over 7 years
    Returns empty array. :(
  • Moob
    Moob over 7 years
    @Gordana it would be a lot easier to understand what you want if you gave an example of the expected output. Most of the time these volunteers have spent helping you has gone on simply trying to understand your requirements.
  • Moob
    Moob over 7 years
    I think you're on the right lines but until the OP clarifies the requirements it just guesswork. Good answer though.
  • Gordana Jekic
    Gordana Jekic over 7 years
    thank you for detailed explanation. This newArray.push({ choosenValue: array[i][choosenValue] }); works only my key has name exactly "choosenValue" not value that I got from obj "someValue". How to fix that?
  • Andrea Limoli
    Andrea Limoli over 7 years
    Please, answer if the provided solution is correct.
  • Andrea Limoli
    Andrea Limoli over 7 years
    @GordanaJekic have you tries the last example or not?!
  • Gordana Jekic
    Gordana Jekic over 7 years
    Your edit works. :) Only now I have problem to push more keys with values in array. For example I want to push id with value array[i].id in new array?
  • Gordana Jekic
    Gordana Jekic over 7 years
    I found a way. Thank you.