title: Bash: Check if remote directory exists using FTP
body: <p>I'm writing a bash script to send files from a linux server to a remote Windows FTP server.
  I would like to check using FTP if the folder where the file will be stored exists before attempting to create it.
  Please note that I cannot use SSH nor SCP and I cannot install new scripts on the linux server. Also, for performance issues, I would prefer if checking and creating the folders is done using only one FTP connection.</p>
  
  <p>Here's the function to send the file:</p>
  
  <pre><code>sendFile() {
      ftp -n $FTP_HOST &lt;&lt;! &gt;&gt; ${LOCAL_LOG}
          quote USER ${FTP_USER}
          quote PASS ${FTP_PASS}
          binary
          $(ftp_mkdir_loop "$FTP_PATH")
          put ${FILE_PATH} ${FTP_PATH}/${FILENAME}
          bye
  !
  }
  </code></pre>
  
  <p>And here's what <code>ftp_mkdir_loop</code> looks like:</p>
  
  <pre><code>ftp_mkdir_loop() {
      local r
      local a
      r="$@"
      while [[ "$r" != "$a" ]]; do
        a=${r%%/*}
        echo "mkdir $a"
        echo "cd $a"
        r=${r#*/}
      done
  }
  </code></pre>
  
  <p>The <code>ftp_mkdir_loop</code> function helps in creating all the folders in <code>$FTP_PATH</code> (Since I cannot do <code>mkdir -p $FTP_PATH</code> through FTP).</p>
  
  <p>Overall my script works but is not "clean"; this is what I'm getting in my log file after the execution of the script (yes, <code>$FTP_PATH</code> is composed of 5 existing directories):</p>
  
  <pre><code>(directory-name) Cannot create a file when that file already exists.
  Cannot create a file when that file already exists.
  Cannot create a file when that file already exists.
  Cannot create a file when that file already exists.
  Cannot create a file when that file already exists.
  </code></pre>