<p>I had a similar requirement and finally settled on:</p>
<pre><code>mkdir /my/dest/dir
kubectl  exec $TARGET_POD -n MYNAMESPACE -- tar -zcvf - -C /my/src/directory . | tar -zxvf - -C /my/dest/dir
</code></pre>
<p>This depends on 'tar' being available inside the container.  Similar can be done with any archive command (zip/unzip or cpio) that can deal with stdin/stdout as long as the command is in both the container and the host.</p>
<p>Also note that '*' is not native to tar, but depends on shell expansion (in my experience...), so if you want to match only certain files in a directory on the container I include 'bash'  in the call and cd to the directory first:</p>
<pre><code>kubectl  exec $TARGET_POD -n MYNAMESPACE -- bash -c &quot;cd /my/src/directory;tar -zcvf - *.log&quot; | tar -zxvf - -C /my/dest/dir
</code></pre>
<p>Again, bash or some other shell supporting * expansion must be present in the container.</p>
<p>Finally, this works in the other direction as well sending multiple files to a container:</p>
<pre><code>cd /my/src/dir
tar -zcvf - *.log | kubectl exec $TARGET_POD -n MYNAMESPACE --stdin -- tar -zxvf - -C /my/dest/dir
</code></pre>
<p>And &quot;bash&quot; is your friend if the target directory does not exist:</p>
<pre><code>tar -zcvf - *.log | kubectl exec $TARGET_POD -n MYNAMESPACE --stdin -- bash -c &quot;mkdir -p /my/dest/dir;tar -zxvf - -C /my/dest/dir&quot;
</code></pre>

CC BY-SA 4.0
2022-06-07T13:25:28.510Z
2022-06-07T13:25:28.510Z
W Goldfarb
<p>As there is no wildcard support, a workaround is to do a 2-step process to achieve the same:</p>
<ul>
<li><code>kubectl exec ...</code> to do make a new tmp directory and wildcard copy/move your desired transfer files into that dir on the container</li>
<li>use the <a href="https://devops.stackexchange.com/a/6725/30374">cmd from @cookiedough above</a> to copy all files from that dir on the container to your local</li>
</ul>
<p>Example:</p>
<pre><code>export TARGET_POD=&quot;myapp-mypod1234&quot; &amp;&amp; \
kubectl exec &quot;$TARGET_POD&quot; -n MYNAMESPACE &quot;$TARGET_POD&quot; \
-- sh -c &quot;mkdir -p /tmp/outfiles &amp;&amp; cp /path/out* /tmp/outfiles/&quot; &amp;&amp; \
kubectl cp &quot;sites/$TARGET_POD:/tmp/outfiles/.&quot; .
</code></pre>

CC BY-SA 4.0
2021-08-23T05:10:33.803Z
2021-08-23T05:10:33.803Z
ljs.dev
<p>You can do that using this command:</p>

<pre><code>kubectl cp -n [NAMESPACE] [POD_NAME]:/[POD_DIRECTORY]/. .
</code></pre>

CC BY-SA 4.0
2019-03-28T22:01:29.723Z
This is not a solution that uses wildcards to copy files. `kubectl cp` with wildcards was introduced on Kubernetes v1.14.
Eduardo Baitello
CC BY-SA 4.0
2019-03-28T22:02:54.027Z
I see, I misunderstood the question, your answer is correct. @EduardoBaitello
cookiedough
CC BY-SA 4.0
2021-04-14T17:47:53.007Z
And yet, this solved my problem.  Upvoted (note that the other answer with more votes is still the one that should be accepted).  Ideally this tidbit would have been included in that answer, but that's unreasonable to ask.
interestedparty333
CC BY-SA 4.0
2019-03-28T21:41:47.610Z
2019-03-28T21:41:47.610Z
cookiedough
<p>For everyone's benefit, I am pasting working code as suggested by <strong>heyjared</strong> user on <a href="https://github.com/kubernetes/kubernetes/issues/78854" rel="nofollow noreferrer">https://github.com/kubernetes/kubernetes/issues/78854</a></p>
<pre><code>Use find with xargs as a workaround:

find . | xargs -i{} kubectl cp {} namespace/pod:path
</code></pre>
<p>It worked for me. I replaced &quot;.&quot; with my needed source path.</p>
<p>Example:</p>
<pre><code>find &lt;source_path&gt; | xargs -i{} kubectl cp {} namespace/pod:&lt;path_inside_pod&gt;
</code></pre>
<p>Like:</p>
<pre><code>find /var/tmp/data/ | xargs -i{} kubectl cp {} namespace/pod:/tmp/
</code></pre>
<p>This will copy all contents inside local /var/tmp/data/ directory and store it under /tmp/ inside pod.</p>

CC BY-SA 4.0
2023-09-04T16:05:08.930Z
2023-09-04T16:05:08.930Z
Pankaj Yadav
<p>Wildcard support was added to <code>kubectl cp</code> in the <a href="https://github.com/kubernetes/kubernetes/pull/72641" rel="noreferrer">#72641</a> pull request.</p>
<p>It is merged but <strong>only available in Kubernetes v1.14.0</strong>. Take a look at the <a href="https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG-1.14.md#notable-features" rel="noreferrer">ChangeLog</a>:</p>
<blockquote>
<p>Notable Features:<br />
[...]<br />
• kubectl supports copying files with wild card
(#72641, @dixudx)</p>
</blockquote>
<p>However, as stated on the issue <a href="https://github.com/kubernetes/kubernetes/issues/78854" rel="noreferrer">#78854</a> the PR was made without proper tests and <strong>the feature is actually broken</strong>. The PR to fix the problem (<a href="https://github.com/kubernetes/kubernetes/pull/78928" rel="noreferrer">#78928</a>) is open since June, and it's not merged yet.</p>
<p>Although the PR to fix the feature exists, they <a href="https://github.com/kubernetes/kubernetes/issues/88670" rel="noreferrer">are also considering removing</a> the support to wildcard copy.</p>

CC BY-SA 4.0
2019-07-22T16:58:56.347Z
Does it actually work?  It seems like a lot of people [are saying it doesn't](https://github.com/kubernetes/kubernetes/issues/68222#issuecomment-500402673).
Xiong Chiamiov
CC BY-SA 4.0
2019-09-23T23:46:04.643Z
@XiongChiamiov the feature is broken and it's was not fixed yet (see [#78854](https://github.com/kubernetes/kubernetes/issues/78854))
Eduardo Baitello
CC BY-SA 4.0
2022-10-07T23:05:59.860Z
it seems still not fixed in 2022.... everyone keeps trying to reopen the issue
K.S.
CC BY-SA 4.0
2019-03-28T21:50:23.343Z
2020-07-09T21:02:56.170Z
2020-07-09T21:02:56.170Z
Eduardo Baitello
<p>I have several files named out1, out2, ... in my Kubernetes container. I want to copy them to my local computer. I am using:</p>

<pre><code>$ kubectl cp pod:/path/out* .
</code></pre>

<p>But I am getting an error:</p>

<blockquote>
  <p>tar: Removing leading `/' from member names <br>
      tar: /path/out*: Cannot stat: No such file or directory
      tar: Exiting with failure status due to previous errors <br>
      error: path/out* no such file or directory</p>
</blockquote>

<p>How can I use <code>kubectl cp</code> with wildcards? </p>

<kubernetes><kubectl>
Copy multiple files using * wildcard from kubernetes container
user674669
# Copy multiple files using * wildcard from kubernetes container

(Asked by: username_0 on 2019-03-21)

I have several files named out1, out2, ... in my Kubernetes container. I want to copy them to my local computer. I am using:
$ kubectl cp pod:/path/out* .

But I am getting an error:

tar: Removing leading `/' from member names 
      tar: /path/out*: Cannot stat: No such file or directory
      tar: Exiting with failure status due to previous errors 
      error: path/out* no such file or directory

How can I use kubectl cp with wildcards?

**Tags:** kubernetes, kubectl


---

## 5 answers

### Answer by username_4 on 2019-03-28. Score: 13 | Accepted answer

Wildcard support was added to `kubectl cp` in the [#72641](https://github.com/kubernetes/kubernetes/pull/72641) pull request.

It is merged but **only available in Kubernetes v1.14.0**. Take a look at the [ChangeLog](https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG-1.14.md#notable-features):

> Notable Features:  
> [...]  
> • kubectl supports copying files with wild card
> (#72641, @dixudx)

However, as stated on the issue [#78854](https://github.com/kubernetes/kubernetes/issues/78854) the PR was made without proper tests and **the feature is actually broken**. The PR to fix the problem ([#78928](https://github.com/kubernetes/kubernetes/pull/78928)) is open since June, and it's not merged yet.

Although the PR to fix the feature exists, they [are also considering removing](https://github.com/kubernetes/kubernetes/issues/88670) the support to wildcard copy.

> **Comments:**
> **username_7**: Does it actually work?  It seems like a lot of people [are saying it doesn't](https://github.com/kubernetes/kubernetes/issues/68222#issuecomment-500402673).  
> **username_4**: @XiongChiamiov the feature is broken and it's was not fixed yet (see [#78854](https://github.com/kubernetes/kubernetes/issues/78854))  
> **username_8**: it seems still not fixed in 2022.... everyone keeps trying to reopen the issue  

---

### Answer by username_3 on 2019-03-28. Score: 16

You can do that using this command:
kubectl cp -n [NAMESPACE] [POD_NAME]:/[POD_DIRECTORY]/. .

> **Comments:**
> **username_4**: This is not a solution that uses wildcards to copy files. `kubectl cp` with wildcards was introduced on Kubernetes v1.14.  
> **username_3**: I see, I misunderstood the question, your answer is correct. @EduardoBaitello  
> **username_5**: And yet, this solved my problem.  Upvoted (note that the other answer with more votes is still the one that should be accepted).  Ideally this tidbit would have been included in that answer, but that's unreasonable to ask.  

---

### Answer by username_1 on 2022-06-07. Score: 3

I had a similar requirement and finally settled on:
mkdir /my/dest/dir
kubectl  exec $TARGET_POD -n MYNAMESPACE -- tar -zcvf - -C /my/src/directory . | tar -zxvf - -C /my/dest/dir

This depends on 'tar' being available inside the container.  Similar can be done with any archive command (zip/unzip or cpio) that can deal with stdin/stdout as long as the command is in both the container and the host.
Also note that '*' is not native to tar, but depends on shell expansion (in my experience...), so if you want to match only certain files in a directory on the container I include 'bash'  in the call and cd to the directory first:
kubectl  exec $TARGET_POD -n MYNAMESPACE -- bash -c "cd /my/src/directory;tar -zcvf - *.log" | tar -zxvf - -C /my/dest/dir

Again, bash or some other shell supporting * expansion must be present in the container.
Finally, this works in the other direction as well sending multiple files to a container:
cd /my/src/dir
tar -zcvf - *.log | kubectl exec $TARGET_POD -n MYNAMESPACE --stdin -- tar -zxvf - -C /my/dest/dir

And "bash" is your friend if the target directory does not exist:
tar -zcvf - *.log | kubectl exec $TARGET_POD -n MYNAMESPACE --stdin -- bash -c "mkdir -p /my/dest/dir;tar -zxvf - -C /my/dest/dir"


---

### Answer by username_2 on 2021-08-23. Score: 3

As there is no wildcard support, a workaround is to do a 2-step process to achieve the same:

kubectl exec ... to do make a new tmp directory and wildcard copy/move your desired transfer files into that dir on the container
use the cmd from @username_3 above to copy all files from that dir on the container to your local

Example:
export TARGET_POD="myapp-mypod1234" && \
kubectl exec "$TARGET_POD" -n MYNAMESPACE "$TARGET_POD" \
-- sh -c "mkdir -p /tmp/outfiles && cp /path/out* /tmp/outfiles/" && \
kubectl cp "sites/$TARGET_POD:/tmp/outfiles/." .


---

### Answer by username_6 on 2023-09-04. Score: 0

For everyone's benefit, I am pasting working code as suggested by heyjared user on https://github.com/kubernetes/kubernetes/issues/78854
Use find with xargs as a workaround:

find . | xargs -i{} kubectl cp {} namespace/pod:path

It worked for me. I replaced "." with my needed source path.
Example:
find  | xargs -i{} kubectl cp {} namespace/pod:

Like:
find /var/tmp/data/ | xargs -i{} kubectl cp {} namespace/pod:/tmp/

This will copy all contents inside local /var/tmp/data/ directory and store it under /tmp/ inside pod.


---

