When I try to copy some files in an existing directory with a wildcard, I receive the error:
kubectl cp localdir/* my-namespace/my-pod:/remote-dir/
error: one of src or dest must be a remote file specification
It looks like wildcards support have been removed but I have many files to copy and my remote dir is not empty so I can't use the recursive.
How can I run a similar operation?
As a workaround you can use:
find localdir/* | xargs -I{} kubectl cp {} my-namespace/my-pod:/remote-dir/
In find you can use a wildcard to specify files you are looking for and it will copy it to the pod.
Here is what I have come up with:
kubectl exec -n <namespace> <pod_name> -- mkdir -p <dest_dir> \
&& tar cf - -C <src_dir> . | kubectl exec -i -n <namespace> <pod_name> -- tar xf - -C <dest_dir>
Notice there are two parts, first is making sure the destination directory exists. Second is using tar to archive the files, send it and unpack it in a container.
Remember, that in order for this to work, it is required that the tar
and mkdir
binaries are present in your container.
The advantage od this solution over the one proposed earlier (the one with xargs) is that it is faster because it sends all files at once, and not one by one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With