Linux cp command to copy a folder to current directory - cp

I am wondering if there is an identical command for copying a folder to current directory like it did using the old MS-DOS. Let's say my current directory location is:
/var/www/
I have folders and files at:
/home/hope/subfolder/docs/
/home/hope/subfolder/images/
/home/hope/subfolder/.config
/home/hope/subfolder/readme.txt
I know that the following command:
cp -rT /home/hope/subfolder .
will copy all the files (even dot hidden files) and folders within the "subfolder" folder to the current directory, so the result will be:
/var/www/docs/
/var/www/images/
/var/www/.config
/var/www/readme.txt
Looks like the command to that to copy the source folder to the current location is:
cp -rT /home/hope/subfolder ./subfolder
although this is fine, I find it that sometimes I will make mistakes for complicated folder names for the destination, so is there a way to use a command like:
cp -rT /home/hope/subfolder .
or even like this
cp -rT /home/hope/subfolder /var/www/.
to have the following result:
/var/www/subfolder/docs/
/var/www/subfolder/images/
/var/www/subfolder/.config
/var/www/subfolder/readme.txt
Thank you.

Just omit the -T parameter, as that's what prevents the command from working properly:
cp -r /home/hope/subfolder .
The -T parameter treats the target argument as a file, so no copying will be performed at all if that is actually a directory.
A friendly reminder: virtually all Unix commands have a --help command line argument that is worth trying out in case of a trouble :)

For me the main barrier was the /home part. I needed to copy files from a folder in my home that started with the letter 'a' to my current folder, which was not home. So I used:
cp home/tmp/a* ./
the first line worked for me. While I was trying commands like:
cp ~/home/tmp/a* ./
but this didn't work.

Related

Copy the latest version of an SQL script within Dockerfile

I'm working with a number of different datasets and Docker. For example, I have a number of files in a folder called /databases like model_1.sql, model_2.sql, model_3.sql etc. I would like my Dockerfile to be configured so that when I copy a file over from my databases folder, it picks out only the most recent file (e.x. model_3.sql) while ignoring the others. Is this possible?
edit: since my question apparently wasn't clear enough, here is the pertinent line -
# Copy our newest version
COPY database/model_3.sql /
Rather than manually typing the 3, I would like to be able to do something along the lines of model_*.sql but not copy all of them, copy the the most recent. So if I eventually add a model_4.sql, I will not need to update my Dockerfile when I run it, it will automatically add model_4 instead of model_3.
Yes you can achieve the same using the --build-args option.
Example: Dockerfile
FROM image:tag
ARG LATEST_FILE #Here I'll parse the LATEST_FILE info as a build argument.
COPY ${LATEST_FILE} #And the value for LATEST_FILE is replaced here.
Docker build command:
docker build -f myDockerfile --build-arg LATEST_FILE=$(ls -t database/model_*.sql | head -1) -t imagename:tag .
$(ls -t database/model_*.sql | head -1) will get you the latest file and will be copied in the COPY step.
Please be aware that parsing ls is only dangerous when the files name contains spaces or some funny characters. If you guarantee that the filenames will not have such characters, then parsing ls is ok.

Unable to replace files in docker image with ADD or COPY

I am trying to replace some files in a folder in a docker image. I am using the following command inside Dockerfile:
COPY /home/adietz/Work/20_BSP/Jenkins/venvs/linux_selenium/lib/python2.7/site-packages/browsermobproxy /usr/local/lib/python2.7/dist-packages/browsermobproxy
which results in an error
Step 4/12 : COPY /home/adietz/Work/20_BSP/Jenkins/venvs/linux_selenium/lib/python2.7/site-packages/browsermobproxy /usr/local/lib/python2.7/dist-packages/browsermobproxy
lstat home/adietz/Work/20_BSP/Jenkins/venvs/linux_selenium/lib/python2.7/site-packages/browsermobproxy: no such file or directory
Replacing COPY with ADD results in the same error. Also the following command
COPY /home/adietz/Work/20_BSP/Jenkins/venvs/linux_selenium/lib/python2.7/site-packages/browsermobproxy /usr/local/lib/python2.7/dist-packages/
and gives identical(!) error.
Both paths are folders. The folder in the docker image already exists; I just want to replace the files.
What am I doing wrong here...?
It seems you cannot use absolute paths in the COPY command AND you can only copy files which are inside the folder you are running the docker command.
So to copy these files you have to do e.g. the following steps
cp -r /home/adietz/Work/20_BSP/Jenkins/venvs/linux_selenium/lib/python2.7/site-packages/browsermobproxy .
and then add to the Dockerfile:
COPY browsermobproxy/ /usr/local/lib/python2.7/dist-packages/
A symbolic link also does not work...
I had this same issue and realized that COPY or ADD won't work with node_modules that are referenced instead of directly installed into the project. When I switched this it worked for me.

How to copy multiple files in one layer using a Docker file to different locations?

Is it possible to copy multiple files to different locations in a Dockerfile?
I'm looking to go from:
COPY outputs/output/build/.tools /root/.tools
COPY outputs/output/build/configuration /root/configuration
COPY outputs/output/build/database/postgres /root/database/postgres
I have tried the following, but no joy:
COPY ["outputs/output/build/.tools /root/.tools","outputs/output/build/configuration /root/configuration","outputs/output/build/database/postgres /root/database/postgres"]
Not sure if this is even possible.
Create a file .dockerignore in your docker build context directory. Create a soft link (ln) of the root directory you want to copy and exclude the directories you don't want in your .dockerignore.
In your case, you don't need the soft link as the directory is already in the docker build context. Now, add the directories in the .dockerignore file that you don't want eg. if you don't want bin directory you can do that as,
# this is .dockerignore file
outputs/output/build/bin*
Finally in your Dockerfile,
COPY outputs/output/build/ /root/
Details are here.
it looks like you are trying to do bash style [] brace expansion
RUN command uses sh NOT bash
see this SO article discussing it
Bash brace expansion not working on Dockerfile RUN command
or the docker reference
https://docs.docker.com/engine/reference/builder/#/run

docker cp not working

I'm following this tutorial and when I get to the part where I call:
cp /tf_files/stripped_retrained_graph.pb bazel-bin/tensorflow/examples/android/assets/stripped_output_graph.pb
and
cp /tf_files/retrained_labels.txt bin/tensorflow/examples/android/assets/imagenet_comp_graph_label_strings.txt
They both say "No such file or directory".
As you can see in this image I can cd to the tf_files folder and see that the files are there.
I can also cd to /tensorflow/tensorflow/examples/android/assets and call ls which shows there's just a BUILD file there.
In the cp command is there supposed to already be a stripped_output_graph.pb file in the destination which gets replaced? Or is it meant to just be creating a new file there?
Is there some way of doing cp [source] [current directory] rather than specifying the destination as a path?
I've tried removing the file path part in hope that it just uses the source filename but that doesn't work.
Calling
cp /tf_files/stripped_retrained_graph.pb /tensorflow/tensorflow/examples/android/assets/stripped_output_graph.pb
and
cp /tf_files/retrained_labels.txt /tensorflow/tensorflow/examples/android/assets/imagenet_comp_graph_label_strings.txt
finally worked, wasn’t at all obvious that I’d have to change the destination path or what it should be though.
Also I accidentally saved a file as .p rather than .pb but managed to remove it using $ docker exec <container> rm -rf /tensorflow/tensorflow/examples/android/asset
s/stripped_output_graph.p
Now I managed to copy the files in correctly, but then when I installed the app it was still just running the regular demo app.
Not sure why it didn’t work, so frustrating.
When I rebuilt it after copying the files in I got these conflict messages
Are these normal to have?
It looks like maybe a different labels file is taking priority over mine, how can I reach the external/inception5h/imagenet_comp_graph_label_strings.txt file to delete it so my file is used instead?
Does the “external” part mean that I can’t actually access it?

Dockerhub automated build fails, "not a directory" when adding file

I am trying to use docker hub to automatically build something that builds fine locally. It fails saying:
Build process failed: stat /var/lib/docker/aufs/mnt/1be9db483fa6f3de2596b5261e7c450de8df503185e579278396f14ba179c257/bin/run.sh: not a directory
You can view the build itself here:
https://hub.docker.com/r/zbyte64/rethinkdb-tlsproxy/builds/bjclhq33kgwxxvn6nbfsgyh/
run.sh is in the same directory as Dockerfile, it seems the build path on dockerhub is different then where it stores the Dockerfile.
I have tried the following variations:
COPY run.sh /bin
ADD ./run.sh /bin
The COPY command (on Dockerhub's Docker version) expects the target file on the right hand side, not just the target directory. The following command should work for you even on Dockerhub.
COPY run.sh /bin/run.sh
Or if you want to use ADD, include the trailing slash.
ADD ./run.sh /bin/
What is actually happening? From https://docs.docker.com/engine/reference/builder/#add :
ADD src dest
"If dest does not end with a trailing slash, it will be considered a regular file and the contents of src will be written at dest."
Without the trailing slash on /bin, it expects run.sh to be a directory being copied to directory /bin.
I don't know why, but dockerhub wants the first argument of COPY or ADD to be a directory - not a file. I am running Docker 1.9.1 locally and that is not the case. I switched the Dockerfile to copy a resource directory instead of individual files and things started to work.

Resources