<p>It seems the <code>kubectl cp</code> command uses <code>tar</code> command in the background. How can I use (pass them via the <code>kubectl cp</code> command) some of <code>tar</code> command options such as <code>--exclude</code>? For example, this could be helpful to exclude specific files/directories that are not needed to be copied out of the container.</p>

<p>Thanks</p>

imriss
<p>You can do it as - </p>

<pre><code>kubectl exec -n &lt;some-namespace&gt; &lt;some-pod&gt; -- tar cf - --exclude='pattern' /tmp/foo | tar xf - -C /tmp/bar
</code></pre>

<p>As example -</p>

<p>I got a pod running in the namespace "pankaj"; and inside my container i got 2 files at path - /tmp/foo</p>

<pre><code>/tmp/foo # ls
file1.txt  file2.txt
</code></pre>

<p>My use is I want to copy only file file1.txt  so I will be doing it like -</p>

<pre><code>kubectl exec -n pankaj &lt;pod name&gt; -- tar cf - --exclude='file2.txt' tmp/foo | tar xf - -C /Users/pa357856/test
</code></pre>

<p>the result will be I will get only file1.txt</p>

<pre><code>$ pwd
/Users/pa357856/test
$ ls tmp/foo/
file1.txt
</code></pre>
