Skip to content

Commit 2363c92

Browse files
authored
Merge pull request #28701 from rajeshdeshpande02/add-cp-command
Adding kubectl cp command examples in the cheat sheet
2 parents 8b9ed00 + d0f9411 commit 2363c92

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

content/en/docs/reference/kubectl/cheatsheet.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,24 @@ kubectl exec my-pod -c my-container -- ls / # Run command in existing po
323323
kubectl top pod POD_NAME --containers # Show metrics for a given pod and its containers
324324
kubectl top pod POD_NAME --sort-by=cpu # Show metrics for a given pod and sort it by 'cpu' or 'memory'
325325
```
326+
## Copy files and directories to and from containers
327+
328+
```bash
329+
kubectl cp /tmp/foo_dir my-pod:/tmp/bar_dir # Copy /tmp/foo_dir local directory to /tmp/bar_dir in a remote pod in the current namespace
330+
kubectl cp /tmp/foo my-pod:/tmp/bar -c my-container # Copy /tmp/foo local file to /tmp/bar in a remote pod in a specific container
331+
kubectl cp /tmp/foo my-namespace/my-pod:/tmp/bar # Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace my-namespace
332+
kubectl cp my-namespace/my-pod:/tmp/foo /tmp/bar # Copy /tmp/foo from a remote pod to /tmp/bar locally
333+
```
334+
{{< note >}}
335+
`kubectl cp` requires that the 'tar' binary is present in your container image. If 'tar' is not present,`kubectl cp` will fail.
336+
For advanced use cases, such as symlinks, wildcard expansion or file mode preservation consider using `kubectl exec`.
337+
{{< /note >}}
338+
339+
```bash
340+
tar cf - /tmp/foo | kubectl exec -i -n my-namespace my-pod -- tar xf - -C /tmp/bar # Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace my-namespace
341+
kubectl exec -n my-namespace my-pod -- tar cf - /tmp/foo | tar xf - -C /tmp/bar # Copy /tmp/foo from a remote pod to /tmp/bar locally
342+
```
343+
326344

327345
## Interacting with Deployments and Services
328346
```bash

0 commit comments

Comments
 (0)