<p>I am copying a local directory into Kubernetes pod using <code>kubectl cp</code> command</p>

<pre><code>kubectl cp test $POD:/tmp
</code></pre>

<p>It copies the <code>test</code> directory into Kubernetes pod <code>/tmp</code> directory.</p>

<p>Now I want to overwrite the test directory in the pod.  I did not find any option to overwrite directory while copying using <code>kubectl cp</code> command.</p>

<p>Currently, I am deleting the test directory from the pod and then copy the directory. </p>

<pre><code>kubectl exec $POD -- sh -c 'rm -rf /tmp/test'
kubectl cp test $POD:/tmp
</code></pre>

<p>This is working fine, but in case any error comes while copying, existing directory from pod will also be deleted.</p>

<p>How can I overwrite the pod directory with a local directory without deleting the pod directory first?</p>

<p>Thanks in advance.</p>

Kishore
<p><strong>Currently there is unfortunatelly no way to achieve your desired state with <code>kubectl cp</code> command.</strong></p>
<p>If there are some undocumented features, please feel free to edit this answer and provide the solution, but currently there is no single place in documentation that could suggest the opposite.</p>
<p>Neither <a href="https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#cp" rel="nofollow noreferrer">here</a> nor in the context help of the <code>kubectl</code> command, available by running <code>kubectl cp --help</code>, there is no option mentioned that would modify the default operation of the<code> kubectl cp</code> command, which is basically a merge of the content of the already existing directory and copied one.</p>
<pre><code>$ kubectl cp --help
</code></pre>
<pre><code>Copy files and directories to and from containers.
    
Examples:
  # !!!Important Note!!!
  # Requires that the 'tar' binary is present in your container
  # image.  If 'tar' is not present, 'kubectl cp' will fail.
  #
  # For advanced use cases, such as symlinks, wildcard expansion or
  # file mode preservation, consider using 'kubectl exec'.
  
  # Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace &lt;some-namespace&gt;
  tar cf - /tmp/foo | kubectl exec -i -n &lt;some-namespace&gt; &lt;some-pod&gt; -- tar xf - -C /tmp/bar
  
  # Copy /tmp/foo from a remote pod to /tmp/bar locally
  kubectl exec -n &lt;some-namespace&gt; &lt;some-pod&gt; -- tar cf - /tmp/foo | tar xf - -C /tmp/bar
  
  # Copy /tmp/foo_dir local directory to /tmp/bar_dir in a remote pod in the default namespace
  kubectl cp /tmp/foo_dir &lt;some-pod&gt;:/tmp/bar_dir
  
  # Copy /tmp/foo local file to /tmp/bar in a remote pod in a specific container
  kubectl cp /tmp/foo &lt;some-pod&gt;:/tmp/bar -c &lt;specific-container&gt;
  
  # Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace &lt;some-namespace&gt;
  kubectl cp /tmp/foo &lt;some-namespace&gt;/&lt;some-pod&gt;:/tmp/bar
  
  # Copy /tmp/foo from a remote pod to /tmp/bar locally
  kubectl cp &lt;some-namespace&gt;/&lt;some-pod&gt;:/tmp/foo /tmp/bar

Options:
    -c, --container='':
    Container name. If omitted, use the kubectl.kubernetes.io/default-container annotation for selecting the
    container to be attached or the first container in the pod will be chosen

    --no-preserve=false:
    The copied file/directory's ownership and permissions will not be preserved in the container

    --retries=0:
    Set number of retries to complete a copy operation from a container. Specify 0 to disable or any negative
    value for infinite retrying. The default is 0 (no retry).

Usage:
  kubectl cp &lt;file-spec-src&gt; &lt;file-spec-dest&gt; [options]

Use &quot;kubectl options&quot; for a list of global command-line options (applies to all commands).
</code></pre>
<p>Basically the default behavior of <code>kubectl cp</code> command is <strong>a merge of the content of source and destination directory</strong>. Let's say we have local directory <code>/tmp/test</code> containing:</p>
<pre><code>/tmp/test$ ls
different_file.txt
</code></pre>
<p>with single line of text <code>&quot;some content&quot;</code>. If we copy our local <code>/tmp/test directory</code> to <code>/tmp</code> directory on our <code>Pod</code>, which already contains <code>test</code> folder with a different file, let's say <code>testfile.txt</code>, the content of both directories will be merged, so our destination <code>/tmp/test</code> will contain eventually:</p>
<pre><code>/tmp/test# ls
different_file.txt  testfile.txt
</code></pre>
<p>If we change the content of our local <code>different_file.txt</code> to <code>&quot;yet another content&quot;</code> and run again the command:</p>
<pre><code>kubectl cp /tmp/test pod-name:/tmp
</code></pre>
<p>it will only override the destination <code>different_file.txt</code> which is already present in the destination <code>/tmp/test</code> directory.</p>
<p>Currently there is no way to override this default behavior.</p>
